Combine two images together
56 views (last 30 days)
Mohammed Al-Mostafa on 10 Dec 2017
i have two different images for the same wall , i want to combine both so the second one will be placed in the proper place over the first image as shown below. any idea which code shall i use ?,i used many all converts the images to Gray.
Michal Dobai on 11 Dec 2017
This is answer to your comment .
Problem with your code is, that you display only registered image (Ir). As Image Analyst suggested , you should first combine image A with registered image Ir.
So instead of:
you should do something like this:
% Calculate mask of images 1.jpg and 2.jpg
% (for this example, let's say that 'outside' black pixels are
% background pixels)
maskA = imfill(original ~= 0, 'holes');
maskB = imfill(rgb2gray(Ir) ~= 0, 'holes');
% Mask area where these 2 pictures overlap
overlapMask = maskA & maskB;
% ...and extend it to 3 dimensional RGB image
overlapMaskRGB = cat(3, overlapMask, overlapMask, overlapMask);
% Now, as Image Analyst suggested, sum pixels in non overlap regions
% and in overlap regions, compute the average value.
% You can, for example, sum both images, and in overlaped area divide it by 2
C = A+Ir;
C(overlapMaskRGB) = C(overlapMaskRGB)/2;
Result image is attached below. Now, as you can see, there is a little problem around the edges. (there are some dark pixels). That is because in this example we 'assume' that background of your input images is completely black (that means pixel values [0 0 0]). It doesn't have to be always true, especially for JPEG images, because of the compression artifacts.
You can use one of the image formats with lossless compression (for example png) for better results, or you can set the threshold and mask pixels darker than this threshold.
More Answers (1)
Image Analyst on 10 Dec 2017