0

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)

Processed picture

  • 1
    minAreaRect creates a bounding box around the green contour, where the area of the bounding box is as small as possible. The box can have all orientations, but in your case, it would have about the same orientation as the blue box. So 0 could be true. – Epsi Apr 05 '22 at 14:08
  • Thank you, it makese sense now. Do you know how I could get the angle of the green box? Or maybe get the angles of all 4 lines (forming the green box) and use the average of them for deskewing? – Oana Alexandra Apr 05 '22 at 14:13
  • You could look at these examples to extract the corners of the receipt: https://stackoverflow.com/questions/7263621/how-to-find-corners-on-a-image-using-opencv and https://docs.opencv.org/4.x/dc/d0d/tutorial_py_features_harris.html. With the coordinates of the corner points you can calculate the angle of the bill – Epsi Apr 05 '22 at 14:27
  • @OanaAlexandra you can first get the extreme corner points of the contour (in green), you will get 4 points. Draw 4 lines connecting each of them. Find the angle of each of those lines with respect to X-axis – Jeru Luke Apr 05 '22 at 15:16

0 Answers0