OpenCV comes with Java bindings since version 2.4.4.
The OpenCV 2.4.8 distribution for Windows contains pre-compiled jar and dll files for the Java module so there is actually no need to build this module from source. These files are located in opencv/build/java/. All you need to do is import opencv-248.jar into your Java application and point to appropriate (32 or 64 bit) dll using System.load(). If you just want to know how to run an OpenCV application in Java then you can skip the build sections of this post.
But if you are interested in building the Java module then read on from the next section below.
The OpenCV 2.4.8 distribution for Windows contains pre-compiled jar and dll files for the Java module so there is actually no need to build this module from source. These files are located in opencv/build/java/. All you need to do is import opencv-248.jar into your Java application and point to appropriate (32 or 64 bit) dll using System.load(). If you just want to know how to run an OpenCV application in Java then you can skip the build sections of this post.
But if you are interested in building the Java module then read on from the next section below.
Software used
OpenCV 2.4.8 Windows distribution.
CMake 2.8.12.1 (latest as of 06 Jan 2014) - to configure and create the build files.
CMake 2.8.12.1 (latest as of 06 Jan 2014) - to configure and create the build files.
Visual Studio 2010 IDE - to build the OpenCV libraries.
NetBeans 7.3 IDE - to build and run the sample application.
Following packages are required to build the OpenCV Java module -
Java Development Toolkit (JDK) 1.7.0_45 64 bit (latest as of 04 Jan 2014)
Apache Ant 1.9.3 (latest as of 06 Jan 2014)
Python 2.7.1 (should be at least version 2.7)
The Operating System (OS) is Windows 7 Home Premium (64 bit).
Installing Apache Ant
Download and unzip Apache Ant into a suitable folder
e.g. C:\Users\faram\bin\apache-ant-1.9.3
Point Environment Variable ANT_HOME to above path.
Add its bin subdirectory to PATH.
e.g. C:\Users\faram\bin\apache-ant-1.9.3\bin
Set JAVA_HOME to directory where you have installed the JDK.
e.g. C:\Program Files\Java\jdk1.7.0_45
Open a command prompt and type ant -version. The output should be something like -
Apache Ant(TM) version 1.9.3 compiled on December 23 2013
This means that Ant installation is ok.
Configuring the build
Start CMake as administrator.
Enter the path to the OpenCV source. e.g. D:\PROJECTS\LIBRARIES\c\opencv\sources
and path where you want to place the binaries e.g. D:/PROJECTS/LIBRARIES/c/opencv/build_custom
Expand the BUILD group and ensure that BUILD_opencv_java is checked. Check/uncheck entries in all the groups, as required.
Click the Configure button. CMake will check for required files and write output to its console. If it doesn't find any files then highlight them in red. Rectify the items marked in red and press Configure again. The console output should confirm that the java module will be built.
Press Generate button. This will generate all the make files required by Visual Studio 2010 to build the binaries.
Building OpenCV Binaries
Go to the build directory (D:/PROJECTS/LIBRARIES/c/opencv/build_custom) and double-click on OpenCV.sln to open the solution and constituent projects in Visual Studio 2010.
Select the required solution platform (Win32 or x64). If you are using 64 bit Java then the OpenCV binaries will also need to be 64 bit. Select the required configuration i.e. Debug or Release.
Build the solution by pressing F7. It may take some time to build all the projects.
The OpenCV DLLs will be created in the bin/Debug or bin/Release subdirectory on the build directory (e.g. D:\PROJECTS\LIBRARIES\c\opencv\build_custom\bin\Debug). These DLLs will also include the java module DLL i.e. opencv_java248.dll. Add this path to the PATH environment variable.
The OpenCV jar file, opencv-248.jar will be created in the bin subdirectory of the build directory (e.g. D:\PROJECTS\LIBRARIES\c\opencv\build_custom\bin).
Running an OpenCV application in Java
Create a New Java Application project in NetBeans 7.3 IDE. Add opencv-248.jar as a library. Write some code using the OpenCV Java API. Within it, call the native library, opencv_java248.dll before using any of the API entities.
Following code loads and displays an image in a window and also displays an edge-detected version in another window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package testopencv; import java.io.ByteArrayInputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; /** * Loads an image and displays it in a window and displays its edge-detected version in another window. * @author faram */ class Main { static { // Load the OpenCV DLL System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } public static void main(String[] args) { // Load an image file and display it in a window. Mat m1 = Highgui.imread("D:/PROJECTS/LIBRARIES/c/opencv/lena.jpg"); imshow("Original", m1); // Do some image processing on the image and display in another window. Mat m2 = new Mat(); Imgproc.bilateralFilter(m1, m2, -1, 50, 10); Imgproc.Canny(m2, m2, 10, 200); imshow("Edge Detected", m2); }//main /** * Shows given image in a window. Analogous to cv::imshow() of C++ API. * * @param title * @param img */ public static void imshow(String title, Mat img) { // Convert image Mat to a jpeg MatOfByte imageBytes = new MatOfByte(); Highgui.imencode(".jpg", img, imageBytes); try { // Put the jpeg bytes into a JFrame window and show. JFrame frame = new JFrame(title); frame.getContentPane().add(new JLabel(new ImageIcon(ImageIO.read(new ByteArrayInputStream(imageBytes.toArray()))))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } } |
Press F6 to build and run the project.
References
Appendix
Screenshots of some CMake groups from my build -
Ungrouped Entries
BUILD group
ENABLE, INSTALL, JAVA , OPENCV, TBB groups
WITH group
Resources
Some make files from my build -
No comments:
Post a Comment