Convert Threshold to Mask in Python Script Question

Hu guys,

I used a python script to threshold an image. The result is shown below. My next step would be to run the particle analyzer on this image, but is has to be converted to a thresholded image or 8-bit binary.

1st: Question: Why is the threshold image not already usable for the particle analyzer?
2nd question: If I use Process - Binary - Convert to Mask I get the result below, which does not really make sense to me. What is my mistake here?

To me it seems my threshold script is not really creating " thresholded image", because the pixel vales are either 0 or 12, but not 0 or 255. But I do not really know why, since the relevant line of code is

ip.threshold(lowth_corr)

and from the API documentation I read

`threshold(int level)`

Sets pixels less than or equal to level to 0 and all other pixels to 255.

image

image

image

There is something odd, your first image is “normal” greyscale, the second one has an inverted LUT. How is that possible?

Hi,

maybe you didn’t update the image processor of your image after thresholding? The little script below creates a mask of the blobs sample image as an example. I also added a few lines showing how to include particle analysis directly from the API in a script. Hopefully it helps:

#import packages 
from ij import IJ
from ij.measure import ResultsTable  
from ij.plugin.filter import ParticleAnalyzer

##load blobs image
IJ.run("Blobs (25K)");

##get the image processor, threshold it and update the image
imp = IJ.getImage()
ip = imp.getProcessor()
ip.threshold(100)
imp.setProcessor(ip)

##Particle analysis
new_table = ResultsTable()
#define the actions of the particle analyser
int_params = ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.DISPLAY_SUMMARY
int_params = int_params + ParticleAnalyzer.SHOW_RESULTS
#define what properties should be measured
int_measure = ParticleAnalyzer.AREA + ParticleAnalyzer.CIRCULARITY
#run the particle analyzer with size limits min=100px, max=10000000px
pa = ParticleAnalyzer(int_params, int_measure , new_table, 100, 10000000)
pa.analyze(imp);

Cheers,
Guillaume

Hi,

I tried that but still get stuck. If I take your code and apply it to my image I get a 16bit image with 0 or 255 as pixel value. See the image below. And the particle analyzer only accepts 8-bit images I converted it to 8-bit. See macro recorder. But this leaves mewith an 8bit image with pixel 0 or 12 …?

And I have no idea why? It should be 0 and 255.

And I also attached the original image.Osteosarcoma_01_DAPI.ome.tiff (3.9 MB)

I got i working now … The rescaling was the problem. When the image is 16bit and has values 0 or 255 the converion to 8bit has to cut the histogram without rescaling…

In retrospective it seems logical …

Sebi

#import packages 
from ij import IJ
from ij.measure import ResultsTable  
from ij.plugin.filter import ParticleAnalyzer
from ij.process import ImageProcessor, ImageConverter

##get the image processor, threshold it and update the image
imp = IJ.getImage()
ip = imp.getProcessor()
ip.threshold(300)
imp.setProcessor(ip)

# this must be done for a 16bit image
ip = ip.convertToByte(False)
imp.setProcessor(ip)

##Particle analysis
new_table = ResultsTable()
#define the actions of the particle analyser
int_params = ParticleAnalyzer.ADD_TO_MANAGER+ParticleAnalyzer.DISPLAY_SUMMARY
int_params = int_params + ParticleAnalyzer.SHOW_RESULTS
#define what properties should be measured
int_measure = ParticleAnalyzer.AREA + ParticleAnalyzer.CIRCULARITY
#run the particle analyzer with size limits min=100px, max=10000000px
pa = ParticleAnalyzer(int_params, int_measure , new_table, 100, 10000000)
pa.analyze(imp)