Quantcast

Using Opencv Lib with MicroManager plugin

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Using Opencv Lib with MicroManager plugin

Vikram Kopuri
Hi Everyone ,

I've been working on a MicroManager plugin that uses Camera images for XY tracking. I found OpenCV has extensive libraries for tracking (like the phaseCorrelate and Optical flow ones like Lucas Kanade).
I'm also finding opencv functions(like Canny Edge Detector for finding edges ) seem to run faster. than imagej or micro manager equivalents.

In this email I'm sharing what I had to do to get OpencV libraries working in a MicroManager plugin , incase others find it useful. Also to solicit feedback and advice on a better and safer methods.

I'm working with Opencv 2.4.11 as it seems to work with Java Compiler 1.6 , the newer Opencv 3.0 need complier 1.7 or newer I think.

I started out with open the Opencv Java eclipse tutorial , http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html#java-eclipse

First you get opencv 2.4 and install it , then in eclipse you refer to opencv-2411.jar and the dll opencv-2411.dll either the x64 or x86. Then in the code before using any Opencv function , use the syntax "System.loadLibrary( Core.NATIVE_LIBRARY_NAME );" To load the libraries.

Outside the Eclipse debug environment , this didn't work for me . Kept getting the "java.lang.UnsatisfiedLinkError" error.

To work around this , what I needed to export my project as a "Runnable Jar" or "fat Jar" with "Extract required lib into generated jar" selected. And copy the opencv-2411.dll in "C:\Windows\System32" (because the dll has to be in a java library path) or instead of "System.loadLibrary(lib_name)" use the "java.lang.Runtime.getRuntime().load(path of the dll)" instead because it accepts the path of the dll directly , and doesn't care if the path part of the library or not.

Couple of problems with this workaround is that MicroManager plugins are not fat jars and a fat jar won't work. Next is when I deploy the plugin I want it to load the correct dll (x64 or x86 correctly).

To work around this . What I needed to do was , copy the opencv/build/java folder which just has the opencv-2411.jar and the opencv-2411.dll to micromanager plugin folder like here , "C:\Program Files\Micro-Manager-1.4\plugins\opencv_2411"

Then to load the correct dll , I found some code on stack overflow http://stackoverflow.com/a/18780559 , and I modified it a little to work for my case. Below is my modified code to load the correct opencv dll based on architecture.


String osName = System.getProperty("os.name");
if(osName.startsWith("Windows")){
int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
ReportingUtils.logMessage("OS is windows");
if(bitness == 32){
ReportingUtils.logMessage("32 bit detected");
//the run time method is more thread safe, and it seems to ignore repeated reloads
java.lang.Runtime.getRuntime().load("C:/Program Files/Micro-Manager-1.4/plugins/opencv_2411/x86/opencv_java2411.dll");


}
else if (bitness == 64){
ReportingUtils.logMessage("64 bit detected");
java.lang.Runtime.getRuntime().load("C:/Program Files/Micro-Manager-1.4/plugins/opencv_2411/x64/opencv_java2411.dll");

}
else{
ReportingUtils.logMessage("Cannot figure out os bit , so loading 32 bit");
java.lang.Runtime.getRuntime().load("C:/Program Files/Micro-Manager-1.4/plugins/opencv_2411/x86/opencv_java2411.dll");
}
}
else{ //else if(osName.equals("Mac OS X")){
ReportingUtils.logMessage("!!!!! Non Windows OS not supported !!!!!");
}


} catch (Exception e) {
ReportingUtils.logMessage("!!!!!opencv code library failed to load.!!!!!" + e);
}


Question for the pros , is there a way from inside the MicroManager plugin to figure out MicroManager install path "C:/Program Files/Micro-Manager-1.4" . If I can deduce that automatically , then deploying it becomes very easy. All I have to tell the user is , put the plugin jar in \Micro-Manager-1.4\mmplugins and copy the opencv_2.4.11 jar and dll to Micro-Manager-1.4\plugins and it works.

I tested it on a few computers , just need the opencv-2411.jar and the right opencv-2411.dll . Users don't need to download the entire opencv library or add any additional folder to system path. And I export my MicroManager plugin just as a regular jar.

Let me know if i'm doing anything unsounds. If there is a way to figure out MicroManager install path that would be very useful to me. Like to also hear your thoughts on using opencv and micromanager together, has anyone done this before , I would like to learn more.

Thanks.



With Regards

Vikram Kopuri
Electronic Engineer | Applied Scientific Instrumentation , Eugene OR, USA | +1 541-461-8181 x119


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Opencv Lib with MicroManager plugin

Nico Stuurman-2
Hi Vik,


> In this email I'm sharing what I had to do to get OpencV libraries working in a MicroManager plugin , incase others find it useful. Also to solicit feedback and advice on a better and safer methods.
Thanks for sharing!  It is a pitty that the data have to go from the C++
layer in the core, to the Java layer, than back to OpenCV (C++).  There
used to be ImageProcessors in the C++ layer, but those were never
developed further, and I don't think that there would be a clean way to
feed-back results of the processing, so your approach is definitely the
most straight forward.

> Question for the pros , is there a way from inside the MicroManager plugin to figure out MicroManager install path "C:/Program Files/Micro-Manager-1.4" .

Does:

System.getProperty("user.dir");

work?  It returns the correct path for me.

Best,

Nico



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Using Opencv Lib with MicroManager plugin

Vikram Kopuri
Hi Nico ,

"System.getProperty("user.dir");"  worked perfectly :) Thanks a lot.

Vikram
Loading...