# Merging two images with background

21 views (last 30 days)
Guy Sivan on 22 Dec 2021
Edited: DGM on 2 Jan 2022
Hi i have two images that i want to combine, one of a background and one with an item.
I wat the item to be inside the background to feel a part of it. How can i do it
For example. I have a 2 pictures of the northen lights and a basketball. I want to put the basketball inside the lights picture and make it look like it belongs without the white edges(after i make it smaller - which i know how ) in order for it to suit the other image.
Is there a MATLAB's function that does this? What is the way?
Thanks

DGM on 22 Dec 2021
Edited: DGM on 22 Dec 2021
This should be a start
% resize fg
fg = imresize(fg,0.5);
sfg = size(fg);
fghsv = rgb2hsv(fg);
% compose the image
offset = [1000 500];
yrange = offset(1)+(1:sfg(1));
xrange = offset(2)+(1:sfg(2));
imshow(bg)
The lighting on the FG is obviously nonsense in the combined context, but I doubt realism is the goal here. You could go further if you wanted. For sake of my own convenience, I'm going to use MIMT tools here.
% resize fg
fg = imresize(fg,0.5);
sfg = size(fg);
fghsv = rgb2hsv(fg);
% use MIMT tools to fudge the lighting color lazily
fg = imflatfield(fg,50);
fg = imtweak(fg,'hsl',[0 0.5 1]);
fg = imblend(vol,fg,1,'overlay',1.5);
% compose the image
offset = [1000 500];
yrange = offset(1)+(1:sfg(1));
xrange = offset(2)+(1:sfg(2));
imshow(bg)
DGM on 2 Jan 2022
I don't know how you would reliably get a good mask using color-based segmentation with that image. As I mentioned, I just simply generated the mask manually. This can either be done in MATLAB using freehand or assisted freehand ROI tools:
imshow(mypicture); % display the image
R = drawfreehand(gca); % freehand selection
% R = drawassisted(gca); % or assisted freehand selection
%% and then when you're done editing the ROI
Or you can use any other image editing software to manually create the mask (attached).
Once the mask is obtained, the composition is similar.
% resize fg
rsfactor = 0.40;
fg = imresize(fg,rsfactor);
fg = fliplr(fg); % just so the lighting makes sense
sfg = size(fg);
% compose the image
offset = [1000 500];
yrange = offset(1)+(1:sfg(1));
xrange = offset(2)+(1:sfg(2));
imshow(bg)
Besides the need to import and resize the mask, I made a number of small changes. Since the goose has no text, I elected to flip it so that the lighting direction matches roughly. I didn't bother with the other lighting edits.
In the case of the basketball, I generated the mask by selecting the white background due to its uniformity. In the case of the goose, I selected the goose. In other words, the basketball composition used an inverted mask. Accordingly, the mask terms in the composition are now swapped. Similarly, the parameter used for imadjust is shifted by half.
In order to reduce the obvious transitions caused by hard-edged (logical) masking, the mask can be blurred slightly. However, it's often undesired to simply blur the mask, as that would allow a halo of background content to be included (e.g. a white ring around the basketball). The call to imadjust constricts the blurred mask such that it is effectively no larger than the logical mask. In other words, it's feathered only on one side. In the case of the goose mask, it's feathered on the inside (only the white region). Since the basketball mask is inverted, it's feathered on the outside (only the black region)