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?
The ideal result would be something like this, which four points can be identified.
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'