I am working on an OCR project using OpenCV & Tesseract. I have successfully identified the shape of the receipt and now I am trying to extract the angle of the shape (drawn with green below) to deskew the image.
To detect the contour I used more RETR_EXTERNAL method, as I found it produces the best results for my dataset (the contours of background noise is minimal)
code to extract all contours
image2 = cv2.imread('./Dataset - Receipt Images/1036-receipt.jpg')
img_gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
ret, thresh2 = cv2.threshold(img_gray2, 150, 255, cv2.THRESH_BINARY)
contours4, hierarchy4 = cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
image_copy5 = image2.copy()
cv2.drawContours(image_copy5, contours4, -1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow('EXTERNAL', image_copy5)
To get the largest contour (blue box) I applied the following:
def drawMaxAreaContour(contours, image):
if len(contours) != 0:
c = max(contours, key=cv2.contourArea)
cv2.drawContours(image, c, 1, (0, 0, 255), 5)
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (250, 0, 0), 2)
To get the angle of the shape, i tried: cv2.minAreaRect(c)
And the results is 0 - the angle of the green box is 0, which is obviously not true.
So, specifically, my question: 2. OR how can I get the angle of that shape? (assuming it would be applied on the shape of a receipt, not a rectangular shape, like the blue one in my case)