To overcome infinite loop problem to assign a particular value

1 view (last 30 days)
I want to assign zero to a pixel whose intensity value is greater than a certain threshold value, but my programme goes to an infinite loop. How can I overcome this problem. The code is
[M N]=size(I)
for i=1:M
for j=1:N
P(i,j)=improfile(I,i,j)
if(P(i,j)>t)
I(i,j)=0;
else
I(i,j)=1;
end
end
end
figure,imshow(I)% I is the input image array
Please help me to overcome this problem.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Nov 2013
P(i,j)=improfile(I,i,j) attempts to select the path using x = i and y = j as the endpoint of the line. You have not specified a second point to end at, and it is not documented what it would do in such a case. If you are expecting it to return I(i,j) then why not just code that directly instead of calling improfile() ?
It looks to me like your entire code could be replaced by
P = 0 + (I <= t);

More Answers (1)

Image Analyst
Image Analyst on 24 Nov 2013
Edited: Image Analyst on 24 Nov 2013
Looks like you're trying to do image segmentation. If so, just do
binaryImage = grayImage <= thresholdValue;
Notice how I use descriptive variable names, not an alphabet soup of single letter names. And it's not recommended to use i as a loop variable since i is also the imaginary variable. And of course I is a horrible variable letter to use because a capital I looks like a 1 or an l (lower case ell) in some fonts.
Anyway, you might be interested in my image segmentation tutorial: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial-blobsdemo

Community Treasure Hunt

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

Start Hunting!