How do I prevent my variable from being overwritten?

from binary image like white color banana and background is black .I divided this in three part 20% ,60% and 20%Middle 60% part is used to measure width of banana.I have rows and cols coordinates; for each col (within 60%area) want to find width
[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);
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;
Desired P should be of size 1 x (length(columns))

1 commentaire

I gave you more compact and efficient code below, but you chose not to use it. Tell why.

Connectez-vous pour commenter.

 Réponse acceptée

Your d is getting overwritten at each iteration of the j loop. You need to have d(j) instead of just d. But you actually don't even need it at all to find the first and last white pixel on a line. Just use find() with the 'first' and 'last' option:
for row = n11 : n22
x2 = find(BW(row,:), 1, 'last');
x1 = find(BW(row,:), 1, 'first');
d1(row) = x2 - x1;
end

10 commentaires

for row = n11 : n22 x2 = find(BW(row,:), 1, 'last'); x1 = find(BW(row,:), 1, 'first'); d1(row) = x2 - x1; end * ERROR MESSAGE: *Improper assignment with rectangular empty matrix.
Is my logic is right for finding width?after measuring width i will convert this into mm and find mean of it.pl suggest whether i am in right direction or not?
You have to make it more robust . That is what I keep telling people . When do you think this would fail? How about when there is a row (line) with no white pixels at all on it? What would you do then? find() returns an empty array. So you need to do something like
if ~isempty(x1)
See what you can do with that hint to make it handle lines with no white pixels in it.
if ~isempty(x1) d=0; end
IS IT CORRECT???
Image Analyst
Image Analyst le 20 Sep 2013
Modifié(e) : Image Analyst le 20 Sep 2013
Almost. But you didn't check for x2, and you didn't use d1(row).
for row = n11 : n22
x2 = find(BW(row,:), 1, 'last');
x1 = find(BW(row,:), 1, 'first');
if isempty(x2) || isempty(x1)
% No white pixel on this line.
% (If x1 is empty, x2 will also be
% so you don't really need to check both of them.).
d1(row) = 0;
else
d1(row) = x2 - x1;
end
end
You can attach your file if you want.
for col2 = n11 : n22 x2 = find(BW(:,col2), 1, 'last'); x1 = find(BW(:,col2), 1, 'first'); if isempty(x2) isempty(x1) % No white pixel on this line. % (If x1 is empty, x2 will also be % so you don't really need to check both of them.). d1(col2) = 0; else d1(col2) = x2 - x1; end end
*my code works well from above code, but d1 size is 1x n22
required d1 size is n11x n22*
Why would it be a 2D matrix when all you're recording is the thickness of your object on each line. What is the other dimension for?
sorry,i mean d1 size should be 1 x (n22-n11) and required number of column are n22-n11 but getting number of columns n22. To get d1 of size 1 x (n22-n11) i extract nonzero element from d1 matrix and got desired result i.e d1<1 x (n22-n11)>
"Thank you for your suggestions"
If you want values of indexes in d1 to correspond to row numbers in the original image, then you should have as many rows in d1 as you do in the image. So then, if d1(99) = 321, you know the width at line 99 is 321 pixels, regardless of where you start or stop considering the rows (regardless of what n22 or n11 are).
Otherwise you will have n22-n11+1 elements. For example if n11 = 101, and n22 = 199, you will have 199-101+ 1 = 99 elements. But then you need to know that d1(1) corresponds to row 101, and d1(41) corresponds to row 141.
So, which way do you want it? Also, please attach your image so I can try the code with your exact image.
See if the file attached below does what you want.
sir, file is very useful for me. Thanks for helping me.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by