How to add two binay images and add the result to a third image?

Dear all,
I have one array that includes 5 binary images [I1, I2, I3, I4, I5].
I want to have a new array [R1, R2, R3, R4] where:
R1 = I1 + I2
R2 = R1 + I3
R3 = R2 + I4
R4 = R3 + I5
How to make a for loop for that?
Any help will be very appreciated.
Thank you very much.
Meshoo

 Risposta accettata

Try this:
R1 = int32(I1) + int32(I2);
R2 = R1 + int32(I3);
R3 = R2 + int32(I4);
R4 = R3 + int32(I5);
output_R = [R1, R2, R3, R4];
imshow(output_R, []); % The [] are important.

8 Commenti

Thank you very much, but I want to make a for loop for that. It is not always that I have 5 binary images in the array. Sometimes I have 100, 120, etc.
Any idea how to make a loop for that?
Thank you very much.
Meshoo
Then for the "one array" you'll have to know how many images are there in it. For example, if it's a 500 row by 1000 column array, are the I1, I2, etc. 100 columns wide each, so that you have 10 images? Or are they 500 columns wide so that you have 2 images? Or are they 50 pixels wide so that you have 20 images? You have to know otherwise you can't extract the I1, I2, etc. from the "one array".
To make it clearer. I have a stack images (with same size and format)and I use the following code to upload them:
[fileName,pathName] = uigetfile('*.tif')
dname = fullfile(pathName,fileName)
filelist = dir([fileparts(dname) filesep '*.tif']);
fileNames = {filelist.name}';
num_frames = (numel(filelist));
I = imread((fileNames{1}));
Now let's say I want to binarize all the images and put them in one new arry Z. I did the following:
Z = []
for k = 1:num_frames; % to test small set use---> for k = 1:5%
I = imread(filelist(k).name);
I = im2bw(I);
Z{k} = I;
end
If I want to see the first image
figure, imshow(Z{1,1});
Now I want to create a new arry R[] and to add those images in Z such as
R1 = Z1 + Z2
R2 = R1 + Z3
R3 = R2 + Z4
R4 = R3 + Z5
Number of images in the stack is known = num_frames.
So how do that a for loop for that?
Thank you.
Do you want to do an integer addition? Or do you want to do a logical "OR" of the binary images. In other words, if you have 5 images all with some particular pixel set (1, white, true), then do you want the output value of that pixel to be 1,white,true? Or do you want it to have a value of 5?
I want to do logical such that 1+1 = 1 (bilevel image).
Initialize Z{1} to be the first binary image, when k == 1. Then for subsequent iterations just OR it in
if k == 1
Z{k} = I;
else
Z{k} = Z{k-1} | I;
end
Meshooo
Meshooo il 19 Ago 2014
Modificato: Meshooo il 19 Ago 2014
Thank you very much. Your program will add all the binary images, but actually I want the flow of the program to be in the same way I explained before, because I am doing other operations (adding the binary was just an example).
So how to make a new array R[ ] that contains four binary images R1, R2, R3, R4.
R1 = I1 + I2
R2 = R1 + I3 % do something
R3 = R2 + I4
R4 = R3 + I5
Not a good idea. Remember you said "Sometimes I have 100, 120, etc." So now I refer you to the FAQ about why this is a bad idea: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Accedi per commentare.

Più risposte (1)

Here is how to make a loop for that:
R(:,1)=I(:,1)+I(:,2); %Initialization
for i=2:4
R(:,i)=R(:,i-1)+I(:,i+1); %This is the loop that you asked for.
end

5 Commenti

His I1, I2, etc. are arrays (2D images), not 1D column vectors like your code requires.
In that case, I can't repair the loop until I know exactly how each image is stored. Are they literally a concatenated array like [I1 I2 ...] or ar they stored individually as variables I1 and I2 or are they cell arrays or a struct. The same indexing logic should work regardless though.
One fool-proof way to do it would be to flatten the images to 1D, and form the 2D array I=[I1,I2...] and use the above code, then unflatten them using "reshape".
My question exactly. The situation is not defined clearly enough to answer. But I think it can be done without reshaping, just so long as you know the size and number of the I1, I2, etc. that make up the "one array".
Thank you all for your comments. Is there some way to do it directly without any reshaping?
Yes. Did you read our comments about what information is required?

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by