Reputation: 278
I have the images A, B and C. How to overlay these images to result in D using Matlab? I have at least 50 images to make it. Thanks.
Download images:
A: https://docs.google.com/open?id=0B5AOSYBy_josQ3R3Y29VVFJVUHc
B: https://docs.google.com/open?id=0B5AOSYBy_josTVIwWUN1a085T0U
C: https://docs.google.com/open?id=0B5AOSYBy_josLVRwQ3JNYmJUUFk
D: https://docs.google.com/open?id=0B5AOSYBy_josd09TTFE2VDJIMzQ
Upvotes: 0
Views: 16032
Reputation: 11785
Judging from the example, it seems to me that the operation requested is a trivial case of "alpha compositing" in the specified order.
Something like this should work - don't have matlab handy right now, so this is untested, but it should be correct or almost so.
function abc = composite(a, b, c)
m = size(a,1); n = size(a,2);
abc = zeros(m, n, 3);
for i=1:3
% Vectorize the i-th channel of a, add it to the accumulator.
ai = a(:,:,i);
acc = ai(:);
% Vectorize the i-th channel of b, replace its nonzero pixels in the accumulator
bi = b(:,:,i);
bi = bi(:);
z = (bi ~= 0);
acc(z) = bi(z);
% Likewise for c
ci = c(:,:,i);
ci = ci(:);
z = (ci ~= 0);
acc(z) = ci(z);
% Place the result in the i-th channel of abc
abc(:,:,i) = reshape(acc, m, n);
end
Upvotes: 0
Reputation: 2109
To fade the images together:
Well since images in matlab are just matrices, you can add them together.
D = A + B + C
Of course if the images don't have the same dimensions, you will have to crop all the images to the dimensions of the smallest one.
The more you apply this principle, the larger the pixel values are going to get. It might be beneficial to display the images with imshow(D, [])
, where the empty matrix argument tells imshow
to scale the pixel values to the actual minimum and maximum values contained in D
.
To replace changed parts of original image:
Create a function combine(a,b)
.
Pseudocode:
# create empty answer matrix
c = zeros(width(a), height(a))
# compare each pixel in a to each pixel in b
for x in 1..width
for y in 1..height
p1 = a(x,y)
p2 = b(x,y)
if (p1 != p2)
c(x,y) = p2
else
c(x,y) = p1
end
end
end
Use this combine(a,b)
function like so:
D = combine(combine(A,B),C)
or in a loop:
D = combine(images(1), images(2));
for i = 3:numImages
D = combine(D, images(i));
end
Upvotes: 4