0

I'm working on a project and I'm detecting every table in an image via OpenCV. I was able to detect, extract and crop the tables, so I already have the cropped images with all the tables.

Now my question is whether I'm able to calc the angle for every image (== table) and perform a correct rotation. The reason is that many of them are vertical or completely crooked. But I need them horizontal in order to perform OCR.

Is there a way to calc the correct angle and do the correct rotation? Does anyone has an idea or solution?

In my case:

  • I got tables which are horizontal and have an angle of < 1 (good)
  • I also have tables which are vertical but also have an angle of < 1 (not good)
  • I have vertical tables which have an angle of almost 90 but the rotation goes to the wrong direction
  • I have horizontal tables which have an angle of 90 and the code rotates the image

I checked this code (source: How to use the output of cv2.HoughLinesP() to rotate the original image?) but it produces the values I mentioned above:

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold 
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = 255 - gray
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Compute rotated bounding box
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]

if angle < -45:
    angle = -(90 + angle)
else:
    angle = -angle
print(angle)

# Rotate image to deskew
(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.imshow('thresh', thresh)
cv2.imshow('rotated', rotated)
cv2.waitKey()
3
  • 2
    welcome to SO - could you add an example of each image class? there are surely better and more specific strategies, but I believe a fairly classic way to do this is to use RANSAC to find a most straight line of pixels (which is ideally a border of the table) and then rotate it from that .. you may also find that trying the OCR twice (or four times) at the then-possible 90° choices and choosing the result one with the most dictionary matches will help orient it Commented Jan 6 at 22:46
  • This is also a great rotate and crop in OpenCV Answer to How to straighten a rotated rectangle area of an image using OpenCV in Python? Commented Jan 6 at 22:50
  • 1
    Yeah, you need to recognize the text to know if you have the table the right way around. You cannot guess that from the table outline alone. Commented Jan 7 at 3:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.