Creating a Java Based OpenCV project with the Origami archetype.

It used to be quite a pain to get started with OpenCV and Java, but not anymore ! Origami has a wonderful maven archetype that can generate a project ready to run OpenCV code in seconds.

Without further ado… here is the secret command:

mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \
-DarchetypeArtifactId=maven-archetype \
-DarchetypeGroupId=origami \
-DarchetypeVersion=1.0 \
-DarchetypeCatalog=https://repository.hellonico.info/repository/hellonico/ \
-Dversion=1.0-SNAPSHOT \
-DgroupId=hello \
-DartifactId=cvj

Will give the following output, where you only need to confirm the different attributes given in the command.

[INFO] Using property: groupId = hello
[INFO] Using property: artifactId = cvj
[INFO] Using property: version = 1.0-SNAPSHOT
[INFO] Using property: package = hello
Confirm properties configuration:
groupId: hello
artifactId: cvj
version: 1.0-SNAPSHOT
package: hello
 Y: : 
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: hello
[INFO] Parameter: artifactId, Value: cvj
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: hello
[INFO] Parameter: packageInPathFormat, Value: hello
[INFO] Parameter: package, Value: hello
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: hello
[INFO] Parameter: artifactId, Value: cvj
[INFO] project created from Archetype in dir: /Users/niko/origami-land/cvj
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.907 s
[INFO] Finished at: 2019-08-16T18:07:40+09:00
[INFO] ------------------------------------------------------------------------

The command generates a folder with the following content:

.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── hello
    │           └── HelloCv.java
    └── test
        └── java
            └── hello
                └── AppTest.java

7 directories, 3 files

You can run the included sample directly with:

mvn compile exec:java

Which will output a simple OpenCV Mat object:

[  1,   0,   0;
   0,   1,   0;
   0,   0,   1]

You could also try, an updated version of the sample:

package hello;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.scijava.nativelib.NativeLoader;
import java.awt.image.BufferedImage;
import origami.Origami;

public class HelloCv {
    public static void main(String[] args) throws Exception {
        NativeLoader.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        
        String origamiImage = "https://raw.githubusercontent.com/hellonico/origami/master/doc/origami.jpg";
        Mat mat1 = Origami.urlToMat(origamiImage);
        mat1.convertTo(mat1, -1, 1, 100);
        BufferedImage bi = Origami.matToBufferedImage(mat1);
        Mat mat2 = Origami.bufferedImagetoMat(bi);
        Imgcodecs.imwrite("origami.jpg", mat2);
    }
}

And you should get a brighter Origami for a brighter weekend.

/origami.jpg