Opencv: using paths and Hough transform when detecting a rectangle

I'm trying to detect white rectangles in an image in shades of gray, using different approaches: defining a path and Hough transform. Unfortunately, there are some limitations to the image I process, i.e.

  • There are many functions in an image, and a rectangle is not the only functions.
  • The rectangle can be combined with other functions (for example, one of the edges of the rectangle can be overlapped by a long straight line)
  • The rectangle may contain some other functions (for example, a letter, numbers, or some logo inside the rectangle).
  • Some functions look like a rectangle (for example, the β€œD” symbol looks like a rectangle with a small arc in the upper right and lower right corner, another example is a trapezoid instead of a parallelogram)
  • The rectangle can be rotated from 0 to 15 degrees both clockwise and counterclockwise
  • It is possible that the lines are divided into several lines in different lighting conditions (for example, 1 pixel gap), so the minimum line length for filtering lines should be small (for example, in the Hough transform).
  • When the minimum line length is set to a small value, most often you need to see repeating lines for the same line in different orientations (i.e. you need to combine several lines)

For the contonours approach, the contours of some images are broken. In addition, the image may contain functions such as a rectangle (for example, the symbol "D"). I am not sure if this is a good approach.

I have seen many articles / forums suggesting using the Hough transform to detect a rectangle similar to the following post. Unfortunately, I have to set a small value for the minimum line length and see duplicate lines. I have no idea how to deal with the points mentioned above (for example, combine all repeating lines and select only one line for each edge, how to distinguish functions with most of them - these are lines, but with small arcs such as "D", and how isolate a square with one edge combined with a long line, etc.).

Hough vs Contour transform for perspective rectangle recognition

Any suggestions are welcome!

EDIT: add some pictures

Character d

D symbol

Rect edge merged with long straight line

Rectangle with logo and edges combined with a long straight line

enter image description here

Trapezoid (with a shadow on the upper forming trapezoid below)

+5
source share
1 answer

I would suggest that you try to use a binary threshold (adaptive or otherwise) on each image, this will give some clear lines for your edge detection. You can also blur / expand images to remove noise (for example, thin lines in the second image)

Then use the path definition and count the paths by finding the largest object in the image with four sides (this will probably be your object).

Make a copy of the image before you use binary / erode, so that when you have an area of ​​interest from edge detection, you can crop a copy of the image in that area.

Sorry, the links to the links are written in python, but I'm sure that once you get the idea, porting it to C ++ will be easy.

Hope this helps.

EDIT

Just tried the method described above, generating each image, detecting the path, and then drawing a bounding box around the largest set of paths.

The following are the results:

enter image description here

A bounding box around a large set of contours

enter image description here

The same thing drawn on top of the original images

enter image description here

+1
source

All Articles