The accepted answer by @Ratbert makes the incorrect claims that
The correct answer is the first one
and
graythresh
uses the min and max values in the image as boundaries,
which is the most logical behavior.
and rayryeng appears to agree with it. David Parks appears to have empirically verified it.
The correct answer is given by Anand which strangely seems to have a negative vote. He explains very convincingly that
full range of grayscale pixel values' depends on the data type of the input image
As he explains,
this is the third option
except for the fact that the dark
image could not possibly get a threshold of 0.75.
First let us clarify the difference between the claims for the simplest case, in clear MATLAB, so there is no confusion. For an image with values ranging from min
to max
, the question poses three possibilities which, when translated to an equation are:
threshold = min + (max-min) * graythresh
threshold = max * graythresh
threshold = 255 * graythresh
Suppose the image consists of just two points one with an intensity of 0, and the other with 100. This means dark = uint8([0 100]);
. A second image light = dark+155;
. When we compute 255*graythresh(dark)
we get exactly 49.5
. When we compute 255*graythresh(light)
we get exactly 204.5
. These answers make it patently obvious that the third option is the only possibility.
There is one further fine point. If you try 255*graythresh(uint8(1:2))
the answer is 1
, and not 1.5
. So it appears that if you are using greythresh
to threshold an image, you should use image <= 255*graythesh(image)
with a less-than-or-equal-to, rather than a plain less-than.
graythresh
by doingopen graythresh
in the MATLAB command prompt. For the dark images, 0.75 is 75% of the way between 0 - 100, and so 75 is the answer. For the light image, 75% of the way is155 + (255-155)*0.75 ~ 230
.... I upvoted Ratbert's answer. – rayryenggraythresh
returned a value relative to the range of its input, why woulda = graythresh(uint8([10 20]))
return a different value thanb = graythresh(uint8([20 30]))
? Look at what Ratbert suggests the threshold ofa
is:a * (20 - 10) + 10
, which is10.5686
, whereas Anand suggests that the threshold isa * 255
, which is14.5
. The second calculation makes a lot more sense! – Numeri