执行图像配准时,图像之间的匹配不正确

2024-06-22 10:50:24 发布

您现在位置:Python中文网/ 问答频道 /正文

原始图像1

Original image 1

原始图像2

Original image 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)

显示我得到的匹配的图像

Image showing the matches I get


Tags: the图像importnonefortkinternpcv2
1条回答
网友
1楼 · 发布于 2024-06-22 10:50:24

(我可能错了,因为我没有处理过显微镜图像处理,而且必须存在解决该区域典型问题的通用方法,如果这不是一个玩具项目,你应该调查该区域)。

在我看来,你应该尝试另一个决定来解决你的问题,而不是使用任何类型的点特征为基础的图像描述符(球,冲浪等)。 首先,并不是所有这些都能提供亚像素精度,你可能需要在处理显微镜图像。但最主要的原因是描述词背后的数学。参考任何简历或论文。 这是link到ORB描述符的论文。请注意作者用于匹配检测点的图像。好的点是图像边缘和角落的一个点,所以它可以用来匹配尖锐和突出的形状物体。你知道吗

众所周知的例子:

Well-known example

匹配点位于字母(独特形状)和纹理图形上。 试着用这个工具检测纯绿色的教科书(封面上没有任何字母和任何东西),你会失败。

所以我认为你的图像不是可以这样处理的(因为物体的形状不锐利,没有纹理,彼此非常接近)。即使是人类的眼睛也很难匹配相似的圆圈(例如,在不太明显的情况下,例如稍微向左/向右移动一个视图)。你知道吗

但我一眼就注意到,你的形象可以用上面的圆圈来描述。Hough-based circle detection更容易理解(对于理解和计算来说)而且真正重要的是,它可以在这样的图像上提供几乎100%的准确率。你可以很容易地操作圆的数量,大小,位置等

不过,显微镜CV是一个独立的领域,有自己的常用工具可供使用,使用Hough或其他工具可能有很多优点和缺点。但乍一看,它似乎比点特征描述更准确。你知道吗

相关问题 更多 >