Loop with different images on Matlab

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,

 Risposta accettata

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 Commenti

Thanks a lot Matz. The function readingparameters is with the aim to read some values from a txt file. It would be also fine to read and store in a Matrix, maybe it's easier. I've tried it also but I have a problem with that line:
out = Data(i,1)*imageArray*Data(i,2);
The problem is the following:
Undefined function 'mtimes' for input arguments of
type 'struct'.
I have changed it on that way:
for i=1:length(tifFiles)
params = fieldnames(Datos);
baseFileName = tifFiles(i).name;
fullFileName = fullfile(dir_entrada, baseFileName);
imageArray = imread(fullFileName);
out = Datos(i,1)*imageArray;
imwrite(out, ['G:\salida\', sprintf('Image%d_out.tiff', i)], 'tiff');
end
And the problem is alwsays on that line:
out = Datos(i,1)*imageArray;
I'm going to check better but thanks because I think it's the way for doing it!
Yes readingparameters is returning a struct, so Data will then be a struct, which is not what we want. If you post the complete readingparameters function I can see what is happening.
Ok!! Here it is:
function [Data] = READINGPARAMETERS(ficherointerc)
fid = fopen(ficherointerc);
texto = fscanf(fid, '%c');
fclose(fid);
posini= strfind(texto,'=');
posfin= strfind(texto,';');
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));
Datos.param4= str2num(texto(posini(4)+1 : posfin(4)-1));
return
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.
Thank you so much, it works!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Convert Image Type in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by