How to scan an image to find out the row or column that will be the first background pixel?

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

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.
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 .
How to find out the upperright part so that the centroid column will be included into into the box.
Please help me in doing this as I am new to matlab . So not getting any clue to do this
I'm completely lost as to what you're asking. Isn't the upper right part [top right] ?
I want the top right i mean the upper right image like the upper left image
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);

Sign in to comment.

More Answers (1)

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

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

Sign in to comment.

Asked:

on 19 Feb 2018

Edited:

on 21 Feb 2018

Community Treasure Hunt

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

Start Hunting!