Detect a specific rectangle in an image
17 views (last 30 days)
Show older comments
Hello,
I have a binary image size (215 lines X 625 columns) and I want to check if there is rectangles having the size (each one) 80 lines X 120 columns (full of '1' ) in the image and if yes , get the 4 corners of each rectangle.
Remark: Many or none rectangles may exists.
Thanks
4 Comments
Image Analyst
on 18 Jul 2022
OK admit it - you didn't read the tutorial did you? I don't see any image attached.
And a labeled region will not have more more 0 than 1. In fact it won't have any zeros at all. One region will just have one integer value in the entire region.
Answers (2)
Walter Roberson
on 16 Jul 2022
Use bwconncomp or regionprops or bwlabel in order to have each connected group of values marked as being part of the same group, returning information about the size and position. Then you would go through the list of groups and check the sizes, discarding regions too large or too small.
You might also want to consider using bwareafilt to filter out regions that are too large or too small . For example you might ask to reject areas smaller than (80*120) pixels and areas larger than (80*120) pixels, which would leave only areas of 9600 pixels exactly; you would then label the regions and then check whether the height and width was right (as opposed to some other shape that just happened to have exactly that area.)
1 Comment
Walter Roberson
on 18 Jul 2022
Use bwareafilt to remove everything with area less than (80*120)
Now regionprops() asking for 'Image' and 'BoundingBox'
Loop over the 'Image' entries and use sum() of the image along the second dimension. Check to be sure that all row counts are at least 120, and reject the entry if it fails. Also take nnz() of the image and compare it to numel() of the image to ensure that nnz() is more than half of numel() to be sure that the region has more 1 than 0.
You can pull out the BoundingBox of the regions that pass your test.
Note: I recommend you re-think your requirements. At present, you could have a completely solid region that was (say) 200 * 500 (plenty large enough), except that there might be one additional pixel above the top row (especially if these are .jpg images -- jpg images introduce artifacts.) Your stated requirements in https://www.mathworks.com/matlabcentral/answers/1761725-detect-a-specific-rectangle-in-an-image#answer_1009345 is not whether there is a large enough region that has at least 120 pixels per row: your stated requirements are that all rows must have at least 120 solid pixels
I = false(100,150);
I(10:end-10,10:end-11) = true;
I(end-9, 50) = true;
imshow(I)
Your current rules would reject this because there is a row that only has one solid pixel, in violation of your rule that all rows must have at least 120 solid pixels.
Mehdi Kooli
on 17 Jul 2022
3 Comments
Image Analyst
on 19 Jul 2022
I don't see any rectangles here:
unless you're considering just each line one at a time, ignoring the fact that it might be connected to a rectangle above and below it which might make the combined shape no longer a perfect rectangle. Can you show the original grayscale or color image from which this segmented image came from and tell us what you want to find in that?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!