graythresh dosn't work with variables other than uint8?

4 次查看(过去 30 天)
Say I have a matrix B (some image):
B=[1 2 3 ;5 6 7 ; 10 11 12;14 56 45];
if the B is defined as uint8, graythresh returns a level:
level=graythresh(B)
ans =
0.1137
But, if B is defined as a uint16 or double (which the documentation says it supports), it returns 0.
B= uint16(B)
B =
4×3 uint16 matrix
1 2 3
5 6 7
10 11 12
14 56 45
level=graythresh(B)
level =
0
What is going on? Thank you.

采纳的回答

DGM
DGM 2024-10-8
Like many tools in IPT, graythresh() depends on the class of the image array to know what its scale is. When you change classes, the data must be correctly scaled. Unless you know the difference, don't use uint16() or double() to change image class. Use im2uint16() or im2double().
% a properly-scaled uint8 image
A = uint8([1 2 3 ; 5 6 7; 10 11 12; 14 56 45])
A = 4×3
1 2 3 5 6 7 10 11 12 14 56 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% the threshold in unit-scale
graythresh(A)
ans = 0.1137
Do the same thing in uint16. The result is the same.
% a properly-scaled uint16 image
B = im2uint16(A)
B = 4×3
257 514 771 1285 1542 1799 2570 2827 3084 3598 14392 11565
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% the threshold in unit-scale
graythresh(B)
ans = 0.1137
Casting without scaling means this image data is essentially black. The brightest pixel in this image is darker than 1LSB in uint8. Internally, the image is quantized to 256 levels for histogram generation. Since the entire image lies in the very bottom of the lowest bin, there's no histogram to split.
% a improperly-scaled uint16 image
% this is uint8-scale data in a uint16 container
B = uint16(A)
B = 4×3
1 2 3 5 6 7 10 11 12 14 56 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% so it gets interpreted completely wrong
graythresh(B)
ans = 0

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by