Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run without GUI #10

Closed
ViriatoII opened this issue May 31, 2022 · 1 comment
Closed

Run without GUI #10

ViriatoII opened this issue May 31, 2022 · 1 comment

Comments

@ViriatoII
Copy link

Hello,

Is it possible to run this directly in python? In headless mode or perhaps being called as a macro like here:

https://forum.image.sc/t/accessing-adding-to-roi-manager-with-pyimagej-script/41505

Cheers,
Ricardo

@ViriatoII
Copy link
Author

Ok, got it. It's quite simple:

import imagej, scyjava

scyjava.config.add_option('-Xmx6g')

ij = imagej.init(mode='interactive')
imp = ij.IJ.openImage('INPUT_IMAGE.tiff')  

imp2 = imp.duplicate()
ip = imp2.getProcessor()
width = imp2.getWidth()
height = imp2.getHeight() - 1

RM = ij.RoiManager()        # we create an instance of the RoiManager class
rm = RM.getRoiManager()     # "activate" the RoiManager otherwise it can behave strangely

max_label = int(imp2.getStatistics().max)
max_digits = int(math.ceil(math.log(max_label,10))) # Calculate the number of digits for the name of the ROI (padding with zeros)
ij.IJ.setForegroundColor(0, 0, 0) # We pick black color to delete the label already computed

roiID = -1 

# Iterate over picture and identify pixel values
for j in range(height):
    for i in range(width):
        current_pixel_value = ip.getValue(i,j)
        if current_pixel_value > 0:
            roiID +=1 
            
            # Select all pixels with same value in image
            ij.IJ.doWand(imp2, i, j, 0.0, "Legacy smooth");

            # Add as ROI to the ROI manager
            roi = imp2.getRoi()
            
            # Set roiname to roiID_X_Y (center_point). 
            roi.setName(str(roiID) + "_" +                          #str(int(current_pixel_value)).zfill(max_digits)) # Old
                        str(round(roi.getXBase() + roi.getFloatWidth()/2)) + "_" +
                        str(round(roi.getYBase() + roi.getFloatHeight()/2)))
            rm.addRoi(roi)        #Error.. Static method was being called without being instantiated
    
            # Fill inner part of roi with 0s so it becomes an outline
            ip.setColor(0); # Fix 32 bit issue
            ip.fill(roi) # Much faster than IJ.run(imp2, "Fill", ....       
                 
rm.save("outputDIR/tmp.zip")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant