MATLAB: Creating an appropriate histogram from MR Image

dicomimage processingImage Processing Toolboximage segmentationimhistmriroi

I'm trying to plot a histogram for an ROI dicom image from an MRI. I'm doing this in order to try to threshold the image. I'm creating a mask from a free-drawn ROI and then plotting a histogram from it. Below is the code i'm using to create the histograms, as well as the images produced. My issue is essentially the x-axis and the number of bins used. First, the x-axis seems very excessive for the dicom image, as can be seen by the zoomed in image. Is there any way to restrict this to an appropriate range? Also, only when i manually write in a large number of bins does the histogram separate in a way that can be useful. Is there a way to do this automatically without a preset number?
CODE USED:
Image = dicomread(dicomImagePath);
figure
imshow(Image,[])
customROI = imfreehand(gca);
mask = customROI.createMask();
figure
imshow(maskedImage,[])
figure
imhist(maskedImage)
Zoomed in manually:
figure
imhist(maskedImage,10000)
Zoomed in manually:
figure
imhist(maskedImage,100000)
Zoomed in manually:

Best Answer

I think the problem is the huge counts at the first and last bins due to masking. Try it this way:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
dicomImagePath = 'mr.jpg';
grayImage = imread(dicomImagePath);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
subplot(2, 2, 1);
imshow(grayImage,[])
axis on;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
uiwait(msgbox('Draw over the image'));
customROI = imfreehand(gca);
mask = customROI.createMask();
% Mask the image using bsxfun() function
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);
% Get histogram of masked image, but only within the mask area.
histogram(maskedImage(mask), 256, 'EdgeColor', 'none')
grid on;
title('Histogram of Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');