1

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

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 但是由于文本之间的间距太少,检测到的垂直线也太少了,因此这也失败了

任何修改或其他方法都值得赞赏

4

1 回答 1

0

偏斜校正的工作代码

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')
于 2020-03-09T05:14:05.377 回答