2

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 because it has been generated with other process) with two different parameters for each image. For example:

Parameterimage1_1= 1.2; %corresponds 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. 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;


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. Usually, I do it in that way but I don't know how to indicate that has to take the two parameters.

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);

It is possible to find the parameters by name? (I mean, for example in a excel file to take the values for the same name as the image). I don't know how to automatize that.

Any kind of help would be appreciated,

Greetings,

1 Answer 1

3

I'm a little confused but you can read mixed text and numerical excel files into cell arrays using xlsread

[num,txt,raw] = xlsread(___)

The raw output is a cell array. You can use strfind to match your image name string and then read in the parameter values from other columns in the spreadsheet.

If you want to be able to generate you image names (i.e. 'image3.tif') then use num2str and concatenate your image number string...something like

    i = 3;
    image_str =  ['image' num2str(i) '.tif'] 

edit:

params = magic(3); % lets pretend these are your parameters
number_of_images = numel(:,1);
for i = 1:numel(number_of_images) 
    %Create string to read image
    image_str =  ['image' num2str(i) '.tif'];
    % Read image
    image = imread(image_str);
    % apply the params
    image = param(i,1)*image/param(i,2);
    %write image output
    imwrite(image, ['G:\' image_str],'tiff');
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks tangled_foot. I've simplified it and now I have a vector with different columns with the parameters (the first column is param1 and the second is param2). How can I make that in every loop of the different images get a different line of the parameters? I mean, for the first image, first line, for the second, the values of the second line... Thanks in advance!
Thanks a lot tangled_foot! I've tried it also but I have a problem with that line: image = param(i,1)*image/param(i,2); The problem is the following: Undefined function 'mtimes' for input arguments of type 'struct'. I'm going to check why is it happening that
That's happening because your param variable is a structure and my code wanted it to just be a matrix. If its a matrix within a struct you'll have to add that part to line so that it points to the matrix that you're saving your param values in within the struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.