How to scan an image to find out the row or column that will be the first background pixel?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have a binary image. I have to scan the image from centroid to its right to find out row consisting of first background pixels ,then centroid to left side and from centroid to top. in each case I have to find out the distance between two columns and rows and find out the max distance.
Accepted Answer
As pointed out by Walter, background is normally black, so you need to invert the image. Finding out the centroid is trivial with regionprops and from there it's also easy to find the boundary pixels.
bwimg = ~yourimage; %invert image so that background is black
props = regionpros(bwimg, 'Centroid');
assert(numel(props) == 1, 'more than one object found in image');
centroid = round(props.Centroid);
left = find(bwimg(centroid(1), :), 1)
right = find(bwimg(centroid(1), :), 1, 'last');
top = find(bwimg(:, centroid(2)), 1);
bottom = find(bwimg(:, centroid(2)), 1, 'last');
edit: capitalisation of Centroid and rounding of value
17 Comments
Zara Khan
on 19 Feb 2018
Thank you for the help. Now how will find out distance between centroidcolumn to leftcolumn, centroidcolumn to rightcolum and centroidrow to top . I also want to plot left,right and top over image object.
Taniya:
The centroid field is capitalized and may be floating point so you need to round it. You can also call bwareafilt() to make sure there is only one blob. Of course you should also check that there is at least one blob. If there are zero, it will throw an error.
bwimg = bwareafilt(~yourimage, 1); %invert image so that background is black
props = regionpros(bwimg, 'Centroid');
left = find(bwimg(round(props.Centroid(1)), :), 1)
etc.
Zara Khan
on 19 Feb 2018
Image Analyst Thank you. I have already rounded off and I am getting left,right,top and bottom values individually. Now how to how to find out distance between centroidcolumn to left and right and also from centroidrow to top and consider the maximum distance as I have crop the image by considering the maximum length.And I have to crop with square sized block.
I'm not sure I understand the question about the distance. The distance between the centroid and the left most point is obviously
props.Centroid(1) - left
Guillaume thank you so much. It works. Now how to take a portion of image by considering this distance or length. I mean I have to consider a square size image whose sides will be equal to this distance or length and have to consider it as a separate image.I am attaching an image here. I want the same like output. These two red square boxes will be two separate images.
Again, I really don't see the problem, since you have all the coordinates/dimensions you need:
imgside = max(props.Centroid(1) - left, props.Centroid(2) - top);
upperleftimage = bwimage(props.Centroid(2) - imgside : props.Centroid(2), props.centroid(1) - imgside : props.centroid(1));
Also, I'm not sure why we're discussing my answer since you accepted another one.
I am a beginner. And also new to this page. Now the problem has been resolved . I accepted your answer .
Zara Khan
on 20 Feb 2018
How to find out the upperright part so that the centroid column will be included into into the box.
Zara Khan
on 20 Feb 2018
Please help me in doing this as I am new to matlab . So not getting any clue to do this
Guillaume
on 20 Feb 2018
I'm completely lost as to what you're asking. Isn't the upper right part [top right] ?
Zara Khan
on 20 Feb 2018
Yes.
Guillaume
on 20 Feb 2018
Well, then what is your question?
I want the top right i mean the upper right image like the upper left image
Zara Khan
on 21 Feb 2018
Still I am not able to find out the upper right part. Please anyone help me in doing this
I still don't understand what the problem is. In the answer you have:
%...
right = find(bwimg(centroid(1), :), 1, 'last');
top = find(bwimg(:, centroid(2)), 1);
%...
That's the upper right coordinate.
Yes that I have done. Now in your second code you have helped me in finding out the upper left square box. Similar box I want for upper right image.imgside = max(props.Centroid(1) - left, props.Centroid(2) - top ); upperleftimage = bwimage(props.Centroid(2) - imgside : props.Centroid(2), props.centroid(1) - imgside : props.centroid(1).That was your codes. Now I want same like for upper right part. Plz help me in doing this. I already have attached a pic that how the output will look like. Thanks in advance
This is really basic matrix indexing so I don't understand why you can't work it out on your own
subimage = image(toprow:bottomrow, leftrow:rightrow);
replace toprow, bottomrow, leftrow and rightrow by whichever coordinate you're interested in. In this latter case,
subimage = image(top:centroid(2), centroid(1):right);
More Answers (1)
Walter Roberson
on 19 Feb 2018
The distance in each case is 0. The centroid is inside the background area, so the distance to background is zero.
A note in this regard: in binary images, 0 is background and 1 is foreground.
1 Comment
Zara Khan
on 19 Feb 2018
Thank you for the reply.Actually the image need to be inverted first then have to scan the image from centroid column to image width and from centroid row to image height then have to find out the first black pixel row and it's distance from the centroid column. This process will be repeated for left hand side and top too. Then have to find out the max distance
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)