Detect a specific rectangle in an image
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2022-7-18
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.
回答(2 个)
Walter Roberson
2022-7-16
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 个评论
Walter Roberson
2022-7-18
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
2022-7-17
3 个评论
Image Analyst
2022-7-19
I don't see any rectangles here:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1070450/image.png)
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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!