opencv功能与空表格模板匹配

问题描述 投票:0回答:1

我一直试图将扫描的公式与其空模板相匹配。目标是旋转并缩放它以匹配模板。

Source fomular, to be rescaled and rotated Template Source (left), template (right) Matches Homography warp result Match (left), Homography warp (right) 模板不包含任何非常特定的徽标,固定十字或矩形框架,可以方便地帮助我进行特征或模式匹配。更糟糕的是,扫描的公式可以歪斜,改变并包含手写签名和邮票。

在测试ORB特征匹配失败后,我的方法是专注于公式(线和列)的形状。

我在这里提供的图片是通过在具有特定最小尺寸的段检测(LSD)之后重构线来获得的。源和模板的大部分内容都是文档布局本身。

在下面的脚本中(它应该与图片一起开箱即用),我尝试进行ORB功能匹配,但无法使其工作,因为它集中在边缘而不是文档布局上。

import cv2  # using opencv-python v3.4
import numpy as np
from imutils import resize


# alining image using ORB descriptors, then homography warp
def align_images(im1, im2,MAX_MATCHES=5000,GOOD_MATCH_PERCENT = 0.15):

    # Detect ORB features and compute descriptors.
    orb = cv2.ORB_create(MAX_MATCHES)
    keypoints1, descriptors1 = orb.detectAndCompute(im1, None)
    keypoints2, descriptors2 = orb.detectAndCompute(im2, None)

    # Match features.
    matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
    matches = matcher.match(descriptors1, descriptors2, None)

    # Sort matches by score
    matches.sort(key=lambda x: x.distance, reverse=False)

    # Remove not so good matches
    numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
    matches = matches[:numGoodMatches]

    # Draw top matches
    imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)

    # Extract location of good matches
    points1 = np.zeros((len(matches), 2), dtype=np.float32)
    points2 = np.zeros((len(matches), 2), dtype=np.float32)

    for i, match in enumerate(matches):
        points1[i, :] = keypoints1[match.queryIdx].pt
        points2[i, :] = keypoints2[match.trainIdx].pt

    # Find homography
    h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

    # Use homography
    if len(im2.shape) == 2:
        height, width = im2.shape
    else:
        height, width, channels = im2.shape
    im1Reg = cv2.warpPerspective(im1, h, (width, height))

    return im1Reg, h, imMatches

template_fn = './stack/template.jpg'
image_fn = './stack/image.jpg'

im = cv2.imread(image_fn, cv2.IMREAD_GRAYSCALE)
template = cv2.imread(template_fn, cv2.IMREAD_GRAYSCALE)

# aligh images
imReg, h, matches = align_images(template,im)

# display output
cv2.imshow('im',im)
cv2.imshow('template',template)
cv2.imshow('matches',matches)
cv2.imshow('result',imReg)
cv2.waitKey(0)
cv2.destroyAllWindows()

有没有办法让模式匹配算法在左边的图像(源)上工作? (另一个想法是只留下线交叉点)

或者,我一直在尝试对循环进行缩放和旋转不变模式匹配,同时保持最大相关性,但它太耗费资源并且不太可靠。

因此,我正在使用opencv寻找正确方向的提示。

python opencv computer-vision pattern-matching feature-detection
1个回答
0
投票

问题在于将图像缩小到真正重要的位置:布局。 此外,ORB不合适,因为它不像SIFT和AKAZE那样强大(旋转和大小不变)。

我进行如下:

  • 将图像转换为黑白图像
  • 使用线段检测和过滤线短于宽度的1/60
  • 从段重建图像(线宽不会产生很大影响)
  • (可选:调整图片大小以加快其余部分)
  • 在线重建上应用高斯变换,宽度的1/25
  • 使用SIFT(专利)或AKAZE(免费)算法检测和匹配功能
  • 找到单应性并扭曲源图片以匹配模板

Matches AKAZE AKAZE的比赛

Matches SIFT SIFT的比赛

我注意到:

  • 模板的布局必须匹配,否则它只会坚持它识别的内容
  • 线检测在分辨率更高的情况下更好,然后由于应用了高斯分布,因此可以进行缩小
  • SIFT产生更多功能,似乎比AKAZE更可靠
© www.soinside.com 2019 - 2024. All rights reserved.