-
Notifications
You must be signed in to change notification settings - Fork 43
Tutorial 1 Java OpenCV 5 minute setup
This tutorial will show you how to setup OpenCV for Java in 5 minutes.
1- Download and install Maven:
Maven is a project management tool that simplifies the process of retrieving dependencies for your Java project. It allows you to automatically download them from the internet using a preconfigured xml file that describes your project. In order to download it, go to https://maven.apache.org/download.cgi and look for a file named something like: apache-maven-3.3.3-bin.zip . Extract this file to a folder, let's say c:\users\your_name\Downloads\apache-maven-3.3.3-bin\ . In order to install it, follow instructions from: https://maven.apache.org/install.html . This is basically: "Adding to PATH: Add the unpacked distribution’s bin directory to your user PATH environment variable by opening up the system properties (WinKey + Pause), selecting the “Advanced” tab, and the “Environment Variables” button, then adding or selecting the PATH variable in the user variables with the value C:\Program Files\apache-maven-3.3.3\bin. The same dialog can be used to set JAVA_HOME to the location of your JDK, e.g. C:\Program Files\Java\jdk1.7.0_51". Check that Maven was correctly installed opening a new terminal and typing: mvn -v.
2- Get your starting pom.xml
There's a pom.xml available from https://github.com/JavaOpenCVBook/code/blob/master/chapter2/opencv-gui/pom.xml . Simply right-click the RAW button and save this in your computer. Remember to open it and change a couple details, like the groupId, the name of your project, and the version.
<modelVersion>4.0.0</modelVersion>
<groupId>org.javaopencvbook</groupId>
<artifactId>opencv-gui</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>opencv-gui</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>javaopencvbook</id>
<url>https://raw.github.com/JavaOpenCVBook/code/maven2/</url>
</repository>
</repositories>
Notice that this pom.xml refers to a 32-bit Java Runtime Environment, as seen in <classifier> tag:
<dependency>
<groupId>org.javaopencvbook</groupId>
<artifactId>opencvjar</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.javaopencvbook</groupId>
<artifactId>opencvjar-runtime</artifactId>
<version>3.0.0</version>
<classifier>natives-windows-x86</classifier>
</dependency>
If you have a 64-bit JRE, change the classifier to natives-windows-x86_64 and save your pom.xml.
In case you are on a 64-bit mac, change the classifier to natives-mac-x86_64 and use version 2.4.8 for the opnecvjar-runtime and opencvjar dependencies. You can browse available versions here:
https://github.com/JavaOpenCVBook/code/tree/maven2/org/javaopencvbook/opencvjar-runtime
3- Package your project
Now it's time for Maven to do his job. In the command prompt, go to the place you saved your pom.xml file, and issue the following command:
mvn package
This will download all native files required for you to run your project. In order to bring it to eclipse, simply type:
mvn eclipse:eclipse
It will generate a project that can be easily imported in Eclipse. Before running it, check your referenced libraries and look for opencvjar-3.0.0 and right click it -> Properties -> Native library -> Workspace -> Point it to the native file location, e.g.: opencv-gui/target/natives . Add a new source folder right-clicking your project and New -> Source Folder... and name it src. It's time to create your main class: right-click your project and New -> Class, and then give it a name, i.e. Main, and make sure you check the public static void main(String[] args), so it will be executable. Add the following code to it:
import org.opencv.core.*;
public class Main {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
System.out.println("Welcome to OpenCV " + Core.VERSION);
Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
System.out.println("OpenCV Mat: " + m);
Mat mr1 = m.row(1);
mr1.setTo(new Scalar(1));
Mat mc5 = m.col(5);
mc5.setTo(new Scalar(5));
System.out.println("OpenCV Mat data:\n" + m.dump());
}
}
Right click your project -> Run as -> Java application -> Point to the Main class, press ok and it should run.
For more detailed explanations check out the book OpenCV 3.0 Computer Vision with Java https://www.packtpub.com/application-development/opencv-java-javacv