Hi guys, this is my first post since I moved two weeks ago to Tijuana. This tutorial is based on a similar application that can be found in this magnificent book: OpenCV 3.0 Computer Vision with Java by Daniel Lélis Baggio.

Java is one of the most popular programming languages in the world and my favourite one by the way. It is really easy to understand, it is strongly typed and with awesome frameworks for testing (JUnit, for example).

Based on that, even my other applications were developed using C# and EmguCV, I found that the integration Java + OpenCV can be easily done. In this tutorial, we will use Maven for Dependency and Project Management.

Woo, wait! What is Maven?

Maven is a tool for Dependencies and Project Management. A dependency is an external software or code that is needed in your own project. We depend on the OpenCV library in order to get this application working. Also, Maven allow us to compile the project, generate a .jar file, execute tests, etc.

Maven.png
Maven totally rocks!

(Warning) What you should download before working on this tutorial.

You need to download two things:

  • Java SDK (7, 8, whatever you want)
  • Maven (download the latest stable version)

Downloading and installing both programs is not quite difficult.If you have downloaded and install them correctly (which may include the Environment Variables setting for both programs), you will be able to execute the following command mvn -v that will display the Java and the Maven versions installed in your computer.

Step 1: Initialize the project

  1. Create a folder in your computer. In my case, I will name it OpenCVTutorial01.
  2. Inside that folder, open a Command Line terminal. You will execute the following command:
    mvn archetype:generate -DgroupId=com.miguellopez.app -DartifactId=tutorial01 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    I suggest to change the variables displayed in bold so they match your name and conventions used by yourself. The groupId should follow the com.companyname.application convention suggested by Oracle.

java01
This command will create a basic Java/Maven project.
  1. The previous command wil generate another folder named by the artifactId. Inside, you will a folder named src and the POM file. The POM file is where you specify everything to Maven. You can determine the dependencies used by your project, the steps to be executed when building and compiling the project, etc.
Java02.PNG
In src, the source code can be found. The POM file is where everything you need for your project will be specified.
java03
Default POM file.
  1. Modify the POM file adding the following dependencies :

    <dependencies>
    <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_64</classifier>
    </dependency>
    </dependencies>

    and the following repositories:

    <repositories>
    <repository>
    <id>javaopencvbook</id>
    <url>https://raw.github.com/JavaOpenCVBook/code/maven2/</url&gt;
    </repository>
    </repositories>

Java06.PNG
This is how the POM file should look once you added the dependencies and repositories.
  1. Execute the following command in the folder where the POM in located: mvn package. This will download the OpenCV dependencies (libraries) from the repository created by the book’s authors.
java05
When executing mvn package, not only the libraries will be downloaded. A target folder will be created; in there, the built application will be found.

Step 2: Define the project Build

  1. We need to modify one last time the POM to define how the Java Application will use the OpenCV library. Add the following build profile in the POM file, but look out at the mainClass variable. This variable should match the groupId name you defined before.

    <build>
    <plugins>
    <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
    <archive>
    <manifest>
    <addClasspath>true</addClasspath>
    <classpathPrefix>lib/</classpathPrefix>
    <mainClass>com.miguellopez.app.App</mainClass>
    </manifest>
    </archive>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
    <execution>
    <id>copy-dependencies</id>
    <phase>package</phase>
    <goals>
    <goal>copy-dependencies</goal>
    </goals>
    <configuration>
    <outputDirectory>${project.build.directory}/lib</outputDirectory>
    <overWriteReleases>false</overWriteReleases>
    <overWriteSnapshots>false</overWriteSnapshots>
    <overWriteIfNewer>true</overWriteIfNewer>
    </configuration>
    </execution>
    </executions>
    </plugin>
    <plugin>
    <groupId>com.googlecode.mavennatives</groupId>
    <artifactId>maven-nativedependencies-plugin</artifactId>
    <version>0.0.7</version>
    <executions>
    <execution>
    <id>unpacknatives</id>
    <phase>generate-resources</phase>
    <goals>
    <goal>copy</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </build>

  2. Execute the mvn package command.

 

Java07.PNG
Build specification in the POM file. Validate that the MainClass variable matches your App.java package.

Step 3: Let’s code the application!

  1. In the src folder, you will find the App.java file. Now we can write the application code. Just be careful with the package (at the top of the file) so it matches your groupId.

    package com.miguellopez.app;

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.CvType;
    import org.opencv.core.Scalar;

    public class App {
    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());
    }
    }

    java08
    App.java file. In the static block, we specify to the JVM to load the native (.dll) library files to be loaded. 
  2. Execute the mvn package command again. This will create the final Java Application.

Step 4: Execute the Application!

  1. Place yourself to the folder where the POM file can be found. Execute the following command. Just be careful with the name of the SNAPSHOT file generated.

java -Djava.library.path=target\natives -jar target\tutorial01-1.0-SNAPSHOT.jar

If everything works ok, something like this will displayed in the Command Line:

JAva09.PNG
App.java execution. We display a 5×10 matrix after we setted several values on it.

This application creates a new 5×10 Matrix. All the locations will have a 0 value. Afterwards we will reference the row number 1 (remember that rows and colums are zero-indexed) to a new variable, setting it values to 1. Also, the column number 5 will be referenced to a new variable setting the values to 5. Finally, the modified matrix will be displayed.

In this program, we used two different ways when printing the matrix. First, we used System.out.println(“OpenCV Mat: ” + m). This prints the matrix’s metadata. However, if we use the m.dump() function, we will print the actual data inside the matrix.

Conclusions

  • We learned how to configure a Java + OpenCV + Maven project.
  • Maven allow us to avoid downloading manually the OpenCV library. When we execute mvn package, Maven will automaticaly download the library from the repository.
  • Using Java, we use the original OpenCV library (.dll files). We avoid using a wrapper as with C# + EmguCV.

As always, is something comes up, do not hesitate to write a comment or contact me and I’ll answer you ASAP.

 

Advertisements