2016-11-14 146 views
1

我有一個數字形式的原始頁面和同一頁面的多個掃描版本。我的目標是歪斜掃描的頁面,以便儘可能匹配原始頁面。我知道我可以使用here中描述的概率霍夫變換來固定旋轉,但掃描後的紙張尺寸也有所不同,因爲有些人將頁面縮放爲不同的紙張格式。我認爲OpenCV中的findHomography()函數結合SIFT/SURF的關鍵點正是我需要解決這個問題的。但是,我無法讓我的deskew()函數工作。使用OpenCV和SIFT/SURF去偏移掃描圖像以匹配原始圖像

我的代碼大部分源於以下兩個來源: http://www.learnopencv.com/homography-examples-using-opencv-python-c/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() 

Original ImageSkewed Image

+1

我做類似[這裏](東西https://stackoverflow.com/questions/32435488/align-x-ray-images-find-rotation-rotate-and -crop/32441230#32441230)這可能是有幫助的。 –

+0

@MartinEvans謝謝,這很相似,但我需要的是儘可能地將偏斜圖像與原始圖像對齊。我剛剛發現這個[Mathlab教程](https://ch.mathworks.com/help/vision/examples/find-image-rotation-and-scale-using-automated-feature-matching.html?requestedDomain=www.mathworks .com)完全解決了我的問題,但不幸的是我沒有得到第5步。你知道如何調整我的示例代碼以使其工作嗎? –

回答

4

原來我是非常接近解決我自己的問題。 這裏是我的代碼的工作版本:

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\nCalculated rotation difference: %.2f" % (scaleRecovered, thetaRecovered)) 

    deskew() 

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