opencv with java

9
i. Download the JDK 7 from the Oracle website and install it ii. Download and install the latest version of the Eclipse IDE (Luna, version 4.4) from the Eclipse Downloads page (suggested package: Eclipse IDE for Java Developers) iii. Download the OpenCV library (version 2.4.10) from http://opencv.org/ i. Extract the downloaded OpenCV file in a location of your choice (e.g., C:\opencv ) ii. Done! All you need is: opencv-2410.jar in the ...\opencv\build\java folder opencv_java2410.dll in the ...\opencv\build\java\x64 folder for 64 bit systems or in the ...\opencv\build\java\x86 folder for 32 bit systems the data folder in ...\opencv\sources Note: follow this instructions if you want to compile OpenCV under Windows... i. Download and install CMake , if not already installed on your OS ii. Download and install Apache Ant , if not already installed on your OS iii. Extract the downloaded OpenCV file in a location of your choice iv. Open CMake ( cmake-gui ) v. Put the location of the extracted OpenCV library in the Where is the source code field (e.g., /tmp/opencv- 2.4.10 ) vi. Put the destination directory of your build in the Where to build the binaries field (e.g., /tmp/opencv- 2.4.10/build ) vii. Check the Grouped and Advanced checkboxes Installing OpenCV for Java Getting started... OpenCV Installation under Windows OpenCV Installation under Linux/Mac

Upload: ionut-ionut

Post on 25-Jan-2016

19 views

Category:

Documents


4 download

DESCRIPTION

nbn

TRANSCRIPT

Page 1: Opencv With Java

i. Download the JDK 7 from the Oracle website and install itii. Download and install the latest version of the Eclipse IDE (Luna, version 4.4) from the Eclipse Downloads

page (suggested package: Eclipse IDE for Java Developers)iii. Download the OpenCV library (version 2.4.10) from http://opencv.org/

i. Extract the downloaded OpenCV file in a location of your choice (e.g., C:\opencv )ii. Done! All you need is:

opencv-2410.jar in the ...\opencv\build\java folderopencv_java2410.dll in the ...\opencv\build\java\x64 folder for 64 bit systems or in the...\opencv\build\java\x86 folder for 32 bit systems

the data folder in ...\opencv\sources

Note: follow this instructions if you want to compile OpenCV under Windows...

i. Download and install CMake, if not already installed on your OSii. Download and install Apache Ant, if not already installed on your OSiii. Extract the downloaded OpenCV file in a location of your choiceiv. Open CMake ( cmake-gui )v. Put the location of the extracted OpenCV library in the Where is the source code field (e.g., /tmp/opencv-

2.4.10 )vi. Put the destination directory of your build in the Where to build the binaries field (e.g., /tmp/opencv-

2.4.10/build )

vii. Check the Grouped and Advanced checkboxes

Installing OpenCV for JavaGetting started...

OpenCV Installation under Windows

OpenCV Installation under Linux/Mac

Page 2: Opencv With Java

viii. Press Configure and use the default compilers for Unix Makefiles

ix. In the Ungrouped Entries group, insert the path to the Apache Ant executable (e.g., /opt/apache-ant-1.9.4/bin/ant )

x. In the BUILD group, unselect:BUILD_PERF_TESTSBUILD_SHARED_LIBRARY to make the Java bindings dynamic library all-sufficientBUILD_TESTS

xi. In the CMAKE group, set to Debug (or Release ) the CMAKE_BUILD_TYPExii. In the JAVA group:

insert the Java AWT include path (e.g., /usr/lib/jvm/java-7-oracle/include/ )insert the Java AWT library path (e.g., /usr/lib/jvm/java-7-oracle/include/jawt.h )insert the Java include path (e.g., /usr/lib/jvm/java-7-oracle/include/ )insert the alternative Java include path (e.g., /usr/lib/jvm/java-7-oracle/include/linux )insert the JVM library path (e.g., /usr/lib/jvm/java-7-oracle/include/jni.h )

xiii. Press Configure two times

xiv. The CMake window should appear with a white background, now: press Generate and close CMake

Page 3: Opencv With Java

xv. Open the terminal and go to the build folder of OpenCV

xvi. Build eveything with the command: make -jxvii. Wait...xviii. Done! All you need is:

opencv-2410.jar in the .../opencv-2.4.10/build/bin directorylibopencv_java2410.so in the .../opencv-2.4.10/build/lib/ directory

the data directory in .../opencv-2.4.9/

i. Open Eclipse and select a workspace of your choice

ii. Create a User Library , ready to be used on all the next projects:

select Window > Preferences... from the menunavigate under Java > Build Path > User Libraries and choose New...enter a name for the library (e.g., opencv2 )select the newly created user library and choose Add External JARs...browse to select opencv-2410.jarafter adding the jar, extend it and select Native library location and press Edit...select External Folder... and browse to select the folder containing the OpenCV libraries (e.g.,C:\opencv\build\java\x64 under Windows)

OpenCV for Java in Eclipse

Page 4: Opencv With Java

Note: the project created by following the next steps is available in the course page on the Teaching Portal

