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

조회 수: 2 (최근 30일)
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.

채택된 답변

Guillaume
Guillaume 2018년 2월 19일
편집: Guillaume 2018년 2월 19일
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
Zara Khan
Zara Khan 2018년 2월 21일
편집: Zara Khan 2018년 2월 21일
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
Guillaume
Guillaume 2018년 2월 21일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 19일
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
Zara Khan
Zara Khan 2018년 2월 19일
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

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by