简体   繁体   English

Python中的三维规范化互相关

[英]3D normalised cross-correlation in Python

I'm currently doing 2D template matching using OpenCV's MatchTemplate function called from Python. 我目前正在使用从Python调用的OpenCV的MatchTemplate函数进行2D模板匹配。 I'm looking to extend my code into 3D but can't find any existing 3D cross-correlation programs. 我希望将我的代码扩展到3D,但找不到任何现有的3D互相关程序。 Can anyone help out? 任何人都可以帮忙吗?

Do you mean that you are currently looking for a known object somewhere in an image, and you are currently only able to handle that object being affine transformed (moved around on a 2D plane), but you want to be able to handle it being perspective transformed? 你的意思是说你正在寻找图像中某处的已知对象,而你目前只能处理被仿射变换的对象(在2D平面上移动),但是你希望能够处理它是透视转化?

You could try using a SURF or SIFT algorithm to find features in your reference and unknown images: 您可以尝试使用SURF或SIFT算法查找参考图像和未知图像中的特征:

def GetSurfPoints(image, mask)
    surfDetector = cv2.FeatureDetector_create("SURF")
    surfExtractor = cv2.DescriptorExtractor_create("SURF")
    keyPoints = surfDetector.detect(image, mask)
    keyPoints, descriptions = surfExtractor.compute(image, keyPoints)
    return keyPoints, descriptions

Then use FLANN to find matching points (this is from one of the cv2 samples): 然后使用FLANN查找匹配点(这是来自其中一个cv2样本):

def MatchFlann(desc1, desc2, r_threshold = 0.6):
    FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing
    flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 4)
    flann = cv2.flann_Index(desc2, flann_params)
    idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
    mask = dist[:,0] / dist[:,1] < r_threshold
    idx1 = numpy.arange(len(desc1))
    matches = numpy.int32( zip(idx1, idx2[:,0]) )
    return matches[mask]

Now if you want to, you could use FindHomography to find a transformation that aligns the two images: 现在,如果您愿意,可以使用FindHomography查找对齐两个图像的转换:

referencePoints = numpy.array([keyPoints[match[0]].pt for match in matches])
newPoints = numpy.array([keyPoints[match[1]].pt for match in matches])
transformMatrix, mask = cv2.findHomography(newPoints, referencePoints, method = cv2.cv.CV_LMEDS)

You could then use WarpPerspective and that matrix to align the images. 然后,您可以使用WarpPerspective和该矩阵来对齐图像。 Or you could do something else with the set of matched points found earlier. 或者您可以使用之前找到的匹配点集来执行其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM