【发布时间】:2013-12-19 22:15:15
【问题描述】:
我正在尝试在 OpenCV 中使用 drawmatches 函数。
它将图像以left and right 格式。我希望将图像放入top-down 格式,然后绘制匹配以更清晰。
有没有一种方法可以在 OpenCV 中完成?或者,我将不得不编写一个新函数?
【问题讨论】:
标签: opencv image-processing computer-vision feature-detection
我正在尝试在 OpenCV 中使用 drawmatches 函数。
它将图像以left and right 格式。我希望将图像放入top-down 格式,然后绘制匹配以更清晰。
有没有一种方法可以在 OpenCV 中完成?或者,我将不得不编写一个新函数?
【问题讨论】:
标签: opencv image-processing computer-vision feature-detection
作为一种快速解决方法,您可以将两个图像预旋转 90 度,检测特征,绘制匹配项,然后撤消旋转,
结果:
Python代码 :(resize部分只是为了让图片适应屏幕大小)
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)
【讨论】:
恐怕您必须编写自己的函数。我觉得应该不会太复杂。
首先看看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 做出贡献。
【讨论】: