OpenCV:如何在顶部和底部配置中绘制匹配项?

4

我正在尝试使用OpenCV中的drawmatches函数。

它以左右格式放置图像。我希望将图像放置在上下格式中,以便更清晰地绘制匹配项。

在OpenCV中是否有一种方法可以实现这一点?还是说我必须编写一个新的函数?

2个回答

4
我很抱歉,您需要编写自己的函数。 我认为它不应该太复杂。
作为起点,只需查看https://github.com/Itseez/opencv/blob/2.4/modules/features2d/src/draw.cpp,其中我们有函数_prepareImgAndDrawKeypoints
static void _prepareImgAndDrawKeypoints( const Mat& img1, const vector<KeyPoint>& keypoints1,
                                         const Mat& img2, const vector<KeyPoint>& keypoints2,
                                         Mat& outImg, Mat& outImg1, Mat& outImg2,
                                         const Scalar& singlePointColor, int flags )
{
    Size size( img1.cols + img2.cols, MAX(img1.rows, img2.rows) );

例如,大小应更改为
Size size( MAX(img1.cols + img2.cols), img1.rows + img2.rows );

然后您可以继续研究该函数(以及其他函数),完成您的任务。也许您还可以通过您的新功能为OpenCV做出贡献。


我也是这么想的..谢谢..!! - Armin Mustafa

1
作为一个快速的解决方法,你可以将两个图像都预先旋转90度,检测特征,绘制匹配,然后撤销旋转。
例如,这些图片(来自这里这里):

im1 im2

Result:

enter image description here

Python代码:(调整大小部分仅用于将图像适应屏幕大小)
import cv2

im1 = cv2.imread('test1.jpg')
im2 = cv2.imread('test2.jpg')

# resize
scale=0.5
n1,m1 = int(im1.shape[0]*scale), int(im1.shape[1]*scale)
n2,m2 = int(im2.shape[0]*scale), int(im2.shape[1]*scale)
im1 = cv2.resize(im1, (m1,n1))
im2 = cv2.resize(im2, (m2,n2))

rotate=True
if rotate:
    im1 = cv2.rotate(im1, cv2.ROTATE_90_COUNTERCLOCKWISE)
    im2 = cv2.rotate(im2, cv2.ROTATE_90_COUNTERCLOCKWISE)
    
# gray versions:
im1g = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
im2g = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)

# sift detections:
sift = cv2.SIFT_create()
kp1, ds1 = sift.detectAndCompute(im1g,None)
kp2, ds2 = sift.detectAndCompute(im2g,None)

# matching
matcher = cv2.DescriptorMatcher.create('BruteForce')
matches = matcher.knnMatch(ds1,ds2, 2)
# Filter matches using the Lowe's ratio test
ratio_thresh = 0.7
good_matches = []
for i, (m,n) in enumerate(matches):
    if m.distance < ratio_thresh * n.distance:
        good_matches.append(m)
        
# draw matches:
im_matches = cv2.drawMatches(im1, kp1, im2, kp2, good_matches,None, 
                             flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
# undo pre-rotation
if rotate:
    im_matches = cv2.rotate(im_matches, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('matches', im_matches)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接