Jython imglib2 import error


I am facing a weird error where am unable to import componenttree library that exists on the imglib2 algorithm github page. In my jython script I am doing

from net.imglib2.algorithm.componenttree import mser

and I get an error message
Traceback (most recent call last):
  File "/Users/aimachine/IJCurieMacros/CountCells.py", line 17, in <module>
    from net.imglib2.algorithm.componenttree import mser
ImportError: No module named componenttree

	at org.python.core.Py.ImportError(Py.java:329)
	at org.python.core.imp.import_logic(imp.java:1290)
	at org.python.core.imp.import_module_level(imp.java:1369)
	at org.python.core.imp.importName(imp.java:1528)

But it is there so I do not understand why this does not work? I have bdv update site on so I should have latest imglib2 in my Fiji, hence I do not understand this error.

When I ask it to print(dir(net.imglib2)) and print(dir(net.imglib2.algorithm)) I get only a limited number of packages

['__name__', 'dog']
Started CountCells.py at Mon May 03 22:33:36 CEST 2021
['__name__', 'algorithm', 'converter', 'img', 'view']

This seems like a very limited version of imglib2 that I can work with. I cloned the repo and made the latest jar, is their any way I can use sys library to append and use functions from my local jar files in jython?

1 Like

Hi @kapoorlab,

I get the same error that you do, but this seems to work for me:

from net.imglib2.algorithm.componenttree.mser import Mser

Maybe the thing after import has to be a java class and not a package?
I don’t use jython much, so don’t know for sure.



You thanks @bogovicj for me that worked too, i needed this instead

from net.imglib2.algorithm.componenttree.mser import MserTree
1 Like

I used it to make this macro to do mser and dog detection together. Just missing a nice 3D overlay for this, I dont know if it is worth doing it this way as a proper code would be a much nicer way of doing this but hey!

#@ Float(label="Size(um)", required=true, value=4, stepSize=0.1) cell
#@ Float(label="Min intensity peak", required=true, value=5, stepSize=0.5) min_peak
#@ Float(label="Delta", required=true, value=5, stepSize=0.5) delta
#@ Float(label="minSize", required=true, value=5, stepSize=0.5) minSize
#@ Float(label="maxSize", required=true, value=500, stepSize=0.5) maxSize
#@ Float(label="maxVar", required=true, value=0.5, stepSize=0.1) maxVar
#@ Float(label="minDiversity", required=true, value=0.5, stepSize=0.1) minDiversity
#@ Boolean(label='Find Maxima', value=True) WhiteBackground
# Varun n Claudia 3D spot detection macro 

import net.imglib2.algorithm
from ij import IJ
from net.imglib2.img.display.imagej import ImageJFunctions as IJF
from net.imglib2.view import Views
from net.imglib2.converter import Converters
from net.imglib2.algorithm.dog import DogDetection
from net.imglib2.algorithm.componenttree.mser import MserTree
from net.imglib2.type.numeric.real import DoubleType
from jarray import zeros  
import math
from java.awt import Color
from ij.gui import PointRoi, OvalRoi , Overlay, EllipseRoi
from ij.plugin.frame import RoiManager
from ij.gui import WaitForUserDialog, Toolbar
from net.imglib2.view import Views
#remove all the previous ROIS
imp = IJ.getImage()
impdog = imp.duplicate()
rm = RoiManager.getInstance()
if not rm:
	rm = RoiManager()

#ask the user to define a selection and get the bounds of the selection
WaitForUserDialog("Select the area,then click OK.").show();
boundRect = imp.getRoi()

#open a XYZ image (split the channels for now)

cal = imp.getCalibration() # in microns

img = IJF.wrap(imp)

zero = img.randomAccess().get().createVariable()
overlay = Overlay()
overlaydog = Overlay()

if WhiteBackground:
   Type = DogDetection.ExtremaType.MINIMA
   darkToBright = False
   Type = DogDetection.ExtremaType.MAXIMA  
   darkToBright = True
if img.numDimensions() == 3:    

		dog = DogDetection(Views.extendMirrorSingle(img), img,
		                   [cal.pixelWidth, cal.pixelHeight, cal.pixelDepth],
		                   cell / 2, cell,
		                   min_peak, False,
elif img.numDimensions() == 2:  
       dog = DogDetection(Views.extendMirrorSingle(img), img,
		                   [cal.pixelWidth, cal.pixelHeight],
		                   cell / 2, cell,
		                   min_peak, False,

def getDeltaVariable( inputimg, delta ):
		a = inputimg.randomAccess()
		inputimg.min( a )
		deltaT = a.get().createVariable()
		deltaT.setReal( delta )
		return deltaT		                                      
newtree = MserTree.buildMserTree(img, getDeltaVariable( img, delta ), int(minSize), int(maxSize), maxVar,minDiversity,darkToBright )

rootset = newtree.roots()
rootsetiterator = rootset.iterator()
ellipselist = []

while rootsetiterator.hasNext():

			rootmser = rootsetiterator.next();

			if rootmser.size() > 0:

				meanandcov = [ rootmser.mean()[0], rootmser.mean()[1], rootmser.cov()[0],
						rootmser.cov()[1], rootmser.cov()[2] ]


for index in range( 0,len(ellipselist)):
				mean = [ ellipselist[index][0], ellipselist[index][1] ]
				cov = [ ellipselist[index][2], ellipselist[index][3],
						ellipselist[index][4] ];
				a = cov[0];
				b = cov[1];
				c = cov[2];
				d = math.sqrt(a * a + 4 * b * b - 2 * a * c + c * c)
				scale1 = math.sqrt(0.5 * (a + c + d)) * 3
				scale2 = math.sqrt(0.5 * abs((a + c - d))) * 3
				if scale1 > 0:
						theta = 0.5 * math.atan2((2 * b), (a - c))
						x = mean[0]
						y = mean[1]
						if(boundRect.contains(int(x), int(y))):
								dx = scale1 * math.cos(theta)
								dy = scale1 * math.sin(theta)
								oval = EllipseRoi(x - dx, y - dy, x + dx, y + dy, scale2 / scale1)
peaks = dog.getPeaks()

roi = OvalRoi(0, 0, cell/cal.pixelWidth, cell/cal.pixelHeight)  

p = zeros(img.numDimensions(), 'i')  

regionpeak = 0 
for peak in peaks:  
  # Read peak coordinates into an array of integers  
  if(boundRect.contains(p[0], p[1])):
      oval = OvalRoi(p[0] - 0.5 * cell/cal.pixelWidth, p[1] - 0.5 * cell/cal.pixelHeight,cell/cal.pixelWidth,  cell/cal.pixelHeight)
      regionpeak= regionpeak + 1
print ('Number of cells in region = ', regionpeak, 'Total cells in the image = ', len(peaks)) 

1 Like