【问题标题】:How Can i resize the image? (cv2.drawMatchesKnn) in colab如何调整图像大小? (cv2.drawMatchesKnn) 在 colab
【发布时间】:2021-02-26 02:27:20
【问题描述】:
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()

这是代码的一部分。 结果图片为

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

【问题讨论】:

    标签: python opencv computer-vision feature-detection google-colaboratory


    【解决方案1】:

    你试过cv2.resize吗?

    img1 的大小大于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()
    

    【讨论】:

    • 我知道怎么做。 '''fig, axs=plt.subplots(1,1,figsize=(18,12)) axs.imshow(img3,cmap='gray')''' 就是这段代码。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2020-10-02
    • 1970-01-01
    相关资源
    最近更新 更多