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

histogram

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;

Best Answer

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')