looping image analysis problem.

조회 수: 1 (최근 30일)
Vidur Joshi
Vidur Joshi 2019년 3월 22일
댓글: KALYAN ACHARJYA 2019년 3월 24일
I've been attempting to have my code run a set of images, and they all have the same naming format, and I want to analyze them, but when I try to loop them, it keeps going on and on and on with no end. What am I doing wrong?
My code loop:
clear all;
image='img_000000000_Default_000.tif';
s=4;
results=zeros(2,s+1);
for i = 0:s
while (i<10)
image=['img-00000000', int2str(i), '_Default_000.tif'];
end
while (i<100 && i>= 10)
image=['img-0000000', int2str(i), '_Default_000.tif'];
end
while (i<1000 && i>=100)
image=['img-000000', int2str(i), '_Default_000.tif'];
end
results(1:2,i+1) = imagefunction(image,3);
end
My code for imagefunction
function [tubemean, tablemean] = imagefunction(image,boxsidelength)
imagex=imread(image);
imshow(image);
boxalter = (boxsidelength-1)/2;
newimage1= zeros(110-(2*boxalter), 130-(2*boxalter));
boxstartpointx = 370 + boxalter;
boxendpointx=480-boxalter;
boxstartpointy=150+boxalter;
boxendpointy=280-boxalter;
box1startpointx = 420 + boxalter;
box1endpointx=426-boxalter;
box1startpointy=201+boxalter;
box1endpointy=208-boxalter;
image1sum=0;
pixelcount1=0;
box2startpointx = 458;
box2endpointx=464;
box2startpointy=181;
box2endpointy=188;
image2sum=0;
pixelcount2=0;
for r = boxstartpointx:boxendpointx
for c = boxstartpointy:boxendpointy
box=imagex(r-boxalter:r+boxalter,c-boxalter:c+boxalter);
boxmean= mean2(box);
boxstd= std2(box);
eqn= boxstd/boxmean;
newimage1(r-(370-boxalter),c-(150-boxalter)) = eqn;
end
end
for r=box2startpointx:box2endpointx
for c = box2startpointy:box2endpointy
image2sum=image2sum+imagex(r,c);
pixelcount2=pixelcount2+1;
end
end
for r=box2startpointx:box2endpointx
for c = box2startpointy:box2endpointy
image2sum=image2sum+imagex(r,c);
pixelcount2=pixelcount2+1;
end
end
for r=box1startpointx:box1endpointx
for c = box1startpointy:box1endpointy
image1sum=image1sum+imagex(r,c);
pixelcount1=pixelcount1+1;
end
end
imagemean=mean2(image);
imshow(imagex);
tubemean=imagemean;
tablemean=image2sum/pixelcount2;
Images are .tif btw, if that is important

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 3월 23일
편집: KALYAN ACHARJYA 2019년 3월 24일
for i = 0:s
while (i<10)
image1=['img-00000000', int2str(i), '_Default_000.tif'];
end
while (i<100 && i>= 10)
image1=['img-0000000', int2str(i), '_Default_000.tif'];
end
while (i<1000 && i>=100)
image1=['img-000000', int2str(i), '_Default_000.tif'];
end
results(1:2,i+1) = imagefunction(image1,3);
end
Yes, your loop run for infinite time
Start from for loop
when i=0
Next go to
while (i<10) % Condition true
%Do operation
end
This while loop runs for infinite time, till i<10, as there are no change of i inside while loop, therefore it runs for infinite times.
Same case may be for other while loop also, please check. While loop run until the condition is true.
Better to use if else, in your case.
for i = 0:s
if i<10
image1=['img-00000000', int2str(i), '_Default_000.tif'];
elseif i<100 && i>= 10
image1=['img-0000000', int2str(i), '_Default_000.tif']
elseif i<1000 && i>=100
image1=['img-000000', int2str(i), '_Default_000.tif'];
else
%do nothing
end
results(1:2,i+1)=imagefunction(image1,3);
end
Hope it helps!
  댓글 수: 2
Image Analyst
Image Analyst 2019년 3월 23일
image is a built-in function. Don't use it for the name of a variable or you will run into trouble if you try to call image later as a function.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 3월 24일
@Image Analyst Yes sir, I missed it. Answer edited, Thank You!

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by