首页 > 解决方案 > 如何调整图像大小?(cv2.drawMatchesKnn) 在 colab

问题描述

img1=cv2.imread('/content/datamy/notre.jpg',0)
img2=cv2.imread('/content/datamy/notre2.jpg',0)

surf = cv2.xfeatures2d.SURF_create(500)
kp1, des1 = surf.detectAndCompute(img1,None)
kp2, des2 = surf.detectAndCompute(img2,None)

bf = cv2.BFMatcher()

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

good = []
for m,n in matches:
    if m.distance < 0.5*n.distance:
        good.append([m])
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)

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

这是代码的一部分。结果图像是在此处输入图像描述

我看不到匹配的线,因为它太小了。有没有办法打印更大的尺寸?(就像原始尺寸一样。)

标签: pythonopencvcomputer-visionfeature-detectiongoogle-colaboratory

解决方案


你试过了cv2.resize吗?

img1size 大于img2,所以你可以得到img1的高度和宽度:

(h, w) = img1.shape[:2]

并调整大小img2

img2 = cv2.resize(img2, (w, h))

结果:


在此处输入图像描述

代码:


import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('notre.jpg', 0)
img2 = cv2.imread('notre2.jpg', 0)

(h, w) = img1.shape[:2]
img2 = cv2.resize(img2, (w, h))

surf = cv2.xfeatures2d.SURF_create(500)
kp1, des1 = surf.detectAndCompute(img1, None)
kp2, des2 = surf.detectAndCompute(img2, None)

bf = cv2.BFMatcher()

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

good = []
for m, n in matches:
    if m.distance < 0.5*n.distance:
        good.append([m])
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=2)

plt.imshow(img3)
plt.show()

推荐阅读