3

My goal is to identify 4 intersected points and do perspective transform.

Given a list of computed rectangles [x, y, w, h] (derived from contours), how to find 4 best lines of a paper.

This thread uses minAreaRect to find the area of non-white pixels but in my case, the environment is very noisy and it is not possible to eliminate all white points even with extensive image processing algorithms applied.

Here is my processing pipeline

  • Grayscale and binarize
  • Apply morphological operations to remove unlikely characters
  • Find contours with a set of rules (experimented values)

I am now thinking a way to get 4 intersected points by first finding 4 lines (left, top, right and bottom) and then do deskew.

Is this something possible?

Link to original Image

Contours

The ideal result would be something like this, which four points can be identified.

enter image description here

Update 1

Attempting to convert a list of rectangles to points. I can only work with rectangles at this stage because previous operations no longer return contours.

def rectsToPoints(rects):

    points = []

    for x1, y1, x2, y2 in rects:
        x, y, w, h = x1, y1, x2-x1, y2-y1

        points.append([
            (x, y),
            (x+w, y),
            (x, y+h),
            (x+w, y+h)
        ])

    return np.array(points, dtype=np.int32)

points = rectsToPoints(rects)
minRect = cv2.minAreaRect(points)

Type Error

cv2.error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/convhull.cpp:137: error: (-215:Assertion failed) total >= 0 && (depth == CV_32F || depth == CV_32S) in function 'convexHull'
Rex Low
  • 2,069
  • 2
  • 18
  • 47
  • 1
    See [this answer](https://stackoverflow.com/a/50914972/2286337). – zindarod Feb 08 '19 at 10:53
  • 1
    Put the four vertices of each rectangle in a list of points, and use `minAreaRect` – Miki Feb 08 '19 at 12:32
  • @Miki Let me try, is there an order I need to follow for the 4 points? – Rex Low Feb 08 '19 at 13:50
  • @zindarod That answer you shared is nice! It uses the 4 largest contours as reference point right? But in my example I have no way to extract that four relevant contour – Rex Low Feb 08 '19 at 14:19
  • @RexLow no need to order the points – Miki Feb 08 '19 at 14:23
  • @Miki I've updated my post with a function I made to convert rects to points. However, it seems like I am getting a type error. I can only use rects at the moment because previous operations no longer return contours. – Rex Low Feb 08 '19 at 14:28

0 Answers0