- 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
How to find pixel counts from an Histogram for 2D uint16 images
1 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Guillaume
2018-1-18
You made two mistakes:
So the fix:
line(repelem(threshold * intmax('uint16'), 2), y, 'Color', 'r')
0 个评论
更多回答(1 个)
Image Analyst
2018-1-18
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 个评论
Guillaume
2018-1-22
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!