Anoop
Anoop

Reputation: 5720

fusing more than 2 images in matlab

In MATLAB, how do I fuse more than two images? For example, I want to do what imfuse does but for more than 2 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 fused version of A and B. I have about 50 images to fuse. How do I achieve this?

Upvotes: 4

Views: 5886

Answers (3)

M Cross
M Cross

Reputation: 1

Piggy-backing on rayryeng's solution: What you want to do is increase the alpha at every step in proportion to how much that image is contributing to the already stored images. For Example:

Adding 1 image to 1 existing image, you would want an alpha of 0.5 so that they are equal.

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

The result follows an x^-1 trend. So at the 20th image, 1/20 = 0.05, so an alpha of 0.05 would be necessary.

Upvotes: 0

beaker
beaker

Reputation: 16810

imfuse with the 'blend' method performs alpha blending on two images. In the absence of an alpha channel on the images, this is nothing more than the arithmetic mean of each pair of corresponding pixels. Therefore, one way of interpreting the fusion of N images is to simply average N corresponding pixels, one from each image, to get the output image.

Assuming that:

  • All images are of size imgSize (e.g., [480,640])
  • All images have the same pixel value range (e.g., 0-255 for uint8 or 0-1 for double)

the following should give you something reasonable:

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

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

A = A/numImages;

The result will be in the array A with type double after the loop and may need to be cast back to the proper type for your image.

Upvotes: 2

rayryeng
rayryeng

Reputation: 104555

You could write a for loop, then simply have a single image that stores all of the fused results and repeatedly fusing this image with a new image you read in. As such, let's say your images were named from file1.jpg to file50.jpg. You could 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 repeatedly read in the next image, and fuse it with the image stored in A. At each iteration, it will take what is currently in A, fuse it with a new image, then store it back in A. That way, as we keep reading in images, we will keep fusing new images on top of those images that were fused from before. After this for loop finishes, you will have 50 images that are all fused together.

Upvotes: 4

Related Questions