Check for post-threshold binary image

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

Check for post-threshold binary image

Michael Doube
Hi all,

I'm looking for a short bit of Java that will check to see if an image
is binary (result of Adjust->Threshold->Apply) or not. Just checking bit
depth and making sure that pixel values are 0 or 255 is not quite enough
because

ImageStack.getPixels(int n);

returns an array of 0 (background) and -1 (foreground) elements, and I
use raw 0 and -1 values in my logic.

Cheers

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Check for post-threshold binary image

Gabriel Landini
On Tuesday 14 April 2009, Michael Doube wrote:

> Hi all,
>
> I'm looking for a short bit of Java that will check to see if an image
> is binary (result of Adjust->Threshold->Apply) or not. Just checking bit
> depth and making sure that pixel values are 0 or 255 is not quite enough
> because
>
> ImageStack.getPixels(int n);
>
> returns an array of 0 (background) and -1 (foreground) elements, and I
> use raw 0 and -1 values in my logic.

ImageStatistics stats;
stats=imp.getStatistics();
if (stats.histogram[0]+stats.histogram[255]!=stats.pixelCount){
                IJ.error("8-bit binary image (0 and 255) required.");
}
Reply | Threaded
Open this post in threaded view
|

Re: Check for post-threshold binary image

Gabriel Landini
In reply to this post by Michael Doube
On Tuesday 14 April 2009, Michael Doube wrote:
> Hi all,
>
> I'm looking for a short bit of Java that will check to see if an image
> is binary (result of Adjust->Threshold->Apply) or not. Just checking bit
> depth and making sure that pixel values are 0 or 255 is not quite enough
> because

Oops I sent the message before finishing reading that checking the histogram
is not enough...

Can't you check before the gePixels?

Cheers

G.
Reply | Threaded
Open this post in threaded view
|

Re: Check for post-threshold binary image

Michael Schmid
In reply to this post by Michael Doube
Hi Mike,

if your 8-bit images use ONLY pixel values of -1 and 0 they are  
indistinguishable from binary images obtained by 'apply threshold'.
(note that -1 is interpreted as 255 by ImageJ: it treats the image as  
unsigned bytes).

If you have also other pixel values, Gabriel's histogram-based method  
will work (the same code is in ij.plugin.filter.Binary).

Michael
________________________________________________________________

On 14 Apr 2009, at 16:13, Michael Doube wrote:

> Hi all,
>
> I'm looking for a short bit of Java that will check to see if an  
> image is binary (result of Adjust->Threshold->Apply) or not. Just  
> checking bit depth and making sure that pixel values are 0 or 255  
> is not quite enough because
>
> ImageStack.getPixels(int n);
>
> returns an array of 0 (background) and -1 (foreground) elements,  
> and I use raw 0 and -1 values in my logic.
>
> Cheers
>
> Mike
Reply | Threaded
Open this post in threaded view
|

Re: Check for post-threshold binary image

Michael Doube
Thanks everyone for your very quick responses.

Looks like I just have to use Gabriel's 'histogram method' and remember
to account for the signed<->unsigned byte issue.

Cheers

Mike

Michael Schmid wrote:

> Hi Mike,
>
> if your 8-bit images use ONLY pixel values of -1 and 0 they are  
> indistinguishable from binary images obtained by 'apply threshold'.
> (note that -1 is interpreted as 255 by ImageJ: it treats the image as  
> unsigned bytes).
>
> If you have also other pixel values, Gabriel's histogram-based method  
> will work (the same code is in ij.plugin.filter.Binary).
>
> Michael
> ________________________________________________________________
>
> On 14 Apr 2009, at 16:13, Michael Doube wrote:
>
>> Hi all,
>>
>> I'm looking for a short bit of Java that will check to see if an  
>> image is binary (result of Adjust->Threshold->Apply) or not. Just  
>> checking bit depth and making sure that pixel values are 0 or 255  
>> is not quite enough because
>>
>> ImageStack.getPixels(int n);
>>
>> returns an array of 0 (background) and -1 (foreground) elements,  
>> and I use raw 0 and -1 values in my logic.
>>
>> Cheers
>>
>> Mike

--
Dr Michael Doube  BPhil BVSc PhD MRCVS
Research Associate
Department of Bioengineering
Imperial College London
South Kensington Campus
London  SW7 2AZ
United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: Check for post-threshold binary image

Gabriel Landini
On Tuesday 14 April 2009, Michael Doube wrote:
> Thanks everyone for your very quick responses.
> Looks like I just have to use Gabriel's 'histogram method' and remember
> to account for the signed<->unsigned byte issue.

I am sure I must have pinched it from somewhere else (most likely from the IJ
code).
It is quite handy, though, because it can be easily implemented in a macro
too:

getHistogram(values, counts, 256);
for (i=1;i<255;i++) total+=counts[i];
if (total>0) exit("8-bit binary image (0 and 255) required.");

Cheers,

G.