简体   繁体   中英

Detect/Segment hatch-filled rectangles OpenCV

左侧阴影填充矩形 - 右侧是左上角矩形检测输出的一个示例

The problem

I need to detect hatch-filled rectangles on a huge image. There are other lines and forms connected to the hatch-filled rectangle. My desired output would be like the black form on the right side of the image. All other forms (not hatch-filled rectangles) are ignored and not in the output.

What I tried:

Build a mask by using a Hough transformation and calculating the angles of the detected lines. Ignore everything that is not about 45°. Use dilation to fill the detected lines into a rectangle. Form a mask and apply it to the gray image. Here I was able to detect the lines for the upper left rectangle, but not for the one in the bottom (lines too short?). Furthermore, this did not work if other lines are attached to the rectangle (as can be seen in the upper left).

Any ideas on how to approach this problem the best way?

For what it is worth:

  • detect all lines slanted by 45°,

  • erase all them in the middle,

  • seed fill from a pixel in the middle.

This will fail for hatches with another angle, for legit 45° lines and for shapes that are not properly closed. Antialiasing along the edges will also cause issues. You may binarize first, but slanted/round edges will turn ugly.

在此处输入图像描述


Update:

You can improve the result by applying a morphological closing with a square element, which will respect the rectangular shapes and erase the thin lines. Then AND with the original.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM