How do i combine a set of images on MATLAB?
Show older comments
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
Walter Roberson
on 4 Sep 2016
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?
Mohammed
on 4 Sep 2016
Image Analyst
on 5 Sep 2016
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.
Mohammed
on 5 Sep 2016
Answers (2)
Walter Roberson
on 5 Sep 2016
total_image = sum( cat(4, TheCell{:}), 4, 'native');
5 Comments
John BG
on 5 Sep 2016
blending, not concatenating
Walter Roberson
on 5 Sep 2016
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.
Mohammed
on 5 Sep 2016
Walter Roberson
on 5 Sep 2016
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.
Walter Roberson
on 5 Sep 2016
If you want the average image, then
total_image = cast( mean( cat(4, TheCell{:}), 4), class(TheCell{1,1}) );
Image Analyst
on 5 Sep 2016
0 votes
See my attached demo to average RGB images together.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!