python - 如何调整图像大小?(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()
我看不到匹配的线,因为它太小了。有没有办法打印更大的尺寸?(就像原始尺寸一样。)
解决方案
你试过了cv2.resize
吗?
img1
size 大于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()
推荐阅读
- python - 使用 pandas DataFrame 从 excel 列创建列表
- r - 从两个数据表中创建重复值的数据集并合并信息?
- c# - SQL Server Reporting Services 无法设置数据源连接字符串
- vue.js - 如何渲染另一个组件的作用域插槽?
- if-statement - 缩短函数(switch、if...else if、第三语法)
- python - 如何在不使用 numpy 的情况下展平 python 中的浮点数列表?
- typescript - FluentValidation.ts 多个验证器和消息
- node.js - 显示具有多个角色的用户 discord.js
- python - 如何使用google api在python中注册?
- elixir - 为什么 Phoenix LiveView 上的表单中的 file_input 不返回 %Plug.Upload{}?