I = imread('Coins.jpg');
Id = im2double(I); % I is a uint8 grayscale image
T = 0.5*(min(Id(:)) + max(Id(:)));
deltaT = 0.01; % convergence criterion
done = false;
while done
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(g)));
done = abs(T - Tnext) < deltaT;
T = Tnext;
end
i want to count number of iteration for this program.! as well i want to store value of T at each iteration.!

2 commentaires

I = imread('Coins.jpg');
Id = im2double(I); % I is a uint8 grayscale image
done = false;
count = 0
T = zeros(1,100); % Try to presize to something big enough to hold all iterations if you can estimate it
while done
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(g)));
count = count + 1;
T(count) = Tnext
end
Why we changed T.? Cant we do with previous T.?
And above is giving results as 0
Adam
Adam le 16 Déc 2014
I edited my answer below. I didn't notice the initialisation of T higher up and the >= test against it in the while loop.

Connectez-vous pour commenter.

 Réponse acceptée

Adam
Adam le 16 Déc 2014
Modifié(e) : Adam le 16 Déc 2014
count = 0
newT = zeros(1,100); % Try to presize to something big enough to hold all iterations if you can estimate it
while done
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(g)));
count = count + 1;
newT(count) = Tnext
done = abs(T - Tnext) < deltaT;
end
Presize newT if you can come up with a sensible bounded estimate, otherwise the warning you get is likely irrelevant anyway. Your code will be a little slower, but unlikely to matter in this case.

6 commentaires

I = imread('Coins.jpg');
Id = im2double(I); % I is a uint8 grayscale image
T = 0.5*(min(Id(:)) + max(Id(:)));
deltaT = 0.01; % convergence criterion
done = false;
count = 0
newT = zeros(1,100); % Try to presize to something big enough to hold all iterations if you can estimate it
while done
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(g)));
count = count + 1;
newT(count) = Tnext
end
This gives 0 count and 0 value of newT at each point.
You initialise
done = false;
and then do
while done
...
end
This means your loop will not execute at all.
I thought the name of the loop variable was confusing when I glanced at it, but i didn't look at that part of the logic. To keep the variable name sensible what you want is
while ~done
...
end
Image Analyst
Image Analyst le 16 Déc 2014
Modifié(e) : Image Analyst le 16 Déc 2014
Why did you set done=false , so that you never enter the loop? And, even if you did get into the loop, you never update done so how would it know when to exit the loop? Please learn how to use the debugger. If you knew how, you'd step through the code line by line and you would have seen that you never enter the loop. Debugging it yourself is SO much faster than debugging via back and forth messages in a discussion forum. Even after you fix those, there are other errors I can see that I haven't told you about but which you'll discover, and probably realize how to fix once you see them, when you step through the program.
Adam
Adam le 16 Déc 2014
Note, I just edited my original answer to add in the updating of done in the loop that I accidentally missed out first time.
Nimisha
Nimisha le 16 Déc 2014
Ya, This ~ sign works. Thank You very Much :)
Adam
Adam le 16 Déc 2014
No problem :)
Would have been a bit quicker if I absorbed your whole original post first time, but I was trying to just scan it for the parts relevant to the question which turned out to be a mistake!

Connectez-vous pour commenter.

Plus de réponses (0)

Question posée :

le 16 Déc 2014

Commenté :

le 16 Déc 2014

Community Treasure Hunt

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

Start Hunting!

Translated by