简体   繁体   English

OpenCV Python特征检测示例扩展

[英]OpenCV Python Feature detection examples extension

I'm chasing a little assistance with an idea I'm playing with. 我正在追逐一个我正在玩的想法的帮助。 I want to take the features located in an image with code similar to the example on 我想采用位于图像中的功能,其代码类似于示例

See sample image at bottom of page here Last section/Example is the one I'm talking about 请参阅页面底部的示例图像。最后一节/示例是我正在讨论的内容

in particular for my issue I wanted to use the matches indicated in the image to find the target in the scene image like illustrated with a seemingly simple addition. 特别是对于我的问题,我想使用图像中指示的匹配来找到场景图像中的目标,如图所示,看似简单的添加。 I want to draw a bounding box around the target when located in the scene frame 我想在场景框中找到目标周围的边界框

Example of output I'm after 我之后的输出示例

Rather than just putting a bounding box around the features, I would rather have a list of the four contour points that represent the transformed target on the scene frame if that makes sense. 如果有意义的话,我宁愿有一个四个轮廓点列表来代表场景框架上的变换目标,而不仅仅是在特征周围放置一个边界框。

Big picture, I want to take the subsection of the scene image containing my target and crop it out of the scene image, mask the non-target areas out of the image remaining and then use this as my source for a further process. 大图,我想拍摄包含我的目标的场景图像的子部分并将其裁剪出场景图像,从剩余的图像中掩盖非目标区域,然后将其用作我进一步处理的源。

At this point I've managed to do all it need to with a hard coded set of points to represent the corners of the target image as rotated and transformed in the scene image so everything works I just need an example of how to determine the x,y co-ords of each corner of the target in that scene 在这一点上,我已经成功地用一组硬编码的点来表示目标图像的角落在场景图像中旋转和变换所以一切正常我只需要一个如何确定x的例子,y该场景中目标每个角落的合作

I didn't want to post the code as its a bit clunky and its the concept I'm after, not a complete 'do it for me please' fix 我不想发布代码,因为它有点笨重,它是我追求的概念,而不是一个完整的'为我做它请'修复

Any advice much appreciated, If you could show me using the example code attached how to do this I'd be very grateful, Cheers. 任何建议都非常感激,如果你能告诉我使用附带的示例代码如何做到这一点,我将非常感激,干杯。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img1 = cv2.imread('box.png',0)          # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage

# Initiate SIFT detector
sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params,search_params)

matches = flann.knnMatch(des1,des2,k=2)

# Need to draw only good matches, so create a mask
matchesMask = [[0,0] for i in xrange(len(matches))]

# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
    if m.distance < 0.7*n.distance:
        matchesMask[i]=[1,0]

draw_params = dict(matchColor = (0,255,0),
                   singlePointColor = (255,0,0),
                   matchesMask = matchesMask,
                   flags = 0)

img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

plt.imshow(img3,),plt.show()

You need to find the prescriptive transform between the two images. 您需要找到两个图像之间的规定性转换。

Create a set of corresponding coordinates according to the matched features. 根据匹配的要素创建一组对应的坐标。

For example you find that the feature FtI1 in image 1 corresponds to FtJ1 in image 2 so you know that coordinate of FtI1 (xi,yi) corresponds to the coordinate of FtJ1 (xj,yj) and you have this for all the corresponding features. 例如,您发现图像1中的特征FtI1对应于图像2中的FtJ1,因此您知道FtI1(xi,yi)的坐标对应于FtJ1(xj,yj)的坐标,并且您对所有相应的特征都具有此坐标。 After you have a list of corresponding coordinates between the two images you can calculate the prescriptive transform using opecv getPerspectiveTransform. 在两个图像之间有相应坐标列表后,可以使用opecv getPerspectiveTransform计算规定变换。

Finally use the transformation you found on the 4 coordinates of the enclosing shape in the first image to get the coordinates of the enclosing shape in the second image. 最后使用您在第一个图像中的封闭形状的4个坐标上找到的变换,以获得第二个图像中封闭形状的坐标。 The opencv function for that is warpPerspective. opencv函数就是warpPerspective。

An example of how to do that in opecv is in: http://docs.opencv.org/3.1.0/da/d6e/tutorial_py_geometric_transformations.html 在opecv中如何做到这一点的一个例子是: http ://docs.opencv.org/3.1.0/da/d6e/tutorial_py_geometric_transformations.html

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

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