Detect a specific rectangle in an image

17 次查看(过去 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 个评论
Mehdi Kooli
Mehdi Kooli 2022-7-16
@Image Analyst Thank you for your reply.
The question may concern anyone who can help.
I mentionned Walter Roberson because I saw he replied to a similar topics questions.
Image Analyst
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
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
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
Mehdi Kooli 2022-7-17
@Walter Roberson Thank you for th response.
To be more precise, I'm searching for rectangles having the size (80*120) pixels or more.
The complexity is how to check if my labeled region dosen't have more '0' tha '1' and how to check if all the lines have at least 120 pixels of '1'. Is there any matlab commands or functions please ?
  3 个评论
Mehdi Kooli
Mehdi Kooli 2022-7-19
编辑:Image Analyst 2022-7-19
@Walter Roberson Thank you for the response.
@Image Analyst Thank you for the tutorial.
You will find attached a csv file containing my binary data (215 X 625).
You can consider it as a binary image. The goal is to find all the rectangles having at least the size 20 X 164 (as exemple) full of '1' .
I tried the code :
New_C1_Binary=csvread('C:\Users\cskopinski\Desktop\IHM_Coude-E\Datas exemple compensation eloignement\New_C1_Binary.csv');
[l_m,k_m]=size(New_C1_Binary);
val1=20;
val2=100;
New_C1_Binary=logical(New_Imag_ZNPG_Tab_C1_thres); % conversion data to logical type
BW_1 = bwareafilt(New_Imag_ZNPG_Tab_C1_thres,[(val1*val2) (l_m*k_m)]); %ffilter the image in order to remove objects lower than 20*164
Image_stat = regionprops(BW_1,'Image') ;
Boundingbox_stat= regionprops(BW_1,'Boundingbox');
ROI_1=Image_stat(1).Image;
imshow (ROI_1); % THIS is the first object that I should findrectangle in (see attached image "ROI_1.png")
I should get the position of each rectangle found in ROI_1. Then I restart the process for each object detected after applying bwareafilt (4 objects in this case).
Do you have any code example to propose please?
I'm not very proficient in Matlab.
Image Analyst
Image Analyst 2022-7-19
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?

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by