Loop with different images on Matlab
1 view (last 30 days)
Show older comments
Emmanuelle
on 24 Jul 2014
Commented: Emmanuelle
on 14 Aug 2014
I have a question about how can I create a loop. I'm going to try to simplify. I have three images (the real problem has a huge images). For example:
image1.tif
image2.tif
image3.tif
On the other hand, I have a text file (I cannot introduce this on the code directly) with two different parameters for each image. For example:
Parameterimage1_1= 1.2; %value related to image1
Parameterimage1_2= 2.3; %corresponds to image1
Parameterimage2_1= 5.3; %corresponds to image2
Parameterimage2_2= 2.4; %corresponds to image2
(...)
What I need to do is to read the text file and then, apply two different parameters for each image in a loop to calculate. What I have done is the following:
Image1= imread ('image1.tif');
Image2= imread ('image2.tif');
Image3= imread ('image3.tif');
Data= READINGPARAMETERS(parameters)
param1= Data.param1;
param2= Data.param2;
param3=Data.param3;
(...)
Image1_out= param1*Image1/param2;
Image2_out= param3*Image1/param4;
Image2_out= param5*Image1/param6;
imwrite(Image1_out, 'G:\Image1_out.tiff','tiff');
imwrite(Image2_out, 'G:\Image2_out.tiff','tiff');
function [Data] = READINGPARAMETERS(parameters)
fid = fopen(parameters); % I have defined the path previously
text = fscanf(fid, '%c');
posini= strfind(text,'=');
posfin= strfind(text,';');
Datos.param1= str2num(texto(posini(1)+1 : posfin(1)-1));
Datos.param2= str2num(texto(posini(2)+1 : posfin(2)-1));
Datos.param3= str2num(texto(posini(3)+1 : posfin(3)-1));
(...)
return
My question is that I don't know how to create the loop for that and with every image, uses two parameteres. Usually, I do it in that way but I don't know how to indicate that has to take the values.
for k = 1:length(tifFiles)
baseFileName = tifFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
AND here the process
imwrite(imageArray, fullFileName);
Any kind of help would be appreciated,
Greetings,
0 Comments
Accepted Answer
Matz Johansson Bergström
on 24 Jul 2014
I don't know exactly what you do in the function READINGPARAMETERS, but I would first let the function store the values in a matrix Data instead of the was you describe it (is it a cell?). Say you have 100 images, then the matrix would be 100x2.
Then I would loop through the images something like (I have not tried this)
for i=1:length(tifFiles)
imageArray = imread(fullFileName);
out = Data(i,1)*imageArray*Data(i,2);
imwrite(out, ['G:\', sprintf('Image%d_out.tiff', i)], 'tiff');
end
By the way, is that supposed to be Image1 for each row or Image1, Image2 and Image3?
Image1_out= param1*Image1/param2;
Image2_out= param3*Image1/param4;
Image2_out= param5*Image1/param6;
This is a step on the way I think.
5 Comments
Matz Johansson Bergström
on 24 Jul 2014
Edited: Matz Johansson Bergström
on 24 Jul 2014
Ok, I think I know what you want to do now. It seems that the syntax of the file you want to parse is for example
bla bla = 43.235;
bla bla = 6.235;
param1 and param2 should be applied to the first image, param3 and param4 to the next etc. if I understand you correctly.
Then the following should work
function Data= READINGPARAMETERS(parameters)
fid = fopen(parameters);
strs = textscan(fid, '%s = %f;') %strings and floats
Data = zeros(length(strs), 2);
tmp = strs{2}; %pick out the floats
%The parameters are in the columns for each image file
Data(:, 1) = tmp(1:2:end);
Data(:, 2) = tmp(2:2:end);
Now you will have the parameters in a matrix, so you can apply them as previously mentioned. I hope this is what you wanted.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!