Automate threshold value calculation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Automate threshold value calculation

kirill8910
Hello!

 Could you help me..

Is it possible to make thresholding operation calculate threshold value automatically for different histograms.

For example I need to have a threshold value calculated = intensity value corresponded to 2% of max counts value in certain histogram.




Thank you!

Kirill
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

Winfried Wurm
Hello Kirill

I´ve had an similiar problem last week, and wrote a little Marco for it. You
can use the getHistogram function.
If I´ve got your Problem right something like this should help you:


image = getImageID;
getHistogram(values, counts, 256);  // I assumed from your Histogram that
you are working with 8-Bit pictures. If not, the last number must be
corected.
maxcount=0;

for(i=0;i<256;i++)
{
    if (maxcount<counts[i])
    {
        maxcount=counts[i];
        maxval=i;
    }
}

val=(maxval*0,02);
setThreshold(val,maxval);


Hope that will solve your Problem.

- Winfried


-----Ursprüngliche Nachricht-----
From: kirill8910
Sent: Sunday, January 15, 2012 4:20 PM
To: [hidden email]
Subject: Automate threshold value calculation

Hello!

Could you help me..

Is it possible to make thresholding operation calculate threshold value
automatically for different histograms.

For example I need to have a threshold value calculated = intensity value
corresponded to 2% of max counts value in certain histogram.

http://imagej.588099.n2.nabble.com/file/n7189903/Histogram.png


Thank you!

Kirill

--
View this message in context:
http://imagej.588099.n2.nabble.com/Automate-threshold-value-calculation-tp7189903p7189903.html
Sent from the ImageJ mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

kirill8910
Hello, Winfried!

Thousands Thanks for help! The macro seems to be working. But one thing, I'm trying to get 0.02 from maxcounts [i] value. And my attempts are not correct.

image = getImageID;
getHistogram(values, counts, 256);  

maxcount=0;

for(i=0;i<256;i++)
{
    if (maxcount<counts[i])
    {
        maxcount*0.02=counts[i]; // that is wrong here of corse.. but can it be set somehow?
        val=i;
    }
}


run("Threshold...");

setThreshold(0,val);


Thank you!

Kirill
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

Gabriel Landini
In reply to this post by kirill8910
On Sunday 15 Jan 2012 20:29:18 Winfried Wurm wrote:

> I´ve had an similiar problem last week, and wrote a little Marco for it. You
> can use the getHistogram function.
> If I´ve got your Problem right something like this should help you:
>
>
> image = getImageID;
> getHistogram(values, counts, 256);  // I assumed from your Histogram that
> you are working with 8-Bit pictures. If not, the last number must be
> corected.
> maxcount=0;
>
> for(i=0;i<256;i++)
> {
>     if (maxcount<counts[i])
>     {
>         maxcount=counts[i];
>         maxval=i;
>     }
> }
>
> val=(maxval*0,02);
> setThreshold(val,maxval);

That cannot be right. The macro is assigning a greyscale value which is a
fraction of the gresycale value at which the mode occurs...
That approach can only return a number between 0 and 5.1, so it would not be  
very useful.

I understood that the original post required to compute the value at which the
fraction of the histogram which is 0.02 of the maxcounts.
That would mean to compute the sum of pixels in each bin starting from 0
(the cumulative distribution) and stop (threshold) when one reaches 0.02 of
the max count.

Also "0,02" in the macro above is not a number, but two. This should be 0.02.

Cheers
Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

kirill8910
Thanks a lot, Gabriel!

But is it the getHistogram function as well?

Kirill
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

Gabriel Landini
On Monday 16 Jan 2012 15:03:01 you wrote:
> Thanks a lot, Gabriel!
>
> But is it the getHistogram function as well?

Yes you use the same function to get the histogram but it is not clear what is
exactly you are trying to compute.

Is it the fraction of the histogram that accounts for at least 0.02 of the
mode frequency?

If so you need to modify this as:
//---------------8<----------------
getHistogram(values, counts, 256);
maxcount=0;

for(i=0;i<256;i++){
    if (maxcount<counts[i])     {
        maxcount=counts[i];
        maxgrey=i;
    }
}

val=(maxcount * 0.02);//this is the 0.02 of the mode

//find the threshold
j=-1;
cumulative=0;

while (cumulative<val) {
  j++;
 cumulative+=counts[j];
}

