python - 使用 Python 在两个图像中查找匹配的数据点
问题描述
我有两张图片,即图 1 和图 2。两者均取自同一来源但未对齐。任务是在这两个图像中找到共同的数据点,并在两个图像中匹配的数据点之间画线。我看这个图应该像图 4。
到目前为止,我已经使用过 OpenCV 并编写了以下代码:
import cv2
import matplotlib.pyplot as plt
img_file1= "Fig_1.png"
img_file2= "Fig_2.png"
img1= cv2.imread(img_file1)
img2= cv2.imread(img_file2)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
figure, ax = plt.subplots(1, 2, figsize=(16, 8))
ax[0].imshow(img1, cmap='gray')
ax[1].imshow(img2, cmap='gray')
#sift
sift = cv2.xfeatures2d.SIFT_create()
keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None)
keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None)
#feature matching
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)
matches = bf.match(descriptors_1,descriptors_2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1, keypoints_1, img2, keypoints_2, matches[:50], img2, flags=2)
plt.imshow(img3),plt.show()
这给出了意想不到的结果,见图 4。加上看起来相当混乱和不清楚。
谁能帮助我如何做到这一点?提前致谢。
图。1
图像3
图 3
解决方案
基本上,这在我看来是一个注册问题(图像需要注册)。
这是您可以执行的操作:
from skimage.registration import phase_cross_correlation
from skimage.io import imread
from skimage.measure import label, regionprops
from skimage.filters import threshold_otsu
from matplotlib.pyplot import imshow, plot, figure
import numpy as np
# Load images
img_a = imread("671OL.jpg", as_gray=True)
img_b = imread("zpevD.jpg", as_gray=True)
# apply threshold
th_img_a = img_a > threshold_otsu(img_a)
th_img_b = img_b > threshold_otsu(img_b)
# measure connected component
img_lable = label(th_img_a)
r_props = regionprops(img_lable)
figure(figsize=(15,7))
rows, cols = img_b.shape
# calculate the registration (shift) of the two images
flow = phase_cross_correlation(th_img_a, th_img_b)
# stack the images and trace the segments that connect the points
d=10
# a vertical white bar between the two pictures
vbar=np.ones((rows,d))
xshift = cols+d
dy,dx = flow[0]
dx=dx + xshift
imshow(np.hstack([img_a, vbar, img_b]), cmap='gray')
for rp in r_props:
y0,x0 = rp.centroid
x1 = x0 + dx
y1 = y0 - dy
if y1<rows and x1 < 2*cols + d:
# filter out points that are not in img_b
plot([x0,x1],[y0,y1], '--', alpha=0.5)
推荐阅读
- javascript - 根据状态中的变量数显示输入字段
- c++ - 使用自定义比较器对地图进行排序时出错
- c++ - vxWorks 6.9 中的定时器
- node.js - 等待不适用于nodejs中的插入命令
- wordpress - SSL证书解释
- postgresql - postgreSQL中的批量返回语句?
- google-cloud-automl - 在 Google AutoML 视觉中,是否可以生成图像中重要区域的定位图(热图)?
- typescript - 将对象的 Observable 转换为 Observables 的对象
- c++ - strcmp - '[' 标记之前的预期主表达式
- python - 散景:如何为带有双索引 Pandas 分组数据框的条形图设置单索引标签图例?