How to overlap more than two images into a one image with Matlab?
24 views (last 30 days)
Show older comments
Hello,
I need to overlap more than two images into a one image with Matlab. I used the below code for two images. It's worked.
But it doesn't work for three images. Please help me to overlap more than two images.
%for two images
A = imread('152.bmp');
B = imread('006.bmp');
%B = imrotate(A,5,'bicubic','crop');
D = imfuse(A,B,'blend','Scaling','joint');
imshow(D)
imwrite(D,'my_blend_overlay.bmp');
%for three images
%{
A = imread('152.bmp');
B = imread('006.bmp');
C = imread('151.bmp');
D = imfuse(A,B,C,'blend','Scaling','joint');
imshow(D)
imwrite(D,'my_blend_overlay.bmp');
%}
0 Comments
Answers (1)
DGM
on 19 Jun 2022
Imfuse() is a simple tool for visually comparing two images. It has no utility for similar tasks involving more than two images. It does not support more than two image arguments, and trying to apply it iteratively will produce nonsense.
For example, say you have a set of four simple RGB images:
% however many images
Aor = imread('squig_or.png');
Acy = imread('squig_cy.png');
Amg = imread('squig_mg.png');
Ard = imread('squig_rd.png');
imstack = {Aor,Acy,Amg,Ard};
montage(imstack) % show the images as a montage
When using the 'blend' option, the result is the simple arithmetic mean of the images -- i.e. the weights of the two images are both 50%. If you tried to iteratively apply imfuse(), the weights will become progressively smaller. The last image added to the composition will always have the largest weight. This is almost certainly an unwanted outcome.
% weight of Ard = 0.5
% weight of Amg = 0.25
% weight of Aor,Acy = 0.125
outpict = imstack{1};
for f = 2:numel(imstack)
% scaling option does not apply when using 'blend' with RGB inputs
outpict = imfuse(outpict,imstack{f},'blend');
end
imshow(outpict)
On the other hand, if you simply take the average of all the images, then you end up with equal weights.
% however many images
Aor = imread('squig_or.png');
Acy = imread('squig_cy.png');
Amg = imread('squig_mg.png');
Ard = imread('squig_rd.png');
% assuming all images have the same size and class
outpict = cat(4,Aor,Acy,Amg,Ard);
outpict = cast(mean(outpict,4),class(Aor));
% each image has a weight of 0.25
imshow(outpict)
Lastly, the use of the joint scaling option has no influence when using the 'blend' method with RGB images. If the images were grayscale and you actually wanted to replicate the behavior, then you could do something like this:
% however many images
Aor = rgb2gray(imread('squig_or.png'));
Acy = rgb2gray(imread('squig_cy.png'));
Amg = rgb2gray(imread('squig_mg.png'));
Ard = rgb2gray(imread('squig_rd.png'));
% assuming all images have the same size and class
outpict = cat(4,Aor,Acy,Amg,Ard);
outpict = im2uint8(mean(mat2gray(outpict),4));
imshow(outpict)
The fact that the images have been arranged into a 4D stack means that joint normalization is as simple as normalizing the stack itself with mat2gray().
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!