Why does the "graythresh" function return 0 on input images of type "double"?
8 views (last 30 days)
Show older comments
MathWorks Support Team
on 23 Jan 2017
Commented: Anand
on 23 Jan 2017
The documentation for the "graythresh" function says that it accepts images of type "uint8", and of type "double".
For "uint8", the "graythresh" function works as expected, as follows :
>> I = imread('coins.png');
>> level = graythresh(I)
level =
0.4941
>>imshow(im2bw(I, level));
However, when the input image is of type "double", the level is 0 as follows :
>> I = imread('coins.png');
>> level = graythresh(double(I))
level =
0
Why does this happen even though the "graythresh" function can accept inputs of type "double"?
Accepted Answer
MathWorks Support Team
on 23 Jan 2017
The "graythresh" function assumes that if the input image is of type "double", then the image is scaled between 0 and 1.
So, in order to correctly use the "graythresh" function with "double" images, scale the input image between 0 and 1 as follows :
I = imread('coins.png');
dbI = double(I);
dbI = dbI/max(max(dbI));
level = graythresh(dbI);
imshow(im2bw(I, level));
1 Comment
Anand
on 23 Jan 2017
The alternate would be to use im2double instead of just double. This function does the necessary scaling.
Also, imbinarize is the preferred function to use instead of im2bw.
I = imread('coins.png');
dbI = im2double(I);
level = graythresh(dbI);
imshow(imbinarize(I, level))
In fact, you don't even need to separately call graythresh. The imbinarize function does this internally.
I = imread('coins.png');
dbI = im2double(I);
imshow(imbinarize(I));
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!