Merging more than two images in Matlab

In MATLAB, how do I merge more than two images? For example, I want to do what it imfuse

does, but for more than two images. Using two images, this is the code I have:

A = imread('file1.jpg');
B = imread('file2.jpg');

C = imfuse(A,B,'blend','Scaling','joint'); 

      

C

will be a smooth version of A

and B

. I have about 50 images. How do you achieve this?

+3


source to share


3 answers


You can write a loop for

and then just have one image that stores all the fused results and re-merge that image with the new image you are reading. Thus, let your images be named from file1.jpg

before file50.jpg

. You can do something like this:

A = imread('file1.jpg'); 
for idx = 2 : 50
    B = imread(['file' num2str(idx) '.jpg']); %// Read in the next image
    A = imfuse(A, B, 'blend', 'Scaling', 'joint'); %// Fuse and store into A
end

      



What the above code will do is that it will re-read on the next image and merge it with the image saved in A

. On each iteration, it will take what is currently in A

, pin it with a new image, and then save it in A

. Thus, as we continue to read the images, we will continue to fuse new images on top of those that were previously fused. After completing the loop for

, you will have 50 images that are all merged together.

+4


source


imfuse

uses the method 'blend'

to alpha-blend two images. In the absence of an alpha channel in images, this is no more than the arithmetic average of each pair of corresponding pixels. Therefore, one way to interpret the fusion of N images is to simply average the N corresponding pixels, one from each image, to produce an output image.

Assuming that:

  • All images are sized imgSize

    (eg [480,640])
  • All images have the same range of pixel values ​​(e.g. 0-255 for uint8 or 0-1 for double)


the following should give you something sane:

numImages = 50;
A = zeros(imgSize,'double');

for idx = 1:numImages
    % Borrowing rayryeng nice filename construction
    B = imread(['file' num2str(idx) '.jpg']); 
    A = A + double(B);
end

A = A/numImages;

      

The result will be in an array of A

type double

after the loop and you may need to revert it to the appropriate type for your image.

+1


source


Pigging bearing on Rayryeng's solution: What you want to do is increase the alpha at each step in proportion to how much this image contributes to the images already saved. For example:

By adding 1 image to 1 existing image, you need an alpha of 0.5 to make them equal.

Now by adding one image to 2 existing images, it should contribute 33% of the image and therefore an alpha of 0.33. 4th image should contribute 25% (alpha = 0.25), etc.

The result follows the trend x^-1

. So in the 20th image 1/20 = 0.05

, so an alpha of 0.05 is needed.

0


source







All Articles