Nicolas Modrzyk 2018
Nicolas Modrzyk Java Image Processing Recipes https://doi.org/10.1007/978-1-4842-3465-5_1
1. OpenCV on the JavaVM
A few years ago, while on a trip to Shanghai, a very good friend of mine bought me a massive book on OpenCV. It had tons of photography manipulation, realtime video analysis samples, and in-depth explanations that were very appealing, and I just could not wait to get things up and running on my local environment.
OpenCV, as you may know, stands for Open Source Computer Vision ; it is an open source library that gives you ready-to-use implementations of advanced imaging algorithms, going from simple-to-use but advanced image manipulations to shape recognition and real-time video analysis spy powers.
The very core of OpenCV is a multidimensional matrix object named Mat. Mat is going to be our best friend all along this book. Input objects are Mat, operations are run on Mat, and the output of our work is also going to be Mat.
Mat , even though it is going to be our best friend, is a C++ object, and as such, is not the easiest of friends to bring and show around. You have to recompile, install, and be very gentle about the new environment almost anywhere you take him.
But Mat can be packaged.
Mat, even though he is a native (i.e., runs natively), can be dressed to run on the Java Virtual Machine almost without anyone noticing.
This first chapter wants to get you introduced to work with OpenCV with some of the main languages of the Java Virtual Machine, namely Java of course, but also the easier-to-read Scala and the Google-hyped Kotlin.
To run all these different languages in a similar fashion, you will first get (re-?)introduced to a Java build tool named leiningen and then you will move on to use simple OpenCV functions with it.
The road of this first chapter will take you to the door of the similarly JVM-based language Clojure, which will give your OpenCV code instant visual feedback for great creativity. That will be for Chapter .
1.1 Getting Started with Leiningen
Problem
You remember the write-once-run-everywhere quote, and you would like to compile Java code and run the Java program in an easy and portable manner across different machines. Obviously, you can always revert to using the plain javac command to compile Java code, and pure Java on the command line to run your compiled code , but we are in the 21st century, and hey, you are looking for something more.
Whatever the programming language, setting up your working environment by hand is quite a task, and when you are done, it is hard to share with other people.
Using a build tool, you can define in simple ways what is required to work on your project, and get other users to get started quickly.
You would like to get started with an easy-to-work-with build tool.
Solution
Leiningen is a build tool targeting (mostly) the JavaVM. It is similar in that sense to other famous ones like (Remember? The) Ant, (Oh My God) Maven, and (it used to work) Gradle.
Once the leiningen command is installed, you can use it to create new JavaVM projects based on templates, and run them without the usual headaches.
This recipe shows how to install Leiningen quickly and run your first Java program with it.
How it works
You will start by simply installing Leiningen where you need it, and then creating a blank Java project with it.
Note
Installing Leiningen requires Java 8 to be installed on your machine. Note also that due to the fact that Java 9 is solving old problems by breaking current solutions, we will choose to keep Java 8 for now.
Installing Leiningen
The Leiningen web site itself is hosted and can be found at
https://leiningen.org/
At the top of the Leiningen page, you can find the four easy steps to install the tool manually yourself.
So here it goes, on macOS and Unix :
Place it on your $PATH where your shell can find it (e.g., ~/bin)
Set it to be executable (chmod a+x ~/bin/lein)
Run it from a terminal, lein, and it will download the self-install package
And on Windows:
Download the lein.bat batch script
Place it on your C:/Windows/System32 folder, using admin permission
Open a command prompt and run it, lein, and it will download the self-install package
On Unix , you can almost always use a package manager. Brew, on macOS, has a package for leiningen.
On Windows , there is also a good Windows installer, located at https://djpowell.github.io/leiningen-win-installer/ .
If you are a Chocolatey fan, Windows has a package for Chocolatey as well: https://chocolatey.org/packages/Lein .
If you finished the install process successfully on a terminal or command prompt, you should be able to check the version of the installed tool. On the first run, Leiningen downloads its own internal dependencies, but any other subsequent runs will regularly be fast.
NikoMacBook% lein -v
Leiningen 2.7.1 on Java 1.8.0_144 Java HotSpot(TM) 64-Bit Server VM
Creating a New OpenCV-Ready Java Project with Leiningen
Leiningen mainly works around a text file, named project.clj, where the metadata, dependencies, plug-ins, and settings for those projects are defined in a simple map.
When you execute commands on the project calling the lein command, lein will look into that project.clj to find the relevant information it needs regarding that project.
Leiningen comes with ready-to-use project templates, but in order to understand them properly, lets first walk through a first example step by step.
For a leiningen Java project, you need two files:
One that describes the project, project.clj
One file with some Java code in it, here Hello.java
A first project simple directory structure looks like this:
.
java
Hello.java
project.clj
1 directory, 2 files
For peace of mind, we will keep the code of this first Java example pretty simple.
public class Hello {
public static void main(String[] args) {
System.out.println("beginning of a journey");
}
}
Now lets see the content of the text file named project.clj in a bit of detail:
(defproject hellojava "0.1"
:java-source-paths ["java"]
:dependencies [[org.clojure/clojure "1.8.0"]]
:main Hello)
This is actually Clojure code, but lets simply think of it as a domain specific language (DSL), a language to describe a project in simple terms.
For convenience, each term is described in Table .
Table 1-1
Leiningen Project Metadata
Word | Usage |
---|
Defproject | Entry point to define a project |
Hellojava | The name of the project |
0.1 | A string describing the version |
:java-source-paths | A list of directories relative to the project folder, where you will put Java code files |
:dependencies | The list of external libraries and their versions needed to run the project |