clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
dicomImagePath = 'mr.jpg';
grayImage = imread(dicomImagePath);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage,[])
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(msgbox('Draw over the image'));
customROI = imfreehand(gca);
mask = customROI.createMask();
maskedImage = bsxfun(@times, grayImage, cast(mask, 'like', grayImage));
subplot(2, 2, 2);
imshow(maskedImage,[])
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2, 2, 3);
histogram(grayImage, 256, 'EdgeColor', 'none')
grid on;
title('Histogram of Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2, 2, 4);
histogram(maskedImage(mask), 256, 'EdgeColor', 'none')
grid on;
title('Histogram of Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer