Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV Computer Vision with Java

You're reading from   OpenCV Computer Vision with Java Create multiplatform computer vision desktop and web applications using the combination of OpenCV and Java

Arrow left icon
Product type Paperback
Published in Jul 2015
Publisher
ISBN-13 9781783283972
Length 174 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Daniel Lelis Baggio Daniel Lelis Baggio
Author Profile Icon Daniel Lelis Baggio
Daniel Lelis Baggio
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Setting Up OpenCV for Java FREE CHAPTER 2. Handling Matrices, Files, Cameras, and GUIs 3. Image Filters and Morphological Operators 4. Image Transforms 5. Object Detection Using Ada Boost and Haar Cascades 6. Detecting Foreground and Background Regions and Depth with a Kinect Device 7. OpenCV on the Server Side Index

A Java OpenCV simple application

It's time to create a simple application that will show that we can now compile and execute Java code with OpenCV. Create a new Java class containing a Main method and paste the code given as follows. It simply creates a 5 x 10 OpenCV matrix, sets some of its rows and columns, and prints the result to the standard output.

Make sure you load the correct dynamic link libraries through a call to System.loadlibrary("opencv_java300"). Since, you might want to change the library version later, a better approach would be to use the Core.NATIVE_LIBARAY_NAME constant, which will output the correct library name. You can also find this file in the code repository for chapter1 of this book, under ant/src.

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

class SimpleSample {

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

}

According to Oracle's documentation, it states that, class can have any number of static initialization blocks. And they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

You should make sure that any calls to the OpenCV library are preceded by a single System.loadLibrary call, in order to load the dynamic libraries. Otherwise, you will receive an java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat(IIIDDDD)J error. This generally occurs in a static block.

If everything goes well, you should see the following output in the console:

Welcome to OpenCV 3.0.0-rc1
OpenCV Mat: Mat [ 5*10*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x2291b70, dataAddr=0x229bbd0 ]
OpenCV Mat data:
[ 0, 0, 0, 0, 0, 5, 0, 0, 0, 0;
  1, 1, 1, 1, 1, 5, 1, 1, 1, 1;
  0, 0, 0, 0, 0, 5, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 5, 0, 0, 0, 0;
  0, 0, 0, 0, 0, 5, 0, 0, 0, 0]
You have been reading a chapter from
OpenCV Computer Vision with Java
Published in: Jul 2015
Publisher:
ISBN-13: 9781783283972
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image