Re: Getting the number of pixels which values are below a certain threshold

Posted by Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Getting-the-number-of-pixels-which-values-are-below-a-certain-threshold-tp3688975p3688978.html

Hi Stephan,

loops in the macro language are like in C or Java, except that you  
need no type declaration.

Also there was one problem with my idea: Using 'min, max' is correct  
for 32-bit images only (>90% of my images are 32 bit, so I did not  
think about this), and there was a 'minus 1' was missing).

So, without trying it for all image types...

//find number of pixels below a threshold
threshold=-500;
getStatistics(area, mean, min, max, std, histogram);
result=0;
for (i=0; i<lengthOf(histogram); i++) {
   if (bitDepth()==32)
     value=min+(max-min)*i/(lengthOf(histogram)-1);
   else
     value=calibrate(i);
   if (value<threshold) result+=histogram[i];
}
path=getDirectory("temp")+"ij_result.txt";
f = File.open(path);
print(f, result);


Note that your macro would print to the 'Log' window, you won't be  
able to access this from the shell or a Batch file. The macro above  
prints to a file in the temp directory (depending on the operating  
system, on Unix-like systems: /tmp/). Use
   print(getDirectory("temp"));
to see where it goes. You can also use getDirectory("home") or any  
other directory you like.

By the way, instead of calling ImageJ from the shell (or Batch file  
under Windows) for each file, it might be faster to create a file  
with all the filenames and have ImageJ open the file and process the  
complete list in a loop.


Michael
________________________________________________________________

On 2 Mar 2010, at 13:34, Stephan Bender wrote:

> Hi Michael,
>
> thanks for the advice. However, I still do not get it because of  
> the loop you describe.
>
> This is how I would start with the macro:
>
>    name = getArgument;
>    if (name=="") exit ("No argument!");
>    setBatchMode(true);
>    open(name);
>     // here goes the loop
>     print(getTitle+": "+result);
>
> Can you please help me with the loop?
>
> Thank you!
>
> Best regards,
>
> Stephan
>
> Am 02.03.2010 11:05, schrieb Michael Schmid:
>> Hi Stephan,
>>
>> this should be easy:
>>   getStatistics(area, mean, min, max, std, histogram);
>> In a 'for' loop, sum up all values in the histogram array from  
>> index 0 to
>>   floor(lengthOf(histogram)*(threshold-min)/(max-min))
>> where threshold is -500 in your case.
>> Write the result to a file:
>>  f = File.open(path);
>>  print(f, result);
>> in your shell script or batch file, you can read the value from  
>> the file.
>>
>>
>> Michael
>> ________________________________________________________________
>>
>> On 1 Mar 2010, at 18:18, Stephan Bender wrote:
>>
>>> Hello,
>>>
>>> I'm quite new to ImageJ, I hope someone can help me with this task:
>>>
>>> I need get the number of pixels which values are below a certain
>>> threshold. The image format is DICOM (16 bit), and I need the result
>>> on a Unix command line, so I guess I'll have to use a macro  
>>> function.
>>> A common image has appr. 60% of the pixel values from -2048 to -500,
>>> I need to know the exact number of pixels below -500.
>>>
>>> Thank you very much for your help!
>>>
>>> Best regards,
>>>
>>> Stephan