How to combine desired cropped image to another image?
Show older comments
Hello!
I want to crop only mask and wear on the face of image. I tried, but cropped mask image have other area (like white area). So, result like that in image3.
Can someone help me, please.
Thanks a lot!
Accepted Answer
More Answers (3)
Here's a start.
In this example, the white region in the FG image is removed using a very crude one-sided S threshold. This is likely adequate, though refining the selection can be done by adding constraints (upper/lower limits on H and/or V). I removed the straps, since they wouldn't be in the right place anyway.
The scale and offset can be adjusted, but I've made no attempt to safeguard against the FG region being placed outside the BG extents.
I performed the FG masking prior to resizing in an attempt to maximize the smoothness of the mask edges. Note that the mask is cast numeric prior to resizing.
% read the images
FG = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1304830/madical%20mask.jpg');
BG = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1304835/backham.jpg');
% geometry parameters
fgscale = 0.24;
fgoffset = [65 145]; % [y x]
% generate FG mask
fghsv = rgb2hsv(FG);
FGa = fghsv(:,:,2) > 0.02;
% rescale FG and FGa
FG = imresize(FG,fgscale);
FGa = imresize(im2double(FGa),fgscale);
% get insertion location subscripts
szfg = size(FGa);
xrange = fgoffset(2):fgoffset(2)+szfg(2)-1;
yrange = fgoffset(1):fgoffset(1)+szfg(1)-1;
% extract BG region, compose, and replace
bgsample = im2double(BG(yrange,xrange,:));
bgsample = FGa.*im2double(FG) + (1-FGa).*bgsample;
BG(yrange,xrange,:) = im2uint8(bgsample); % assumes BG is uint8
imshow(BG);
Theeere we go.
Categories
Find more on Image Arithmetic 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!