How to find pixel counts from an Histogram for 2D uint16 images

5 views (last 30 days)
I have double MR image (2D uint16), I have plotted hitogram of my original image and I want to find the Otsu threshold level and display it on the histogram. There is my code : How could I correct it please. Thank you.
subplot(2, 4, 2);
h=histogram(grayImage);
title('Histogram of Original Image');
thresholdLevel = graythresh(grayImage);
y = ylim();
line(thresholdLevel, y,'Color', 'r');
grid on;

Accepted Answer

Guillaume
Guillaume on 18 Jan 2018
You made two mistakes:
  • because you pass a 1x1 x value and a 1x2 y value to line, it actually plots two points, not a line. One point at (thresholdLevel, y(1)) the other at (thresholdLevel, (y(2)).
  • graythresh return a normalised threshold in the range [0 1], so to plot your line on your histogram you need to scale the threshold back to the intensity range of your image
So the fix:
line(repelem(threshold * intmax('uint16'), 2), y, 'Color', 'r')

More Answers (1)

Image Analyst
Image Analyst on 18 Jan 2018
Try this:
h=histogram(grayImage);
title('Histogram of Original Image');
thresholdLevel = graythresh(grayImage)
% Convert to an actual gray level.
thresholdLevel = thresholdLevel * intmax(class(grayImage))
line([thresholdLevel, thresholdLevel], ylim, 'Color', 'r', 'LineWidth', 2); % Plot vertical line.
grid on;
% Threshold the image.
binaryImage = grayImage > thresholdLevel;
  8 Comments
MMSAAH
MMSAAH on 22 Jan 2018
Sorry! but why to use the thresholdLevel while I 'm not going to use it to have the binary image ?
Guillaume
Guillaume on 22 Jan 2018
Note that there shouldn't be any difference difference between using the threshold returned by graythresh and calling imbinarize with no options, since both end up calling otsuthresh, using 256 bins, after having converted the image to uint8.
The problem with using
thresholdLevel = thresholdLevel * intmax(class(grayImage))
is that it is only valid for unsigned classes (which was the case in the original question, but no longer is for the dicom image given). For signed classes, the formula should be:
classrange = [intmin(class(grayImage)), intmax(class(grayImage))];
thresholdLevel = thresholdLevel*diff(classrange) + classrange(1);
That is, for int16 images the threshold is between -32768 and 32767, not 0 and 32767.
Note 2: you should remove the metadata from the images you post here, particularly patient information, unless it's made up.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!