MATLAB: How can i overlay 2 pictures and then delete one part from one picture

colour segmentationImage Processing ToolboxmaskMATLABmelanomaoverlay

I have 2 images, one of a skin melanoma, and one of a mask of that melanoma. The mask contains black pixels for where the skin is, and white pixels for where the melanoma is. I want to find out how many colors are present in the melanoma and for this I want to place the 2 pictures on top of eachother. After that I want to delete the pixels representing the skin, so that the colors are only determined for the melanoma. How can I do this?
Thanks!

Best Answer

To mask the rgb image with your mask (binary image), do this:
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Then you can extract the individual color channels:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
and get histograms of each with imhist(). Be sure to zero out the first bin because it will be abnormally high because of so many zeros in the function. Or you can extract just the pixels in the mask
redPixels = redChannel(mask);
Related Question