BW is binary image want to find distance between first row having '1' and last row having '1' from column n11 to n22 from following code.from this code result obtained is from column 1 to n22,but desired result should be from column n11 to n22

BW=[0 0 0 0 0 0;1 1 1 1 1 0;0 1 1 1 0 0;0 0 0 0 0 0];
[rows, cols] = find(BW == 1);
n1=min(cols); n2=max(cols);
m1=min(rows); m2=max(rows);
column = n1:n2 ;
a=length(column);
b=a*0.2;
n11=round(n1+b);
n22=round(n2-b);
a1=horzcat(rows,cols);
columns = n11:n22 ;
for ii=n11:(n22);
for j=m1:m2
[row1 col]=find(BW(:,ii)==1) ;
x2=max(row1);
x1=min(row1);
d=x2-x1;
end
d1(ii)=d;
end
P=d1;

4 Comments

You didn't ask a question. Also, can you illustrate what you are trying to do?
So do you just want the number of rows between the top row and the bottom row (where rows means only rows that have any number of 1's in them)? Or do you want to find the distance between the two most distant points anywhere in the image? Or the distance between two most distant points where one point is in the top and the other point must be in bottom row? This is ambiguous so please clarify.
the distance between two most distant points where one point is in the top and the other point must be in bottom row in the same column.
_getting result_ P=[0 1 1 1]%%from col 1 to n22. Here n22=4
*desired result* P=[1 1 1]%%%for cols 2,3 & 4. n11=2 to n22=4
P should be 1x3 matrix but getting P<1x4>matrix
I didn't understand any of that. Try again. What is P? Please attach your image. You say "the distance between two most distant points where one point is in the top and the other point must be in bottom row in the same column" -- well if they're in the same column, but just in different rows (top row and bottom row) then why isn't it just the subtraction of the rows? Can you explain that to me? For example if I have a 1 in column 42 in row 10, and a 1 in column 42 (same column, as you said) of row 40, then why isn't it just 40-10 = 20 rows as the distance???
Anyway, let's say that really smart and perceptive person actually understood what you want, and gave you code for it. What do you want it for? What is the higher level goal of this hard-to-understand algorithm?

Sign in to comment.

Answers (1)

BW=[0 0 0 0 0 0;1 1 1 1 1 0;0 1 1 1 0 0;0 0 0 0 0 0];
n11=1;
n22=5;
s=BW(:,n11:n22);
id=find(any(s,2));
d=id(end)-id(1)

1 Comment

BW binary image like horizontal banana white color and background is black .I divided this in three part 20% ,50% and 20%.50% middle part is used to measure width.I have rows and cols coordinates and for each col (within 50%area) want to find difference between top & bottom of the banana.

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Asked:

on 20 Sep 2013

Community Treasure Hunt

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

Start Hunting!