久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 OpenCV 和 SIFT/SURF 校正掃描圖像以匹配原始圖

Deskewing scanned image to match original image using OpenCV and SIFT/SURF(使用 OpenCV 和 SIFT/SURF 校正掃描圖像以匹配原始圖像)
本文介紹了使用 OpenCV 和 SIFT/SURF 校正掃描圖像以匹配原始圖像的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)數(shù)字形式的原始頁(yè)面和同一頁(yè)面的多個(gè)掃描版本.我的目標(biāo)是對(duì)掃描的頁(yè)面進(jìn)行糾偏,使其盡可能與原始頁(yè)面匹配.我知道我可以使用

解決方案

原來(lái)我非常接近解決自己的問(wèn)題.這是我的代碼的工作版本:

將 numpy 導(dǎo)入為 np導(dǎo)入簡(jiǎn)歷2從 matplotlib 導(dǎo)入 pyplot 作為 plt導(dǎo)入數(shù)學(xué)def 糾偏():im_out = cv2.warpPerspective(skewed_image, np.linalg.inv(M), (orig_image.shape[1], orig_image.shape[0]))plt.imshow(im_out, '灰色')plt.show()orig_image = cv2.imread(r'image.png', 0)skewed_image = cv2.imread(r'imageSkewed.png', 0)沖浪 = cv2.xfeatures2d.SURF_create(400)kp1, des1 = surf.detectAndCompute(orig_image, None)kp2, des2 = surf.detectAndCompute(skewed_image, 無(wú))FLANN_INDEX_KDTREE = 0index_params = dict(算法=FLANN_INDEX_KDTREE,樹(shù)=5)search_params = dict(檢查=50)flann = cv2.FlannBasedMatcher(index_params, search_params)匹配 = flann.knnMatch(des1, des2, k=2)# 根據(jù)勞氏比率測(cè)試存儲(chǔ)所有好的匹配.好=[]對(duì)于 m,n 匹配:如果 m.distance <0.7 * n.距離:好.追加(米)MIN_MATCH_COUNT = 10如果 len(good) >MIN_MATCH_COUNT 個(gè):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, 掩碼 = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)# 詳見(jiàn) https://ch.mathworks.com/help/images/examples/find-image-rotation-and-scale-using-automated-feature-matching.htmlss = M[0, 1]sc = M[0, 0]scaleRecovered = math.sqrt(ss * ss + sc * sc)thetaRecovered = math.atan2(ss, sc) * 180/math.piprint("計(jì)算的比例差:%.2f
計(jì)算的旋轉(zhuǎn)差:%.2f" % (scaleRecovered, thetaRecovered))糾偏()別的:print("沒(méi)有找到足夠的匹配 - %d/%d" % (len(good), MIN_MATCH_COUNT))匹配掩碼 = 無(wú)

I have an original page in digital form and several scanned versions of the same page. My goal is to deskew the scanned pages such that they match the original page as much as possible. I know that I could use the Probabilistic Hough Transform as described here for fixing the rotation but the scanned papers also differ in size as some people scaled the page to a different paper format. I think that the findHomography() function in OpenCV in combination with the keypoints from SIFT/SURF are exactly what I need to solve this problem. However, I just can't get my deskew() function to work.

Most of my code stems from the following two sources: http://www.learnopencv.com/homography-examples-using-opencv-python-c/ and http://docs.opencv.org/3.1.0/d1/de0/tutorial_py_feature_homography.html.

import numpy as np
import cv2
from matplotlib import pyplot as plt


# 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("image.png", 0), None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)
img2 = cv2.resize(cv2.imread("imageSkewed.png", 0), None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)

surf = cv2.xfeatures2d.SURF_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()

解決方案

Turns out I was very close to solving my own problem. Here's the working version of my code:

import numpy as np
import cv2
from matplotlib import pyplot as plt
import math


def deskew():
    im_out = cv2.warpPerspective(skewed_image, np.linalg.inv(M), (orig_image.shape[1], orig_image.shape[0]))
    plt.imshow(im_out, 'gray')
    plt.show()

orig_image = cv2.imread(r'image.png', 0)
skewed_image = cv2.imread(r'imageSkewed.png', 0)

surf = cv2.xfeatures2d.SURF_create(400)
kp1, des1 = surf.detectAndCompute(orig_image, None)
kp2, des2 = surf.detectAndCompute(skewed_image, 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)

    # see https://ch.mathworks.com/help/images/examples/find-image-rotation-and-scale-using-automated-feature-matching.html for details
    ss = M[0, 1]
    sc = M[0, 0]
    scaleRecovered = math.sqrt(ss * ss + sc * sc)
    thetaRecovered = math.atan2(ss, sc) * 180 / math.pi
    print("Calculated scale difference: %.2f
Calculated rotation difference: %.2f" % (scaleRecovered, thetaRecovered))

    deskew()

else:
    print("Not  enough  matches are found   -   %d/%d" % (len(good), MIN_MATCH_COUNT))
    matchesMask = None

這篇關(guān)于使用 OpenCV 和 SIFT/SURF 校正掃描圖像以匹配原始圖像的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 在线中文字幕av | 亚洲欧美日本在线 | 日本三级韩国三级美三级91 | 91日韩在线 | 午夜小视频在线观看 | 国产精品一区二区三区免费 | 激情婷婷网 | 国产黄色精品 | 伊人色播 | 加勒比一区二区三区 | 91免费网站入口 | 日日爱影视 | 日本中文字幕在线播放 | 91久久精品日日躁夜夜躁欧美 | 日韩av中文字幕在线播放 | 日韩1区2区| 久久久影院 | 日韩网站在线观看 | 成人在线网址 | 一区二区影视 | 99在线观看视频 | www.日韩.com | 毛片视频免费 | 国产一区福利 | 国产在线一 | 男人午夜影院 | 日韩欧美国产高清91 | 欧美在线看片 | 国产美女视频网站 | 黑丝一区 | 涩涩999| 好色影院 | 亚洲免费二区 | 国内自拍一区 | 免费国产精品视频 | 毛片毛片毛片毛片毛片 | 亚洲一区二区久久 | 国产精品免费人成网站酒店 | 成人特级毛片 | 日韩毛片视频 | 精品国产91乱码一区二区三区 |