setThreshold(0,j);
print ("Mode: "+maxgrey+ ", frequency: "+maxcount+", fraction: "+val+",
threshold at: "+j+ ", integral: "+cumulative);

//---------------8<----------------

Be aware that the last line might be cut by the mailer.

As you can see, the threshold is the range that accounts for at least the 0.02
of the mode frequency. (you can't get exactly the same value of "fraction" and
"integral" as the bins are discrete).

Cheers
Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

Winfried Wurm
In reply to this post by kirill8910
Yes, youre Right Gabriel. I missunterstood the Problem and thougth he wanted 2% of the Greyvalue of the MaxCount...




-----Ursprüngliche Nachricht-----
Von: "Gabriel Landini" <[hidden email]>
Gesendet: 16.01.2012 17:41:52
An: [hidden email]
Betreff: Re: Automate threshold value calculation

>On Monday 16 Jan 2012 15:03:01 you wrote:
>> Thanks a lot, Gabriel!
>>
>> But is it the getHistogram function as well?
>
>Yes you use the same function to get the histogram but it is not clear what is
>exactly you are trying to compute.
>
>Is it the fraction of the histogram that accounts for at least 0.02 of the
>mode frequency?
>
>If so you need to modify this as:
>//---------------8<----------------
>getHistogram(values, counts, 256);
>maxcount=0;
>
>for(i=0;i<256;i++){
> if (maxcount<counts[i]) {
> maxcount=counts[i];
> maxgrey=i;
> }
>}
>
>val=(maxcount * 0.02);//this is the 0.02 of the mode
>
>//find the threshold
>j=-1;
>cumulative=0;
>
>while (cumulative<val) {
> j++;
> cumulative+=counts[j];
>}
>
>setThreshold(0,j);
>print ("Mode: "+maxgrey+ ", frequency: "+maxcount+", fraction: "+val+",
>threshold at: "+j+ ", integral: "+cumulative);
>
>//---------------8<----------------
>
>Be aware that the last line might be cut by the mailer.
>
>As you can see, the threshold is the range that accounts for at least the 0.02
>of the mode frequency. (you can't get exactly the same value of "fraction" and
>"integral" as the bins are discrete).
>
>Cheers
>Gabriel


___________________________________________________________
SMS schreiben mit WEB.DE FreeMail - einfach, schnell und
kostenguenstig. Jetzt gleich testen! http://f.web.de/?mc=021192
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

kirill8910
In reply to this post by Gabriel Landini
Sorry if I didn't make myself clear. Yes, that sounds right now.

But I'm trying the plugin now and something doesn't work.
It thresholds at 0 only not at needed intensity value. When I put another value (2 or 4 for example) instead of 0.02 it thresholds at somewhere around 100 fraction value.  

Could you tell me please why specify maxgrey=i and j=-1?

Thanks again

Kirill
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

Gabriel Landini
On Monday 16 Jan 2012 21:55:57 you wrote:
> But I'm trying the plugin now and something doesn't work.
> It thresholds at 0 only not at needed intensity value. When I put another
> value (2 or 4 for example) instead of 0.02 it thresholds at somewhere around
> 100 fraction value.

If you put 2 then it will threshold at the cumulative value of of twice the
frequency of the mode, but you specified 0.02. Maybe you get 0 because the
mode is small. I guess that you will also get 0 with nearly uniform
histograms.
As I said, I do not know if this is a sensible way to segment images, you
asked how to implement it.

> Could you tell me please why specify maxgrey=i and j=-1?

i is the index of the histogram. maxgrey gets updated each time there is a
larger frequency than before.

j is the counter of the array to integrate. So the first time it gets
increased its value is 0 (the first bin).
cheers

Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: Automate threshold value calculation

kirill8910
Thanks a lot Gabriel for responses and explanation! That is appreciated very much. And thanks Winfried for response and help!

17.01.2012, 02:47, "Gabriel Landini [via ImageJ]" <[hidden email]>:

> On Monday 16 Jan 2012 21:55:57 you wrote:
>> But I'm trying the plugin now and something doesn't work.
>> It thresholds at 0 only not at needed intensity value. When I put another
>> value (2 or 4 for example) instead of 0.02 it thresholds at somewhere around
>> 100 fraction value.
>
> If you put 2 then it will threshold at the cumulative value of of twice the
> frequency of the mode, but you specified 0.02. Maybe you get 0 because the
> mode is small. I guess that you will also get 0 with nearly uniform
> histograms.
> As I said, I do not know if this is a sensible way to segment images, you
> asked how to implement it.
>
>> Could you tell me please why specify maxgrey=i and j=-1?
>
> i is the index of the histogram. maxgrey gets updated each time there is a
> larger frequency than before.
>
> j is the counter of the array to integrate. So the first time it gets
> increased its value is 0 (the first bin).
> cheers
>
> Gabriel
>
> ----------------------------------------
> If you reply to this email, your message will be added to the discussion below:
> http://imagej.588099.n2.nabble.com/Automate-threshold-value-calculation-tp7189903p7194409.html
> To unsubscribe from Automate threshold value calculation, click here.
> NAML