利用surf特征矫正倾斜图像

# -*- encoding=utf-8 -*-
import cv2
import os
import numpy as np

MIN_MATCH_COUNT = 5
FLANN_INDEX_KDTREE = 0
detector = cv2.xfeatures2d.SURF_create()

img1 = cv2.imread("1.jpg") #原图图片
demopoint, demoscript = detector.detectAndCompute(img1, None,useProvidedKeypoints=False)


img = cv2.imread('22.jpg') #待矫正图片
imgpoint, imgscript = detector.detectAndCompute(img, None,useProvidedKeypoints=False)
index_params = dict(algorithm=FLANN_INDEX_KDTREE, tree=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(demoscript, imgscript, k=2)
good = []
for m, n in matches:
    if m.distance < 0.7 * n.distance:
       good.append(m)

if len(good) > MIN_MATCH_COUNT:
    src_pts = np.float32([demopoint[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
    dst_pts = np.float32([imgpoint[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

    H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
       
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, H)

dst1 = np.float32([dst[0], dst[3], dst[1], dst[2]])
pts1 = np.float32([[0, 0], [w - 1, 0], [0, h - 1], [w - 1, h - 1]])

M = cv2.getPerspectiveTransform(dst1, pts1) #根据原图矫正后的图片

image = cv2.warpPerspective(img, M, (w, h))

cv2.imshow('', img)
cv2.waitKey()
cv2.destroyAllWindows()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值