How do i combine a set of images on MATLAB?

I am trying to write a function that Combines coloured images into a single RGB image(the images are the inputs which are entered as a 1D cell array). the function should return a 3D array representing the combined image.we know that for example that the blue intensity value for the pixel in row 1, column 1 of the combined image will be the sum of the blue intensity values of each pixel in row 1, column 1 of the input images. how do i write a function to represent this?
Thanks

4 Comments

What is the input? A directory name with it being implicit that all .png files in the directory are to be combined? A cell array of strings of image names? A 4 dimensional array in which the 4th dimension is the frame number?
Are all of the images definitely exactly the same size?
When you add images, it would be common for the intensities to overflow. Do you want to be doing some kind of Alpha blending of the images, or is it the case that your input images are already known to be segmented, zero only in isolated regions, and the regions do not overlap between frames?
Thanks for the response, Walter
the input is a cell array of values representing an image (e.g. [zeros(128,'uint8')]). yes all the arrays representing the images are of the same size. yes i want the output to be another image which is blending the images entered as inputs together. since this is a function, the user will enter arrays to represent an image and then the function should return a new image representing the combined image your help is much appreciated :)
Are you sure you want the sum and not the average? Do you want the combined image to be like all images are averaged together, which is like you superimposed them all? Because if you sum uint8 images eventually they will clip at 255. If you cast to double and sum and then divide by the number of images, then cast to uint8 you will not have clipping and the images will look like the combined (averaged/superimposed) image.
Thanks for the response
i am not sure which will be the correct word to describe the situation? sum or average? but i want the output image to be a mixture of the images entered.

Sign in to comment.

Answers (2)

total_image = sum( cat(4, TheCell{:}), 4, 'native');

5 Comments

blending, not concatenating
No alpha value was defined, and no ordering was defined, so summing as originally requested is valid output.
I suspect, John, that you failed to notice the sum() along the 4th dimension.
thanks so much Walter:) what does 'native' stand for??
The 'native' means that the sum should be carried out in whatever data class that the data itself is. Without the 'native' the default is to use double precision.
It is the difference between
uint8(254) + uint8(2)
and
double(254) + double(2)
The first of those will be carried out in uint8 arithmetic resulting in uint8(255) because uint8() "saturates" if you add enough values together. On the other hand, displaying uint8(255) as an image will know that is just the brightest intensity on a scale of 0 to 255.
The second of the two would result in double(257) . Displaying double(257) would result in the brightest intensity but for a very different reason: the expected range for double is 0 to 1 and everything from 1 upward would display as maximum intensity.
So you need to be considering two things: 1) What do you want to have happen when enough values get added together that 255 would be reached; and 2) the need to be able to display the image afterwards.
If you want the average image, then
total_image = cast( mean( cat(4, TheCell{:}), 4), class(TheCell{1,1}) );

Sign in to comment.

See my attached demo to average RGB images together.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 4 Sep 2016

Commented:

on 5 Sep 2016

Community Treasure Hunt

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

Start Hunting!