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.
FG = imread('cameraman.tif');
gradpict = lingrad([imsize(FG,2) 3],[0 1; 1 0],[0 0 1; 1 0 0]*255,'cosine','uint8');
BG = im2uint8(0.3 + freecb(imsize(FG,2),[10 10])*0.4);
BG = imblend(gradpict,BG,1,'overlay');
mask = radgrad(imsize(FG,2),[0.5 0.5],0.5,[255; 0]);
mask = imlnc(mask,'independent','in',[0 0.5],'k',2);
outpict = replacepixels(FG,BG,mask);
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:
BG = colorpict(imsize(FG,2),[100 10 60],'uint8');
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.