cv2.DescriptorMatcher_create()使用BruteForce-Hamming(2)和BruteForce-Hamming的写法
时间: 2024-04-30 17:22:56 浏览: 192
使用BruteForce-Hamming(2)的写法:
```python
import cv2
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
```
使用BruteForce-Hamming的写法:
```python
import cv2
matcher = cv2.DescriptorMatcher_create(cv2.NORM_HAMMING)
```
注意:BruteForce-Hamming是使用NORM_HAMMING作为距离度量的,所以在使用BruteForce-Hamming时,需要将matcher的距离度量设置为NORM_HAMMING。
相关问题
import cv2 # 读取两幅待处理的图像 img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE) img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE) # 对图像进行高斯模糊 img1 = cv2.GaussianBlur(img1, (5, 5), 0) img2 = cv2.GaussianBlur(img2, (5, 5), 0) # 使用Shi-Tomasi算法检测特征点 corners1 = cv2.goodFeaturesToTrack(img1, 100, 0.01, 10) corners2 = cv2.goodFeaturesToTrack(img2, 100, 0.01, 10) # 对特征点进行亚像素定位 corners1 = cv2.cornerSubPix(img1, corners1, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)) corners2 = cv2.cornerSubPix(img2, corners2, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)) # 对特征点进行匹配 matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING) kps1, descs1 = sift.detectAndCompute(img1, None) kps2, descs2 = sift.detectAndCompute(img2, None) matches = matcher.match(descs1, descs2) # 使用RANSAC算法进行匹配点筛选 src_pts = np.float32([kps1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([kps2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 对图像进行配准和拼接 result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0])) result[0:img2.shape[0], 0:img2.shape[1]] = img2 # 显示结果 cv2.imshow('Result', result) cv2.waitKey() cv2.destroyAllWindows()改进这段代码使其输出特征点连线图和拼接图
import cv2
import numpy as np
# 读取两幅待处理的图像
img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)
# 对图像进行高斯模糊
img1 = cv2.GaussianBlur(img1, (5, 5), 0)
img2 = cv2.GaussianBlur(img2, (5, 5), 0)
# 使用Shi-Tomasi算法检测特征点
corners1 = cv2.goodFeaturesToTrack(img1, 100, 0.01, 10)
corners2 = cv2.goodFeaturesToTrack(img2, 100, 0.01, 10)
# 对特征点进行亚像素定位
corners1 = cv2.cornerSubPix(img1, corners1, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
corners2 = cv2.cornerSubPix(img2, corners2, (5, 5), (-1, -1), criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001))
# 对特征点进行匹配
sift = cv2.xfeatures2d.SIFT_create()
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
kps1, descs1 = sift.detectAndCompute(img1, None)
kps2, descs2 = sift.detectAndCompute(img2, None)
matches = matcher.match(descs1, descs2)
# 使用RANSAC算法进行匹配点筛选
src_pts = np.float32([kps1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kps2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 画出特征点连线图
matchesMask = mask.ravel().tolist()
h, w = img1.shape
draw_params = dict(matchColor=(0, 255, 0), singlePointColor=None, matchesMask=matchesMask, flags=2)
img3 = cv2.drawMatches(img1, kps1, img2, kps2, matches, None, **draw_params)
cv2.imshow('Feature Matching', img3)
# 对图像进行配准和拼接
result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0]))
result[0:img2.shape[0], 0:img2.shape[1]] = img2
# 显示结果
cv2.imshow('Result', result)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2 import numpy as np # 读取两幅图像 img1 = cv2.imread('D:\wzk\JIEMIAN\images\er_duibidu.jpg') img2 = cv2.imread('D:\wzk\JIEMIAN\images\yi_duibidu.jpg') # 将两幅图像转换为灰度图像 gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 提取图像特征点 orb = cv2.ORB_create() kp1, des1 = orb.detectAndCompute(gray1, None) kp2, des2 = orb.detectAndCompute(gray2, None) # 匹配特征点 matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING) matches = matcher.match(des1, des2) # 选择最佳匹配点 matches = sorted(matches, key=lambda x: x.distance) good_matches = matches[:int(len(matches)*0.15)] # 绘制特征点连接图 img3 = cv2.drawMatches(img1, kp1, img2, kp2, good_matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # 计算变换矩阵 src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # 拼接图像 result = cv2.warpPerspective(img1, M, (img1.shape[1] + img2.shape[1], img1.shape[0])) result[0:img2.shape[0], 0:img2.shape[1]] = img2 # 保存连接图 cv2.imwrite('D:\wzk\JIEMIAN\Result\ORB-pz.jpg', img3) # 保存第二幅图像 cv2.imwrite('D:\wzk\JIEMIAN\Result\ORB-pj.jpg', result) # 显示结果 cv2.namedWindow("Keypoint Matches", cv2.WINDOW_NORMAL) cv2.imshow("Keypoint Matches", img3) cv2.namedWindow("Result",cv2.WINDOW_NORMAL) cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows()改进为对文件夹内的多幅图像进行配准拼接
可以使用循环遍历文件夹内的所有图像,对每幅图像进行配准拼接,最终将所有拼接结果合并成一张大图像。
以下是示例代码:
```
import cv2
import numpy as np
import os
# 定义文件夹路径和结果保存路径
folder_path = 'D:\wzk\JIEMIAN\images'
result_path = 'D:\wzk\JIEMIAN\Result\ORB-pj.jpg'
# 获取文件夹内所有图像路径
img_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.jpg')]
# 遍历所有图像,进行配准拼接
result = cv2.imread(img_paths[0])
for i in range(1, len(img_paths)):
img = cv2.imread(img_paths[i])
# 将两幅图像转换为灰度图像
gray1 = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 提取图像特征点
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(gray1, None)
kp2, des2 = orb.detectAndCompute(gray2, None)
# 匹配特征点
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(des1, des2)
# 选择最佳匹配点
matches = sorted(matches, key=lambda x: x.distance)
good_matches = matches[:int(len(matches)*0.15)]
# 计算变换矩阵
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# 拼接图像
result = cv2.warpPerspective(result, M, (result.shape[1] + img.shape[1], result.shape[0]))
result[0:img.shape[0], result.shape[1]-img.shape[1]:] = img
# 保存拼接结果
cv2.imwrite(result_path, result)
# 显示结果
cv2.namedWindow("Result",cv2.WINDOW_NORMAL)
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
代码中使用了 `os` 模块来遍历文件夹内所有图像的路径,然后对每幅图像进行配准拼接。最终将所有拼接结果合并成一张大图像,并保存到指定路径。
阅读全文