i. In Eclipse, create a new Java project ( File > New > Java Project or File > New > Project... > Java Project )ii. Choose HelloCV as the project nameiii. On the Java Settings step, under Libraries , select Add Library... , then the newly created User Library (e.g.,

opencv2 ), and click Finishiv. Choose Finish to complete the project creationv. Create a new Java class, in a package of your choice and by using the following code. The output of running

the code should be a 3x3 identity matrix.

Source code:

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

public class HelloCV

A console-based OpenCV Java project

Page 5: Opencv With Java

{ public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat mat = Mat.eye(3, 3, CvType.CV_8UC1); System.out.println("mat = " + mat.dump()); }}

Expected output:

mat = [1, 0, 0; 0, 1, 0; 0, 0, 1]

Note: the project created by following the next steps is available in the course page on the Teaching Portal

The e(fx)clipse plugin and the Scene Builder application are not strictly mandatory for create a JavaFX project:they are only helpers...

i. In Eclipse, install the version 0.9.0 (instead of version 1.0.0) of the e(fx)clipse plugin, by following the guide athttp://www.eclipse.org/efxclipse/install.html#for-the-ambitiousIf you choose not to install such a plugin, you have to create a traditional Java project and add jfxrt.jar(present in the JDK folder) to the project's libraries.

ii. [Optional] Download and install the JavaFX Scene Builder fromhttp://www.oracle.com/technetwork/java/javase/downloads/javafxscenebuilder-info-2157684.html

iii. Create a new JavaFX projectwith a name (e.g., FXHelloCV) and a package at your choicein FXML formatthat includes the OpenCV User Library

iv. The project should contain 4 files:Main.java takes care of the standard Java code required for an FXML applicationFXHelloCVController.java is the controller file for handling the mouse and keyboard inputapplication.css contains the CSS style for the applicationFXHelloCV.fxml is the FXML source file in which the user interface is defined

v. Rename Main.java to FXHelloCV.java so that the name is more meaningfull for this projectvi. Edit the project files as shown in the following code. The expected output is reported after the source code.

FXHelloCV.fxml source:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?><?import javafx.scene.control.*?><?import javafx.scene.layout.*?><?import javafx.scene.image.*?>

<BorderPane xmlns:fx="http://javafx.com/fxml" fx:controller="it.polito.elite.teaching.cv.FXHelloCVController"> <center>

Fun with the GUIs

Page 6: Opencv With Java

<ImageView id="currentFrame" /> </center> <bottom> <HBox alignment="center" > <padding> <Insets top="25" right="25" bottom="25" left="25"/> </padding> <Button fx:id="button" alignment="center" text="Start camera" onAction="#startCamera" /> </HBox> </bottom></BorderPane>

FXHelloCV.java source:

...@Override public void start(Stage primaryStage) { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("FXHelloCV.fxml")); BorderPane root = (BorderPane) loader.load(); Scene scene = new Scene(root, 800, 600); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setTitle("JavaFX meets OpenCV"); primaryStage.setScene(scene); primaryStage.show();

FXHelloCVController controller = loader.getController(); controller.setRootElement(root);

} catch (Exception e) { e.printStackTrace(); } }

public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

launch(args); }...

FXHelloCVController.java source:

@FXML private Button button;

Page 7: Opencv With Java

private Pane rootElement; private Timer timer; private VideoCapture capture = new VideoCapture();

@FXML protected void startCamera(ActionEvent event) { if (this.rootElement != null) { final ImageView frameView = (ImageView) rootElement.lookup("#currentFrame"); final ObjectProperty<Image> imageProp = new SimpleObjectProperty<>(); frameView.imageProperty().bind(imageProp);

if (!this.capture.isOpened()) { this.capture.open(0);

TimerTask frameGrabber = new TimerTask() { @Override public void run() { imageProp.set(grabFrame()); } }; this.timer = new Timer(); this.timer.schedule(frameGrabber, 0, 33);

this.button.setText("Stop Camera"); } else { this.button.setText("Start Camera"); if (this.timer != null) { this.timer.cancel(); this.timer = null; } this.capture.release(); frameView.setImage(null); } } }

private Image grabFrame() { Image imageToShow = null; Mat frame = new Mat();

if (this.capture.isOpened()) { try

Page 8: Opencv With Java

{ this.capture.read(frame);

if (!frame.empty()) { Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY); imageToShow = mat2Image(frame); }

} catch (Exception e) { System.err.println("ERROR: " + e.getMessage()); } }

return imageToShow; }

private Image mat2Image(Mat frame) { MatOfByte buffer = new MatOfByte(); Highgui.imencode(".png", frame, buffer); return new Image(new ByteArrayInputStream(buffer.toArray())); }

public void setRootElement(Pane root) { this.rootElement = root; }

Page 9: Opencv With Java

The OpenCV Javadoc, describing all the OpenCV APIs, are available at the address:http://docs.opencv.org/java/

On Linux, for some (old) webcams, OpenCV can report the following error(s):

HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV`VIDIOC_STREAMON: Bad file descriptor

Such a problem can be resolved by setting the LD_PRELOAD environment variable to /usr/lib/x86_64-linux-gnu/libv4l/v4l1compat.so (or /usr/lib/i386-linux-gnu/libv4l/v4l1compat.so for 32 bit systems) directly in Eclipse

OpenCV Javadoc

Known bugs