Merging two images with background
55 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
DGM
on 22 Dec 2021
Edited: DGM
on 22 Dec 2021
This should be a start
fg = imread('bball.jpg');
bg = imread('lights.jpg');
% resize fg
fg = imresize(fg,0.5);
sfg = size(fg);
% create a feathered mask
fghsv = rgb2hsv(fg);
mask = fghsv(:,:,3).*(1-fghsv(:,:,2))>0.90;
mask = imadjust(imgaussfilt(im2double(mask),3),[0 0.5]);
% compose the image
offset = [1000 500];
yrange = offset(1)+(1:sfg(1));
xrange = offset(2)+(1:sfg(2));
bg(yrange,xrange,:) = im2uint8(im2double(fg).*(1-mask) + im2double(bg(yrange,xrange,:)).*mask);
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.
fg = imread('bball.jpg');
bg = imread('lights.jpg');
% resize fg
fg = imresize(fg,0.5);
sfg = size(fg);
% create a mask
fghsv = rgb2hsv(fg);
mask = fghsv(:,:,3).*(1-fghsv(:,:,2))>0.90;
mask = imadjust(imgaussfilt(im2double(mask),3),[0 0.5]);
% use MIMT tools to fudge the lighting color lazily
vol = radgrad(sfg,[0.5 0.3],0.7,[20 20 50; 169 250 89],'ease','uint8');
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));
bg(yrange,xrange,:) = im2uint8(im2double(fg).*(1-mask) + im2double(bg(yrange,xrange,:)).*mask);
imshow(bg)
5 Comments
DGM
on 2 Jan 2022
Edited: 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
mask = createMask(R); % create a logical mask from 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.
fg = imread('Goose-Bird.jpg');
bg = imread('lights.jpg');
% resize fg
rsfactor = 0.40;
fg = imresize(fg,rsfactor);
fg = fliplr(fg); % just so the lighting makes sense
sfg = size(fg);
% create a mask
mask = imread('goosemask.png');
mask = imresize(mask,rsfactor);
mask = fliplr(mask); % just so the lighting makes sense
mask = imadjust(imgaussfilt(im2double(mask),3),[0.5 1]);
% compose the image
offset = [1000 500];
yrange = offset(1)+(1:sfg(1));
xrange = offset(2)+(1:sfg(2));
bg(yrange,xrange,:) = im2uint8(im2double(fg).*mask + im2double(bg(yrange,xrange,:)).*(1-mask));
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.
Regarding the call to imadjust(), this line implements biased mask feathering.
mask = imadjust(imgaussfilt(im2double(mask),3),[0.5 1]);
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)
More Answers (0)
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!