Deshwal
Deshwal

Reputation: 4152

Unable to Deskew an image using OpenCV in Python

I followed this tutorial by Adrian Rosebrock on Deskewing the images and created a function like:

def deskew(image):
    
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.bitwise_not(gray) # invert colors white numbers black background

    thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    coords = np.column_stack(np.where(thresh > 0))
    angle = cv2.minAreaRect(coords)[-1]
   
    if angle < -45:
        angle = -(90 + angle)
    
    else:
        angle = -angle


    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    rotated = cv2.warpAffine(image, M, (w, h),
        flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)


    cv2.putText(rotated, "Angle: {:.2f} degrees".format(angle),
        (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

    return image,"{:.3f}".format(angle)

    
image = cv2.imread(img_path)
skewed = imutils.rotate(image,14.74)
corrected,angle = deskew(skewed)

plt.imshow(corrected)
plt.title(f"Corrected by:{angle}")
plt.show()

and got angle=0.0

Image is this:

enter image description here

Actually the original image is already skewed. I have all my pictures like this (little or no spacing in border and text : cropped pictures of questions from text book). I want to perform OCR on this. How can I de-skew the pictures?

Upvotes: 1

Views: 9933

Answers (1)

Tarun Chakitha
Tarun Chakitha

Reputation: 416

Here is my approach to correct the 2d-skew in images. It is simple and naive but works fine for almost all images containing a considerable amount of text. It does not correct the 3d distortion. It works best only with images containing only the 2d skew. And might not work for images containing graphics(like your chemistry molecular structures), but give it a try because your images are so similar to mine.

The correct skew function in this Script is my approach.

If you are still not satisfied I have a better idea.

Deskew library is the best for correcting the 2d skew in images.

Hope this solves your problem.

Upvotes: 3

Related Questions