Posts Guide to start using bytedeco Opencv
Post
Cancel

Guide to start using bytedeco Opencv

Who is this post for ?

Backend Java developers looking to start using bytedeco OpenCV libraries.

Tools this post will be using :

  1. JavaCV ( bytedeco ) - 1.5.5
  2. Oracle Java 1.8
  3. Intellij
  4. Maven
  5. 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

  1. JavaCV (bytedeco) :
    1. Open source project that provides wrapper over native OpenCV libs ( packaged along with native OpenCV libraries for all platforms )
  2. Oracle Java 1.8 :
    1. A very popular programming language
    2. OpenJDK is oracle java’s open source implementation
  3. Intellij :
    1. A popular IDE used for Java
    2. Pretty power when it comes to debugging and addon integration
    3. Comes in 2 versions : Community and Ultimate
  4. Maven
    1. Build automation tool
    2. Makes dependency management pretty convinient
  5. MacOS
    1. 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

Step one

Step one

step three

Go ahead, and throw in a package name and a Main class

step four

Here is what your pom should look like currently

step five

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

step six

Hit maven reload and external dependencies should have been downloaded in your external libs folder

step seven

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();

    }

}

Run your main class and your output should look like what is being displayed in the console

step eight

Voila! now you’ve been enabled with a new weapon of mass distribution and will definitely help you in most production scenarios. I hope this post has helped you in some way.

Cheers!

This post is licensed under CC BY 4.0 by the author.