How to add two binary images in order?

Dear all, I have a folder that includes a serial images, let's say 5 images X_1, X_2, X_3, X_4, X_5. I want to use the imadd matlab function to perform the following steps:
Step1: apply imadd to the first two images X_1 and X_2.
Add_1 = imadd(X_2, X_1);
Step2: apply imadd to X_3 and Add_1 that we obtained image from step1
Add_2 = imadd(X_3, Add_1);
Step3: apply imadd to X_4 and Add_2
Step4: again apply imadd to X_5 and Add_3 and so on.
Can we do that using a for loop?
I would appreciate any help and suggestions.
Thank you very much.
Meshoo

Answers (2)

Jan
Jan on 16 Oct 2013
Edited: Jan on 16 Oct 2013
"X_1" might be a simplification, but it matters, what this exactly mean. Is this a name of an image in a folder, the complete path or the imported image data? Do you really use "X_1" as name? If so, please read http://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop and avoid hiding an index in the name of a variable, but prefer a cell array: X{1}, X{2} .... Then adding gets easy:
Add = X{1};
for k = 2:5
Add = imadd(X{k}, Add);
end
Why do you want to add binary images, and what exactly do you mean by add? So do you really want to add them like integers? So that with 5 images you could have a max value of 5?
integerImage = int32(binary1) + int32(binary2);
or do you want to make sure that the output image is white wherever any of the input images are white, so in essence you want to OR the images:
binaryCombined = binary1 | binary2;
To determine which method you should use I really need to know what you plan on doing with this image once you've created it.

4 Comments

Thank you very much. Actually, I am not really focusing in the adding operation The main part of my question is how to apply an operation to two images then from the resulting image apply the same operation to a third image and so on.
In fact I am dealing with confocal microscopic images. So from the microscope I get a serial images with serial names such as Image1, Image2, Image3,....., Image100. Each serial image represent the depth, so Image1 is the upper image and next to it is Image2 and so on.
Now, let's say I want to apply a function between Image1 and Image2 such as imadd or imregister.
Add1 = imadd(Image1, Image2);
Now I want to use Add1 and apply imadd or imregister to it with the following image Image3.
Add2 = imadd(Add1, Image3);
Next I want to do the same thing for the rest of the images
Add3 = imadd(Add2, Image4);
So how can I do that for the whole serial 100 images using a loop?
Thank you very much.
@Mustafa: I have posted a suggestion already. Instead of repeating the question, you could explain if it helps already or which detail is not solved by it.
And explain why you said these are binary images (logical true/false) when it appears that they're probably actually color images or gray scale images since they came from a microscope.

Sign in to comment.

Tags

Asked:

on 16 Oct 2013

Edited:

on 23 Jan 2014

Community Treasure Hunt

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

Start Hunting!