2

I want a way to automatically detect and correct skew of a image of a receipt, I tried to find variance between the rows for various angles of rotation and choose the angle which has the the maximum variance. To calculate variance I did the following:

1.For each row I calculated the sum of the pixels values and stored it in a list.

2.Found the the variance of the list using 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)

However the variance for a skewed image is coming to be more than the properly aligned image,is there any way to correct this

  • variance for the horizontal text aligned image(required):122449908.009789

  • variance for the vertical text aligned image :1840071444.404522

Vertical

Horizontal

I have tried using HoughLines However since the spacing between the text is too less vertical lines are detected,hence this also fails

Any modifications or other approaches are appreciated

Yash Gupta
  • 187
  • 2
  • 11

1 Answers1

0

Working code for skew correction

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)

convert to binary

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')
Community
  • 1
  • 1
Sachin Rajput
  • 238
  • 7
  • 26