Azzera filtri
Azzera filtri

How to combine desired cropped image to another image?

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Luca Ferro
Luca Ferro il 23 Feb 2023
Modificato: Luca Ferro il 23 Feb 2023
the easy solution is to find a mask image with a transparent background (format files as .png and .svg support it).
the medium solution is to find a tool (i.e adobe photoshop) to remove the background, save the image as .png and then use it.
the hard solution is to remove the background by image processing it in matlab. You may want to check out here some solutions: Search results: remove background - MATLAB Answers - MATLAB Central (mathworks.com)
  2 Commenti
DGM
DGM il 23 Feb 2023
I agree. For something like this, grabbing a transparent PNG is going to be a lot simpler.
I don't think that there's an expectation that the composition needs to be good, and I don't doubt that there are many product images floating around that would be just fine.

Accedi per commentare.

Più risposte (3)

DGM
DGM il 23 Feb 2023
Modificato: DGM il 23 Feb 2023
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.

Matt J
Matt J il 23 Feb 2023
You can adjust the AlphaData property of the image to make certain regions of the mask image transparent.

Image Analyst
Image Analyst il 23 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by