Reputation: 77
I need to fuse (overlay) 6 screenshots from a video. I am using MATLAB to do this. However, only the last 3 images are showing clearly, while the others are faded out considerably (see image below). The other forum posts do not deal with this issue:
I am using the code below:
A = imread('1.jpg');
for idx = 2:1:6
B = imread([num2str(idx) '.jpg']); % Read in the next image
A = imfuse(A, B, 'blend', 'Scaling', 'joint'); % Fuse and store into A
end
imshow(A)
imwrite(A, 'output.jpg')
Any help would be greatly appreciated.
Upvotes: 1
Views: 900
Reputation: 125874
It looks like you have a set of images in which the background stays fixed but an object moves through it, and you want to create a composite where every position of the object is displayed simultaneously in one image.
The imfuse
function is not what you want for this, as you've discovered. It is basically averaging each image into the previous ones. The first two images are averaged, resulting in the two appearances of the object being blended in such a way that they are displayed at an effective alpha transparency of 0.5 each, with the background showing through. When you average in the next image, that appearance of the object has an effective alpha transparency of 0.5, but the two previous appearances are halved to 0.25 each. This is why the first objects are so faded out.
What you want to do instead is mask out the areas of the images where differences occur (i.e. where the object appears) and use these masks to overlay the object at its different positions in one image. In order to do this, you'll ideally want a blank image with no object in it. If you don't have one, you can potentially generate one from your object images (assuming the object doesn't sit in the same position for a majority of the images). The code below should work for both grayscale and Truecolor RGB images.
I'll start by generating a set of sample images: four copies of the default MATLAB cameraman.tif
image each with a different colored box in each corner:
img = imread('cameraman.tif'); % Load the image
imgSet = cat(4, img, img, img, img); % Concatenate 4 copies of the image
imgSet(20:40, 20:40, :, 1) = 255; % White box in the upper left
imgSet(20:40, 216:236, :, 2) = 0; % Black box in the upper right
imgSet(216:236, 20:40, :, 3) = 127; % Gray box in the lower left
imgSet(216:236, 216:236, :, 4) = randi(255, [21 21], 'uint8'); % Random values in the
% lower right
We can generate a blank image from this (i.e. reconstitute the original) with the mode
function:
blankImage = mode(imgSet, 4);
And here's the code to mask each image by taking the difference with the blank image, then extract each masked region and add it to a final composite image:
sumImage = imgSet(:, :, :, 1);
for iImage = 2:size(imgSet, 4),
temp = imgSet(:, :, :, iImage);
mask = repmat(any((blankImage ~= temp), 3), [1 1 size(blankImage, 3)]);
sumImage(mask) = temp(mask);
end
imshow(sumImage);
And here's the result, with each box clearly visible (no blending):
The benefit of this solution is that it doesn't require the Image Processing Toolbox. It's just native MATLAB code.
Upvotes: 1