Setting up a loop: Round 1

조회 수: 1 (최근 30일)
Frank
Frank 2011년 5월 18일
Hello!
I've narrowed down what I want this script to do, but I want it to loop through every specified image in a folder through a multiple select with "uiget" and then deposit the looped images into a different folder. However, I don't know how to set it up for a loop. Can anyone help?
  • I'm using the first image as a reference to the other images, so the loop function would run through the second image.
Thanks!
-Frank
Code:
%First Image
i = imread('343b #10000.tif');
j = size(i);
imtool(i);
for k = 1:j(1)
for l = 1:j(2)
if i(k,l) > 160 & i(k,l) > 160
d(k,l) = 1;
else
d(k,l) = 0;
end
end
end
imtool(d);
labela = bwlabel(d);
imagesc(labela);
[labela,num] = bwlabel(d,4);
stats = regionprops(labela,'basic');
stats(1).Area
stats(1).Centroid
max_area = max([stats.Area]);
biggrain = find([stats.Area]== max_area);
p = stats(biggrain).Centroid
%Second Image
z = uigetfile('*tif') %select a file for displacement
a = imread(z); %reads the selected file
b = size(a); %gauges the size of the selected file
imtool(a);
for c = 1:b(1)
for l = 1:b(2)
if a(c,l) > 160 & a(c,l) > 160
e(c,l) = 1;
else
e(c,l) = 0;
end
end
end
imtool(e);
labelb = bwlabel(e);
imagesc(labelb);
[labelb,num] = bwlabel(e,4);
stats = regionprops(labelb,'basic');
stats(1).Area
stats(1).Centroid
max_area = max([stats.Area]);
biggrain = find([stats.Area]== max_area);
o = stats(biggrain).Centroid
%Calculates the differences in the centroid
displacement = p -o %Calculates the difference in centroids
x = displacement(1); %Places x value from displacement into x
%%IM Translate
a2 = imtranslate(a, [0 x]); %Note: [y x] for imtranslate, if it's negative make it positive
imshow(a2)
imwrite(a2, z, 'tif')

채택된 답변

Andrew Newell
Andrew Newell 2011년 5월 18일
You could set up a loop like this:
%Second Image
z = uigetfile('*tif'); %select a file for displacement
while ~isequal(z,0) %if no file is selected, uigetfile returns 0
% process the image here
o = stats(biggrain).Centroid;
z = uigetfile('*tif');
end
  댓글 수: 1
Frank
Frank 2011년 5월 19일
Works great! Thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Biomedical Imaging에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by