How to combine desired cropped image to another image?

조회 수: 7 (최근 30일)
Drov
Drov 2023년 2월 23일
댓글: Drov 2023년 2월 23일
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!

채택된 답변

Luca Ferro
Luca Ferro 2023년 2월 23일
편집: Luca Ferro 2023년 2월 23일
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
DGM
DGM 2023년 2월 23일
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.
Drov
Drov 2023년 2월 23일
Thanks a lot! I will try it.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

DGM
DGM 2023년 2월 23일
편집: DGM 2023년 2월 23일
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 2023년 2월 23일
You can adjust the AlphaData property of the image to make certain regions of the mask image transparent.

Image Analyst
Image Analyst 2023년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by