在Mac下pycharm中基于opencv做几何校正和单应性矩阵求解

本文介绍了一种使用Python和OpenCV在Mac系统上实现图像几何校正的方法。通过SIFT特征匹配算法获取图像间的对应点,并利用RANSAC算法计算单应性矩阵,最终实现了图像配准及几何校正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这段时间做了些工作,在Mac下基于opencv做了单应性矩阵的求解,下面的文档中给出了循环读取文件的代码,读出文件以后两两做siftImageAlignment,求出了几何校正后影像result和单应性矩阵H。

import numpy
import os
import math
import cv2
 - #read file_image in file
def image_file(file_path):
    if os.path.exists(file_path):
        print("files exists")
    else:
        print("no files exists")

    image_dir = []
    files = os.listdir(file_path)
    for fi in files:
        fi_d = os.path.join(file_path, fi)
        if os.path.isdir(fi_d):
                print(os.path.join(file_path, fi_d))
                image_file(fi_d)
        else:
            if fi.endswith('JPG'):
                print(os.path.join(file_path, fi_d))
                image_dir.append(fi_d)
            else:
                print("error file")
    print len(image_dir)
    return image_dir


# save_path change
def save_file(img_dir,save_image,save_path):
    [dirname,filename] = os.path.split(img_dir)
    save_image_ = os.path.join(save_path,filename)
    cv2.imwrite(save_image_, save_image)
    return save_image_


# extract sift_feature and keypoints
def sift_kp(image):
    print 'sift_kp'
    gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    sift = cv2.xfeatures2d_SIFT.create()
    kp,des = sift.detectAndCompute(image,None)
    kp_image = cv2.drawKeypoints(gray_image,kp,None)
    return kp_image,kp,des

# good match
def get_good_match(des1,des2):
    print 'get_good_match'
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)
    good = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good.append(m)
    return good

 - # image alignment
def siftImageAlignment(img1,img2):
   print 'siftImageAlignment'
   _,kp1,des1 = sift_kp(img1)
   _,kp2,des2 = sift_kp(img2)
goodMatch = get_good_match(des1,des2)
   print 'get_good_match_end'
   if len(goodMatch) > 4:
       ptsA= numpy.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
       ptsB = numpy.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
       ransacReprojThreshold = 4
       H, status =cv2.findHomography(ptsA,ptsB,cv2.RANSAC,ransacReprojThreshold);
       print(H)
       print 'findHomography'
       imgOut = cv2.warpPerspective(img2, HT, (img1.shape[1],img1.shape[0]),flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
       print 'warpPerspective'
   return imgOut,H,status
print 'begin'
image_dir = image_file('/Users/xxx/downloads/data')
save_path = "/Users/xxx/downloads/results"
numbers = 0
K = numpy.array([[3.95, 0, 2.4], [0, 3.95, 1.8], [0, 0, 1]])
result = cv2.imread(image_dir[0])
 - #circle read image_dir to deal with jpg images
for numbers in range(len(image_dir)-1):
    print "this is the circle %d of image_dir" %(numbers)
    if len(image_dir) < 1:
        print "not enough images"
    else :
        img1 = result;
        img2 = cv2.imread(image_dir[numbers+1])
        result, H, _ = siftImageAlignment(img1, img2)
        save_file(image_dir[numbers+1],result,save_path)
    print 'result'
print("final end")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值