ROI mask on RGB image to separate foreground from background and further working only on ROI

I am working in a study involving images, the first step is to separate the foreground from background, and do further analysis only with foreground not involving background. Then later using the foreground image I have to find the median value of the foreground pixels then rectify the pixels based on a median value. So what I have done so far is first creating a transparent background, and storing the image using imwrite. Later I used the drawfreehand function to separate the ROI from the transparent background. Now I want to look at the ROI in the old RGB. I don't know how to do that, I am very new to MatLab kindly help with the code.

My code

img = im2double(imread('alphaimg1.png'));
himage = imshow(img);
ROI = drawfreehand
img_mask = createMask(ROI, himage);
roi_img = img(img_mask);
% Maximg = max(roi_img);
% Minimg = min(roi_img);
hist(roi_img, 0:0.01:1);
%MedMask = median(roi_img);
dimg = img;             %must be double or single for this to work
dimg(~img_mask) = nan;
% maskimg = bw2rgb(roi_img);
fun = @(B) median('(B(:)');
medimg = cast(nlfilter(img_mask, [3 3],fun),class(img));
himage = imshow(medimg);
himage.AlphData = img_mask; 
imshow(himage.AlphaData)  

**It shows the following error** 

Unrecognized property 'AlphData' for class 'matlab.graphics.primitive.Image'.

Error in alphaimgtrial1 (line 80) himage.AlphData = double(img_mask);

Thanks Soma