0% found this document useful (0 votes)
286 views9 pages

Opencv With Java

This document provides instructions for installing OpenCV for Java on Windows and Linux/Mac OS systems. It describes downloading and configuring required software, including the JDK, Eclipse, OpenCV library, and optional tools. It also provides example code for basic OpenCV operations in a console project and GUI project using FXML and JavaFX.

Uploaded by

Ionut Ionut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
286 views9 pages

Opencv With Java

This document provides instructions for installing OpenCV for Java on Windows and Linux/Mac OS systems. It describes downloading and configuring required software, including the JDK, Eclipse, OpenCV library, and optional tools. It also provides example code for basic OpenCV operations in a console project and GUI project using FXML and JavaFX.

Uploaded by

Ionut Ionut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Installing OpenCV for Java

Getting started...
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/

OpenCV Installation under Windows


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

OpenCV Installation under Linux/Mac


Note: follow this instructions if you want to compile OpenCV under Windows...
i.
ii.
iii.
iv.
v.

Download and install CMake, if not already installed on your OS


Download and install Apache Ant, if not already installed on your OS
Extract the downloaded OpenCV file in a location of your choice
Open CMake ( cmake-gui )
Put the location of the extracted OpenCV library in the Where is the source code field (e.g., /tmp/opencv2.4.10 )
vi. Put the destination directory of your build in the Where to build the binaries field (e.g., /tmp/opencv2.4.10/build )
vii. Check the Grouped and Advanced checkboxes

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-ant1.9.4/bin/ant )
x. In the BUILD group, unselect:
BUILD_PERF_TESTS
BUILD_SHARED_LIBRARY to make the Java bindings dynamic library all-sufficient
BUILD_TESTS
xi. In the CMAKE group, set to Debug (or Release ) the CMAKE_BUILD_TYPE
xii. 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

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


xvi. Build eveything with the command: make -j
xvii. Wait...
xviii. Done! All you need is:
opencv-2410.jar in the .../opencv-2.4.10/build/bin directory
libopencv_java2410.so in the .../opencv-2.4.10/build/lib/ directory
the data directory in .../opencv-2.4.9/

OpenCV for Java in Eclipse


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 menu
navigate 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.jar
after 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)

A console-based OpenCV Java project


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 name
iii. On the Java Settings step, under Libraries , select Add Library... , then the newly created User Library (e.g.,
opencv2 ), and click Finish
iv. Choose Finish to complete the project creation
v. 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

{
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]

Fun with the GUIs


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 at
http://www.eclipse.org/efxclipse/install.html#for-the-ambitious
If 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 from
http://www.oracle.com/technetwork/java/javase/downloads/javafxscenebuilder-info-2157684.html
iii. Create a new JavaFX project
with a name (e.g., FXHelloCV) and a package at your choice
in FXML format
that 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 application
FXHelloCVController.java is the controller file for handling the mouse and keyboard input
application.css contains the CSS style for the application
FXHelloCV.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 project
vi. 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>

<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;

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

{
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;
}

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

Known bugs
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-linuxgnu/libv4l/v4l1compat.so (or /usr/lib/i386-linux-gnu/libv4l/v4l1compat.so for 32 bit systems) directly in Eclipse

You might also like