📜 ⬆️ ⬇️

Work with OpenCV. Part 1. Installation and Hello World

Navigator:
Work with OpenCV. Part 1. Installation and Hello World

This series of articles will cover working with the OpenCV computer vision library. To work from under Java, the JavaCV interface will be used.

Installation


Everything described below is made under Linux Ubuntu 12.04!
First of all, visit the JavaCV website and download the latest version, then look at the supported version of OpenCV in the Required Software section, this is OpenCV 2.4.6.x.
Go to the OpenCV website and download the currently supported version.

If everything went well, we will have 2 archives:
opencv-2.4.6.1.tar.gz
javacv-0.6-bin.zip
')

Install OpenCV


For the graphics to work in addition to install (well, cmake if suddenly someone is not worth it):
apt-get install libgtk2.0-dev
apt-get install cmake

Installation Commands:
cd opencv-2.4.6.1/

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ./
At the cmake stage, it is important to view the console output and find the following lines:
Java:
- ant: / usr / bin / ant (ver 1.8.2)
- JNI: NO
- Java tests: NO


Without JNI, you will not get the JAR file that we need, if you saw the same thing as me, then check your $ JAVA_HOME, most likely it is empty ... Ideally, you should see the following:
- Java:
- ant: / usr / bin / ant (ver 1.8.2)
- JNI: / usr / lib / jvm / java-8-oracle / include / usr / lib / jvm / java-8-oracle / include / linux / usr / lib / jvm / java-8-oracle / include
- Java tests: YES

We continue further:
make
make install
In the meantime, they are performed, you can go and make yourself a coffee!

Work in IDE


Create a regular Java application, and add the following libraries there:
javacv.jar
javacpp.jar
javacv-linux - *. jar

And we will try to display the webcam broadcast in the window, and also save the photo:
 package Habr; import com.googlecode.javacv.CanvasFrame; import com.googlecode.javacv.FrameGrabber; import com.googlecode.javacv.cpp.opencv_core.*; import com.googlecode.javacv.OpenCVFrameGrabber; import static com.googlecode.javacv.cpp.opencv_core.cvFlip; import static com.googlecode.javacv.cpp.opencv_highgui.*; public class HelloWorld { public static void main(String[] args) { CanvasFrame canvas = new CanvasFrame("Webcam"); canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); FrameGrabber grabber = new OpenCVFrameGrabber(""); try { grabber.start(); IplImage img; while (true) { img = grabber.grab(); canvas.setCanvasSize(grabber.getImageWidth(), grabber.getImageHeight()); if (img != null) { cvFlip(img, img, 1); cvSaveImage("/home/vlad/1.jpeg", img); canvas.showImage(img); } } } catch (Exception e) { } } } 


After launch, you can see yourself, and then find a photo on the specified path :) I think to explain what OpenCV is, and that it may not be necessary. Thanks that's all.

Source: https://habr.com/ru/post/204638/


All Articles