Giter VIP home page Giter VIP logo

Comments (1)

ViriatoII avatar ViriatoII commented on June 24, 2024

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")

from labelstorois.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.