import numpy as np
import cv2
from matplotlib import pyplot as plt
#https://www.javaroad.cn/questions/347518#toolbar-title
# FIXME: doesn't work
def deskew():
im_out = cv2.warpPerspective(img1, M, (img2.shape[1], img2.shape[0]))
plt.imshow(im_out, 'gray')
plt.show()
# resizing images to improve speed
factor = 0.4
img1 = cv2.resize(cv2.imread("./img/zheng2.png", 0), None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)
img2 = cv2.resize(cv2.imread("./img/xie2.png", 0), None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)
#有专利,SURF_create,SIFT_create可以直接跑
'''
1. 卸载已有安装opencv-python:
pip uninstall opencv-python
2. 安装opencv-contrib-python 3.2版本以下:
pip install opencv-contrib-python==3.4.2
也可以不降低版本号,进行编译,详细流程见链接
https://blog.csdn.net/m0_50736744/article/details/129351648
'''
surf = cv2.xfeatures2d.SIFT_create()
kp1, des1 = surf.detectAndCompute(img1, None)
kp2, des2 = surf.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# store all the good matches as per Lowe's ratio test.
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
MIN_MATCH_COUNT = 10
if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good
]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good
]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h, w = img1.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
deskew()
img2 = cv2.polylines(img2, [np.int32(dst)], True, 255, 3, cv2.LINE_AA)
else:
print("Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT))
matchesMask = None
# show matching keypoints
draw_params = dict(matchColor=(0, 255, 0), # draw matches in green color
singlePointColor=None,
matchesMask=matchesMask, # draw only inliers
flags=2)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, good, None, **draw_params)
plt.imshow(img3, 'gray')
plt.show()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
指针式仪表倾斜校正opencv算法python代码及仪表图像(包含倾斜的和模板图像) opencv 里面的sift算法,如果想改成SURF算法直将“SIFT_create”修改成“SURF_create”即可 #SURF_create受专利保护,直接运行报错,SIFT_create可以直接跑 下面提供了两种使用SURF_create的方法 1. 卸载已有安装opencv-python: pip uninstall opencv-python 2. 安装opencv-contrib-python 3.2版本以下: pip install opencv-contrib-python==3.4.2 也可以不降低版本号,进行编译,详细流程见链接 https://blog.csdn.net/m0_50736744/article/details/129351648
资源推荐
资源详情
资源评论
收起资源包目录
倾斜矫正代码python.zip (11个子文件)
img
zheng.png 66KB
xie1.png 374KB
zheng2.png 544KB
bg1.jpg 167KB
zheng1.png 319KB
xie2.png 452KB
bg3.jpg 67KB
people.jpg 74KB
xie.png 265KB
cat.jpg 518KB
SIFT.py 3KB
共 11 条
- 1
资源评论
大胡子大叔
- 粉丝: 7462
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功