【问题标题】:Automatic Skew correction using opencv使用 opencv 自动校正歪斜
【发布时间】:2019-06-19 00:32:05
【问题描述】:

我想要一种自动检测和纠正收据图像歪斜的方法, 我试图找到不同旋转角度的行之间的差异,并选择具有最大差异的角度。 为了计算方差,我做了以下操作:

1.对于每一行,我计算了像素值的总和并将其存储在一个列表中。

2.使用np.var(list)找到列表的方差

    src = cv.imread(f_name, cv.IMREAD_GRAYSCALE)
    blurred=median = cv.medianBlur(src,9)
    ret,thresh2 = cv.threshold(src,127,255,cv.THRESH_BINARY_INV)
    height, width = thresh2.shape[:2]
    print(height,width)
    res=[-1,0]
    for angle in range(0,100,10):

        rotated_temp=deskew(thresh2,angle)
        cv.imshow('rotated_temp',rotated_temp)
        cv.waitKey(0)
        height,width=rotated_temp.shape[:2]
        li=[]
        for i in range(height):
            sum=0
            for j in range(width):
                sum+=rotated_temp[i][j]
            li.append(sum)
        curr_variance=np.var(li)
        print(curr_variance,angle)
        if(curr_variance>res[0]):
            res[0]=curr_variance
            res[1]=angle


    print(res)
    final_rot=deskew(src,res[1])
    cv.imshow('final_rot',final_rot)
    cv.waitKey(0)

但是,倾斜图像的方差会超过正确对齐的图像,有什么办法可以纠正这个问题

  • 横向文本对齐图像的方差(必需):122449908.009789

  • 垂直文本对齐图像的方差:1840071444.404522

我尝试过使用 HoughLines 但是由于文本之间的间距太少,检测到垂直线,因此这也失败了

感谢任何修改或其他方法

【问题讨论】:

  • 尝试扩大图像然后侵蚀它。这应该会生成大斑点,您可以检测到最大长度

标签: python image-processing opencv3.0 variance houghlines


【解决方案1】:

歪斜校正的工作代码

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image as im
from scipy.ndimage import interpolation as inter

input_file = r'E:\flaskV8\test1.jpg'

img = im.open(input_file)

转换成二进制

wd, ht = img.size
pix = np.array(img.convert('1').getdata(), np.uint8)
bin_img = 1 - (pix.reshape((ht, wd)) / 255.0)
plt.imshow(bin_img, cmap='gray')
plt.savefig(r'E:\flaskV8\binary.png')
def find_score(arr, angle):
    data = inter.rotate(arr, angle, reshape=False, order=0)
    hist = np.sum(data, axis=1)
    score = np.sum((hist[1:] - hist[:-1]) ** 2)
    return hist, score
delta = 1
limit = 5
angles = np.arange(-limit, limit+delta, delta)
scores = []
for angle in angles:
    hist, score = find_score(bin_img, angle)
    scores.append(score)
best_score = max(scores)
    best_angle = angles[scores.index(best_score)]
    print('Best angle: {}'.format(best_angle))
    data = inter.rotate(bin_img, best_angle, reshape=False, order=0)
    img = im.fromarray((255 * data).astype("uint8")).convert("RGB")
    img.save(r'E:\flaskV8\skew_corrected.png')

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 2014-04-26
    • 2020-01-17
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    相关资源
    最近更新 更多