Imfuse with 3 images

67 views (last 30 days)
Jacob
Jacob on 8 Jul 2013
Edited: DGM on 28 Oct 2021
Hello,
I have three RGB images that I would like to combine for display. I am using the imfuse function, but I get different results depending on whether I use:
X = imfuse(A,B);
Y = imfuse(X,C);
image(Y)
or
X = imfuse(A,C);
Y = imfuse(X,B);
image(Y)
and so on.
Is there a smart way to fuse all three images such that they each are weighted equally in the final result?

Answers (1)

DGM
DGM on 28 Oct 2021
Edited: DGM on 28 Oct 2021
The documentation for imfuse() is pretty transparent. The default mode is 'falsecolor', wherein two monochrome images (e.g. A and B) are represented in a primary-secondary color pair. This is simply done by concatenating copies of the two images to form an RGB image. The default ordering is equivalent to cat(3,B,A,B), resulting in a green-magenta color scheme.
As RGB inputs to imfuse() are converted to monochrome, the result from the first usage of imfuse() will be converted to grayscale, losing all distinguishing information between the first two images. In short, there isn't a way to use imfuse() to do anything with more than two images.
If the images are the same size, then one solution for creating a false color image with three inputs should be fairly obvious now.
% three RGB images
A = imread('peppers.png');
s = size(A);
B = imresize(imread('football.jpg'),s(1:2));
C = imresize(imread('gantrycrane.png'),s(1:2));
% trivial method for three images
D = cat(3,rgb2gray(A),rgb2gray(B),rgb2gray(C));
imshow(D)
That said, it might be kind of an incomprehensible rainbow mess to look at. This trivial method isn't simply extensible to cases with more than three images, and the poor readability with three images should be a hint that combining even more images will tend to further hinder readability anyway.
What if the images aren't the same size? Use imcrop(), padarray(), or imresize() to make them match. It is up to you to decide how the image content should be colocated.
A more general approach
If it's truly desired to handle more than three images, or if it's desired to use colors other than the simple RGB primaries, consider the following general method. This uses MIMT tools, since I'm not going to reinvent the wheel to do it with base MATLAB/IPT tools. MIMT is available on the File Exchange.
% three RGB images
A = imread('peppers.png');
s = size(A);
B = imresize(imread('football.jpg'),s(1:2));
C = imresize(imread('gantrycrane.png'),s(1:2));
% colormap
%cm = [1 0 0;0 1 0;0 0 1]; % same as prior method
cm = [0.667 1 0;0 0.667 1;1 0 0.667]; % or you can use anything else
% colorize the images
Ac = imblend(colorpict(s(1:2),cm(1,:)),rgb2gray(A),1,'multiply');
Bc = imblend(colorpict(s(1:2),cm(2,:)),rgb2gray(B),1,'multiply');
Cc = imblend(colorpict(s(1:2),cm(3,:)),rgb2gray(C),1,'multiply');
% blend the images
E = mergedown(cat(4,Ac,Bc,Cc),1,'screen');
clf; imshow(E)
Or for four images:
% four RGB images
A = imread('peppers.png');
s = size(A);
B = imresize(imread('football.jpg'),s(1:2));
C = imresize(imread('gantrycrane.png'),s(1:2));
D = imresize(imread('pears.png'),s(1:2));
% colormap
cm = [1 0 0;0.5 1 0;0 1 1;0.5 0 1];
% colorize the images
Ac = imblend(colorpict(s(1:2),cm(1,:)),rgb2gray(A),1,'multiply');
Bc = imblend(colorpict(s(1:2),cm(2,:)),rgb2gray(B),1,'multiply');
Cc = imblend(colorpict(s(1:2),cm(3,:)),rgb2gray(C),1,'multiply');
Dc = imblend(colorpict(s(1:2),cm(4,:)),rgb2gray(D),1,'multiply');
% blend the images
E = mergedown(cat(4,Ac,Bc,Cc,Dc),1,'screen');
clf; imshow(E)
Again, combining more than two images can quickly become visually problematic depending on the image content.
If we're already using MIMT, handling any size mismatch is easy enough to deal with if global alignment schemes are acceptable. In the prior examples, I explicitly resized each image to match A. By using imstacker(), the transformations are automatic, allowing user-defined padding, cropping, or scaling with specified or default gravity and padding options. Consider the case where all the images are presented in their original size, centered and padded to fit the geometry of the largest image.
% three RGB images (go ahead and convert to grayscale)
A = rgb2gray(imread('peppers.png'));
B = rgb2gray(imread('football.jpg'));
C = rgb2gray(imread('gantrycrane.png'));
% scale/crop/pad/align as desired (see help imstacker)
S = imstacker({A,B,C},'gravity','center','padding',0);
% colormap
cm = [0.667 1 0;0 0.667 1;1 0 0.667];
% colorize the images
s = imsize(S,2);
Ac = imblend(colorpict(s,cm(1,:)),S(:,:,:,1),1,'multiply');
Bc = imblend(colorpict(s,cm(2,:)),S(:,:,:,2),1,'multiply');
Cc = imblend(colorpict(s,cm(3,:)),S(:,:,:,3),1,'multiply');
% blend the images
E = mergedown(cat(4,Ac,Bc,Cc),1,'screen');
imshow(E)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!