필터 지우기
필터 지우기

Applying a circular mask to an image

조회 수: 48 (최근 30일)
Blake Mitchell
Blake Mitchell 2021년 9월 7일
편집: DGM 2023년 4월 1일
I would like to take an image (attached original image) and apply a gray circular mask that has a soft fade-out (see reference image). I am quite new to working with natural images. Any insight / starter code would be much appreciated!

채택된 답변

DGM
DGM 2021년 9월 7일
편집: DGM 2023년 4월 1일
There are other posts about how to composite two images (or an image and a flat BG)
I'm going to do this one using MIMT tools for my own convenience.
% load foreground image
FG = imread('cameraman.tif'); % raw single-channel source
% make colored checkerboard background
sout = size(FG);
gradpict = lingrad([imsize(FG,2) 3],[0 1; 1 0],[0 0 1; 1 0 0]*255,'cosine','uint8'); % colored gradient
BG = im2uint8(0.3 + freecb(imsize(FG,2),[10 10])*0.4);
BG = imblend(gradpict,BG,1,'overlay');
% create soft mask
mask = radgrad(imsize(FG,2),[0.5 0.5],0.5,[255; 0]); % linear radial gradient
mask = imlnc(mask,'independent','in',[0 0.5],'k',2); % levels and curves tool
outpict = replacepixels(FG,BG,mask); % apply mask
imshow(outpict);
Adjusting the contrast ('k') parameter for imlnc() will increase the hardness of the mask. This is for k = 6:
In the above cases, I reused the colored checkerboard BG from the other example. Let's say you want a solid color BG instead:
% inputs to replacepixels() may be either images or tuples
%BG = colorpict(imsize(FG,2),[100 10 60],'uint8'); % an image
BG = [0.4 0.04 0.25]; % a tuple
Of course, simple gray backgrounds are easy enough to make without tools.
Most of the tools used above are from MIMT, which is here:
If you need to know how they do what they do, just open up the files. In general, the main advantage to using things like replacepixels() is that it handles mismatches of class and number of channels, whereas naive multiplicative masking won't. Note that both these examples involve compositing a single-channel image with an RGB image using a single-channel mask.
  댓글 수: 1
Blake Mitchell
Blake Mitchell 2021년 9월 7일
Thanks, this is super helpful! I was able to create exactly what I needed.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by