Skip to content
Advertisement

Detect rectangles in an image with Python

I have a large collection of binary images like these ones:

sample image 1 sample image 2 sample image 3 sample image 4

On each image I need to detect the white rectangle. The rectangles have different dimensions and orientations and sometimes they are interrupted by a black line (see image 2).

I think the problem is easy to solve if one could remove the noisy background. Thus, I tried first using OpenCV’s filter2D function:

JavaScript

But this doesn’t seem to have any effect, probably because I’m not dealing with a gray scale image.

Next I thought about detecting contours and filling all contours black which have a small size:

JavaScript

This helps a bit to remove the noise but it is far from perfect.

Does anybody have an idea how to improve my approach or is there a way to directly detect rectangles without removing the background noise?

Advertisement

Answer

Applying multi filtering and edge detection and thresholding will give you an acceptable results, you can improve the results using morphology or some math to determine the corners and the angle of your rectangle.

result

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement