Why does the "graythresh" function return 0 on input images of type "double"?

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"?

 Akzeptierte Antwort

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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by