【发布时间】:2019-10-22 09:04:45
【问题描述】:
原图1
原图2
我正在尝试匹配两个显微镜图像(请参阅附件)。但是,匹配很糟糕,并且单应矩阵会产生不可接受的结果。有没有办法改进这个注册?
import cv2 # Imports the Open CV2 module for image manipulation.
import numpy as np # Imports the numpy module for numerical manipulation.
from tkinter import Tk # Imports tkinter for the creation of a graphic user interface.
from tkinter.filedialog import askopenfilename # Imports the filedialog window from tkinter
Tk().withdraw()
filename1 = askopenfilename(title='Select the skewed file')
Tk().withdraw()
filename2 = askopenfilename(title='Select the original file')
img1 = cv2.imread(filename1)
img2 = cv2.imread(filename2)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create(nfeatures=10000)
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(des1, des2, None)
matches = sorted(matches, key = lambda x:x.distance)
points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)
for i, match in enumerate(matches):
points1[i, :] = kp1[match.queryIdx].pt
points2[i, :] = kp2[match.trainIdx].pt
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
height, width = img2.shape
im1Reg = cv2.warpPerspective(img1, h, (width, height))
img3 = cv2.drawKeypoints(img1, kp1, None, flags=0)
img4 = cv2.drawKeypoints(img2, kp2, None, flags=0)
img5 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None)
img = np.dstack((im1Reg, img2, im1Reg))
cv2.imshow("Shifted", img3)
cv2.imshow("Original", img4)
cv2.imshow("Matches", img5)
cv2.imshow("Registered", im1Reg)
cv2.imshow("Merged", img)
cv2.waitKey(0)
显示我得到的匹配的图像
【问题讨论】:
-
您能否提供原始图像(不匹配)以便用户尝试他们的算法?也许在特征匹配之前尝试一些图像处理,以便您获得在辐射意义上更相似的图像。
-
我认为你的问题是你的图像质量很差,特征分散,模糊不清。或许去除噪点并增加对比度或二值化可能会有所帮助。
-
谢谢 Grillteller 和 fmw42。我刚刚上传了原始图片。
标签: python opencv image-registration