Who is this post for ?
Backend Java developers looking to start using bytedeco OpenCV libraries.
Tools this post will be using :
- JavaCV ( bytedeco ) - 1.5.5
- Oracle Java 1.8
- Intellij
- Maven
- MacOS (10.13.6)
Quick description about the tools :
Feel free to scroll to the next section if you are fairly familiar with the tools
- JavaCV (bytedeco) :
- Open source project that provides wrapper over native OpenCV libs ( packaged along with native OpenCV libraries for all platforms )
- Oracle Java 1.8 :
- A very popular programming language
- OpenJDK is oracle java’s open source implementation
- Intellij :
- A popular IDE used for Java
- Pretty power when it comes to debugging and addon integration
- Comes in 2 versions : Community and Ultimate
- Maven
- Build automation tool
- Makes dependency management pretty convinient
- MacOS
- A popular operating system
Source Code
If you like, source code for this entire post is available here and can be downloaded for free.
Getting started :
Create a new Intellij maven project
Go ahead, and throw in a package name and a Main class
Here is what your pom should look like currently
Initialise your pom.xml with the following values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>opencvDistributableByteDeco</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- dependency required to start using bytedeco -->
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
</dependencies>
</project>
Your pom.xml should be looking something like this
Hit maven reload and external dependencies should have been downloaded in your external libs folder
Next, go ahead and configure your Main class :
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
package org.example;
/**
* make sure all imports are from bytedeco package name and not org.opencv
* org.opencv package functionalities will not work with bytedeco
*/
import org.bytedeco.javacpp.Loader;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_java;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
/**
* main class
*/
public class Main {
public static void main(String[] args) {
// this loads bytedeco native libraries into the memory. First Step!
Loader.load(opencv_java.class);
//notice how imread is different from the original OpenCV jar wrapper provided by OpenCV org
Mat mat = imread(Main.class.getClassLoader().getResource("sample.jpeg").getPath());
if (mat == null)
System.err.println("Unable to read image");
System.out.println("Image dimensions -> " + mat.cols() + " x " + mat.rows());
mat.release();
}
}