Use openCV within Jython macro

Tags: #<Tag:0x00007fb87ccb4148> #<Tag:0x00007fb87ccb4008>


I would like to use openCV in my Jython macros, just like I use it in Python scripts.
I have seen some java implementation of the library (JavaCV) and some ImageJ packages (CVForge, IJ-OpenCV) so I have kind of understood that I could use it by writting java plugin, but as I am not really familiar with Java I would prefer a Jython binding.
Still I have tried putting those jar file in my jython path and import them, but I always ran into some issue.
A detailled procedure would be highly appreciated.
Thank you very much !

Does ImageJ Utilize the GPU?

With the IJ-OpenCV-plugins update site enabled, you can convert your ImagePlus to an OpenCV Mat object as follows:

#@ ImagePlus imp

from ijopencv import ImageConverter

converter = ImageConverter()
m = converter.convertTo(imp)

print m

which will print something like:


When trying this, I noticed that the version of IJOpenCV.jar served by the update site seems to be different from the current master branch of Maybe @joheras can shed some light on this…

With the current version from the master branch, I’d expect something along these lines to work:

from ijopencv.ij import ImagePlusMatConverter
from ijopencv.opencv import MatImagePlusConverter
from org.bytedeco.javacpp.opencv_core import Mat

imp2mat = ImagePlusMatConverter()
mat2imp = MatImagePlusConverter()

m = imp2mat.convertTo(imp, Mat.class)

(taking Adaptive_ThresholdJ as an example)


Did it work in the end @imagejan or was there an issue?? Since this plugin is based on javacpp I’m curious whether it distributes it’s own version of javacpp or is compatible with the old version of javacpp that is distributed with Fiji.


I only tried the script above with the current version served by the update site, and that worked fine (i.e. the conversion; I didn’t test anything else).

Good point. AFAICT, the update site ships its own version of javacpp:

Again, I didn’t test anything else yet, sorry. But I agree it would be nice to avoid that update sites serve artifcacts shading older versions from the ImageJ and Java-8 update sites…


Thanks very much again Jan for your answer.
I was confused by the import statements, and by where to put the jar files.
So any jar in the plugin folder is accessible by Jython then ?

Still a bit puzzling I succesfully used the following line of import
from org.bytedeco.javacpp.opencv_imgproc import matchTemplate
Although when I browse the javacpp.jar I do find the folder structure org>bytedeco>javacpp but then where does the opencv_imgproc comes from ? same for opencv_core

Especially I am used to the dir() command to find out what is available in the module, but there this is not working, the only ouptut is the attribute _name_
import org.bytedeco.javacpp as javacpp
print dir(javacpp)
So I am wondering how to find the function I need… Is there an API reference ?


dir() is a python-specific command, OpenCV is written in C++, and javacpp bridged this to Java. I don’t know the details of how all this works, but I don’t find it that surprising that dir() doesn’t work when using C++ libraries from Java… Maybe others have experience with this and/or can suggest alternatives??

AFAICT, these are defined in the javacpp-presets project:

You might want to have a look at the javacpp-presets API here:

I’m not sure which version Fiji and/or the IJ-OpenCV update site are using though. @bnorthan might know.


Currently Fiji is shipping with javacpp-0.11, which seems to be a pretty old version. More info here