changing layout, adding ant and eclipse sections, more pictures
@ -8,4 +8,4 @@
|
|||||||
.. |Author_FernandoI| unicode:: Fernando U+0020 Iglesias U+0020 Garc U+00ED a
|
.. |Author_FernandoI| unicode:: Fernando U+0020 Iglesias U+0020 Garc U+00ED a
|
||||||
.. |Author_EduardF| unicode:: Eduard U+0020 Feicho
|
.. |Author_EduardF| unicode:: Eduard U+0020 Feicho
|
||||||
.. |Author_AlexB| unicode:: Alexandre U+0020 Benoit
|
.. |Author_AlexB| unicode:: Alexandre U+0020 Benoit
|
||||||
|
.. |Author_EricCh| unicode:: Eric U+0020 Christiansen
|
||||||
|
BIN
doc/tutorials/introduction/desktop_java/images/ant_output.png
Normal file
After Width: | Height: | Size: 23 KiB |
BIN
doc/tutorials/introduction/desktop_java/images/cmake_output.png
Normal file
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 12 KiB |
BIN
doc/tutorials/introduction/desktop_java/images/eclipse_run.png
Normal file
After Width: | Height: | Size: 70 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 25 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 768 KiB |
BIN
doc/tutorials/introduction/desktop_java/images/lena.png
Normal file
After Width: | Height: | Size: 606 KiB |
@ -5,78 +5,323 @@
|
|||||||
Introduction to Java Development
|
Introduction to Java Development
|
||||||
********************************
|
********************************
|
||||||
|
|
||||||
Last updated: 20 January, 2013.
|
Last updated: 12 February, 2013.
|
||||||
|
|
||||||
As of OpenCV 2.4.4, OpenCV supports desktop Java development using nearly the same interface as for Android development.
|
As of OpenCV 2.4.4, OpenCV supports desktop Java development using nearly the same interface as for
|
||||||
This guide will take you from the raw OpenCV source to a working Java application.
|
Android development. This guide will help you to create your first Java (or Scala) application using OpenCV.
|
||||||
We will use the `Simple Build Tool (SBT) <http://www.scala-sbt.org/>`_ to build our Java application and to generate an Eclipse project.
|
We will use either `Eclipse <http://eclipse.org/>`_, `Apache Ant <http://ant.apache.org/>`_ or the
|
||||||
|
`Simple Build Tool (SBT) <http://www.scala-sbt.org/>`_ to build the application.
|
||||||
|
|
||||||
For further reading after this guide, look at the Android tutorials. The interfaces for desktop Java and Android are nearly identical. You may also consult the `Java wiki page <http://code.opencv.org/projects/opencv/wiki/Java_API_howto>`_.
|
For further reading after this guide, look at the :ref:`Android_Dev_Intro` tutorials.
|
||||||
|
The interfaces for desktop Java and Android are nearly identical. You may also consult the
|
||||||
|
`Java wiki page <http://code.opencv.org/projects/opencv/wiki/Java_API_howto>`_.
|
||||||
|
|
||||||
What we'll do in this guide
|
What we'll do in this guide
|
||||||
***************************
|
***************************
|
||||||
|
|
||||||
In this guide, we will:
|
In this guide, we will:
|
||||||
|
|
||||||
* Download the OpenCV source from Github
|
* Get OpenCV with desktop Java support
|
||||||
|
|
||||||
* Build OpenCV and the Java wrappers and install OpenCV
|
* Create an ``Ant``, ``Eclipse`` or ``SBT`` project
|
||||||
|
|
||||||
* Create an SBT project and use it to generate an Eclipse project
|
* Write a simple OpenCV application in Java or Scala
|
||||||
|
|
||||||
* Copy the generated OpenCV jar into the SBT project and write a simple OpenCV application
|
The same process was used to create the samples in the :file:`samples/java` folder of the OpenCV repository,
|
||||||
|
so consult those files if you get lost.
|
||||||
|
|
||||||
The same process was used to create the samples in the samples/java folder of the OpenCV repository, so consult those files if you get lost.
|
Get OpenCV with desktop Java support
|
||||||
|
************************************
|
||||||
|
|
||||||
Download the OpenCV source from Github
|
Starting from version 2.4.4 OpenCV includes desktop Java bindings.
|
||||||
**************************************
|
The most simple way to get it is downloading the appropriate package of **version 2.4.4 or higher** from the
|
||||||
|
`OpenCV SourceForge repository <http://sourceforge.net/projects/opencvlibrary/files/>`_.
|
||||||
|
|
||||||
The OpenCV repository is hosted on `Github <https://github.com/Itseez/opencv/>`_.
|
.. note:: Windows users can find the prebuilt files needed for Java development in the
|
||||||
|
:file:`opencv/build/java/` folder inside the package.
|
||||||
|
Other OSes users (and Windows ones optionally) can build OpenCV from sources locally.
|
||||||
|
|
||||||
To download the repository, enter the following in a terminal:
|
Another option to get OpenCV sources is to clone OpenCV git repository,
|
||||||
|
either the primary one at ``git://code.opencv.org/opencv.git`` or
|
||||||
.. code-block:: bash
|
its `Github mirror <https://github.com/Itseez/opencv/>`_.
|
||||||
|
Be aware that Java is currently only supported on the ``2.4`` git branch and not yet merged to the ``master``.
|
||||||
git clone https://github.com/Itseez/opencv.git
|
In order to build OpenCV with Java bindings you need :abbr:`JDK (Java Development Kit)`
|
||||||
|
(we recommend `Oracle/Sun JDK 6 or 7 <http://www.oracle.com/technetwork/java/javase/downloads/>`_),
|
||||||
This will create a directory "opencv". Enter it.
|
`Apache Ant <http://ant.apache.org/>`_ and ``Python`` v2.6 or higher to be installed.
|
||||||
|
|
||||||
Build OpenCV and the Java wrappers and install OpenCV
|
Build OpenCV
|
||||||
*****************************************************
|
############
|
||||||
|
|
||||||
Java is currently only supported on the 2.4 git branch. This may change in the future, but for now you must switch to that branch:
|
Let's build OpenCV:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
|
git clone git://github.com/Itseez/opencv.git
|
||||||
|
cd opencv
|
||||||
git checkout 2.4
|
git checkout 2.4
|
||||||
|
|
||||||
To build the Java wrapper, first make sure you have Java 1.6+ and Apache Ant installed. Then invoke cmake with the DBUILD_opencv_java flag turned ON:
|
|
||||||
|
|
||||||
.. code-block:: bash
|
|
||||||
|
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cmake .. -DBUILD_opencv_java=ON
|
|
||||||
|
|
||||||
Examine the output of CMake and ensure "java" is one of the modules "To be built". If not, it's likely you're missing a dependency. You should troubleshoot by looking through the CMake output for any Java-related tools that aren't found and installing them.
|
Generate a Makefile or a MS Visual Studio* solution, or whatever you use for
|
||||||
|
building executables in your system:
|
||||||
|
|
||||||
Now build and install everything:
|
.. code-block:: bash
|
||||||
|
|
||||||
|
cmake -DBUILD_SHARED_LIBS=OFF ..
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
.. code-block:: bat
|
||||||
|
|
||||||
|
cmake -DBUILD_SHARED_LIBS=OFF -G "Visual Studio 10" ..
|
||||||
|
|
||||||
|
.. note:: When OpenCV is built as a set of **static** libraries (``-DBUILD_SHARED_LIBS=OFF`` option)
|
||||||
|
the Java bindings dynamic library is all-sufficient,
|
||||||
|
i.e. doesn't depend on other OpenCV libs, but includes all the OpenCV code inside.
|
||||||
|
|
||||||
|
Examine the output of CMake and ensure ``java`` is one of the modules "To be built".
|
||||||
|
If not, it's likely you're missing a dependency. You should troubleshoot by looking
|
||||||
|
through the CMake output for any Java-related tools that aren't found and installing them.
|
||||||
|
|
||||||
|
.. image:: images/cmake_output.png
|
||||||
|
:alt: CMake output
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
Now start the build:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
make -j8
|
make -j8
|
||||||
sudo make install
|
|
||||||
|
|
||||||
In addition to building and installing C++ libraries to your system, this will write a jar containing the Java interface to bin/opencv_<version>.jar. We'll use this jar later.
|
or
|
||||||
|
|
||||||
Create an SBT project and use it to generate an Eclipse project
|
.. code-block:: bat
|
||||||
*****************************************************************
|
|
||||||
|
|
||||||
Now we'll create a simple Java application using SBT. This serves as a brief introduction to those unfamiliar with this build tool. We're using SBT because it is particularly easy and powerful.
|
msbuild /m OpenCV.sln /t:Build /p:Configuration=Release /v:m
|
||||||
|
|
||||||
First, download and install SBT using the instructions `here <http://www.scala-sbt.org/>`_.
|
Besides all this will create a ``jar`` containing the Java interface (:file:`bin/opencv_2.4.4.jar`)
|
||||||
|
and a native dynamic library containing Java bindings and all the OpenCV stuff
|
||||||
|
(:file:`bin/Release/opencv_java244.dll` or :file:`bin/libopencv_java244.so` respectively).
|
||||||
|
We'll use these files later.
|
||||||
|
|
||||||
Next, navigate to a new directory where you'd like the application source to live (outside opencv). Let's call it "JavaSample" and create a directory for it:
|
Create a simple Java sample and an Ant build file for it
|
||||||
|
********************************************************
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
The described sample is provided with OpenCV library in the :file:`opencv/samples/java/ant` folder.
|
||||||
|
|
||||||
|
* Create a folder where you'll create this sample application.
|
||||||
|
|
||||||
|
* In this folder create an XML file with the following content using any text editor:
|
||||||
|
|
||||||
|
.. code-block:: xml
|
||||||
|
:linenos:
|
||||||
|
|
||||||
|
<project name="SimpleSample" basedir="." default="rebuild-run">
|
||||||
|
|
||||||
|
<property name="src.dir" value="src"/>
|
||||||
|
|
||||||
|
<property name="lib.dir" value="${ocvJarDir}"/>
|
||||||
|
<path id="classpath">
|
||||||
|
<fileset dir="${lib.dir}" includes="**/*.jar"/>
|
||||||
|
</path>
|
||||||
|
|
||||||
|
<property name="build.dir" value="build"/>
|
||||||
|
<property name="classes.dir" value="${build.dir}/classes"/>
|
||||||
|
<property name="jar.dir" value="${build.dir}/jar"/>
|
||||||
|
|
||||||
|
<property name="main-class" value="${ant.project.name}"/>
|
||||||
|
|
||||||
|
|
||||||
|
<target name="clean">
|
||||||
|
<delete dir="${build.dir}"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="compile">
|
||||||
|
<mkdir dir="${classes.dir}"/>
|
||||||
|
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="jar" depends="compile">
|
||||||
|
<mkdir dir="${jar.dir}"/>
|
||||||
|
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
|
||||||
|
<manifest>
|
||||||
|
<attribute name="Main-Class" value="${main-class}"/>
|
||||||
|
</manifest>
|
||||||
|
</jar>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="run" depends="jar">
|
||||||
|
<java fork="true" classname="${main-class}">
|
||||||
|
<sysproperty key="java.library.path" path="${ocvLibDir}"/>
|
||||||
|
<classpath>
|
||||||
|
<path refid="classpath"/>
|
||||||
|
<path location="${jar.dir}/${ant.project.name}.jar"/>
|
||||||
|
</classpath>
|
||||||
|
</java>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="rebuild" depends="clean,jar"/>
|
||||||
|
|
||||||
|
<target name="rebuild-run" depends="clean,run"/>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
||||||
|
.. note::
|
||||||
|
This XML file can be reused for building other Java applications.
|
||||||
|
It describes a common folder structure in the lines 3 - 12 and common targets
|
||||||
|
for compiling and running the application.
|
||||||
|
|
||||||
|
When reusing this XML don't forget to modify the project name in the line 1,
|
||||||
|
that is also the name of the `main` class (line 14).
|
||||||
|
The paths to OpenCV `jar` and `jni lib` are expected as parameters
|
||||||
|
(``"${ocvJarDir}"`` in line 5 and ``"${ocvLibDir}"`` in line 37), but
|
||||||
|
you can hardcode these paths for your convenience.
|
||||||
|
See `Ant documentation <http://ant.apache.org/manual/>`_ for detailed description
|
||||||
|
of its build file format.
|
||||||
|
|
||||||
|
* Create an :file:`src` folder nect to the :file:`build.xml` file and a :file:`SimpleSample.java` file in it.
|
||||||
|
|
||||||
|
* Put the following Java code into the :file:`SimpleSample.java` file:
|
||||||
|
.. code-block:: java
|
||||||
|
|
||||||
|
import org.opencv.core.Mat;
|
||||||
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Scalar;
|
||||||
|
|
||||||
|
class SimpleSample {
|
||||||
|
|
||||||
|
static{ System.loadLibrary("opencv_java244"); }
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
* run the following command in console in the folder containing :file:`build.xml`:
|
||||||
|
.. code-block:: bash
|
||||||
|
|
||||||
|
ant -DocvJarDir=path/to/dir/containing/opencv-244.jar -DocvLibDir=path/to/dir/containing/opencv_java244/native/library
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
.. code-block:: bat
|
||||||
|
|
||||||
|
ant -DocvJarDir=X:\opencv-2.4.4\bin -DocvLibDir=X:\opencv-2.4.4\bin\Release
|
||||||
|
|
||||||
|
The command should initiate [re]building and running the sample.
|
||||||
|
You should see on the screen something like this:
|
||||||
|
|
||||||
|
.. image:: images/ant_output.png
|
||||||
|
:alt: run app with Ant
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
Create a simple Java project in Eclipse
|
||||||
|
***************************************
|
||||||
|
|
||||||
|
* Create a new Eclipse workspace
|
||||||
|
* Create a new Java project via :guilabel:`File --> New --> Java Project`
|
||||||
|
|
||||||
|
.. image:: images/eclipse_new_java_prj.png
|
||||||
|
:alt: Eclipse: new Java project
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
Call it say "HelloCV".
|
||||||
|
|
||||||
|
* Open :guilabel:`Java Build Path` tab on :guilabel:`Project Properties` dialog
|
||||||
|
and configure additional library (OpenCV) reference (jar and native library location):
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib2.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib3.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib4.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib5.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib6.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib7.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
.. image:: images/eclipse_user_lib8.png
|
||||||
|
:alt: Eclipse: external JAR
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
` `
|
||||||
|
|
||||||
|
* Add a new Java class (say ``Main``) containing the application entry:
|
||||||
|
|
||||||
|
.. image:: images/eclipse_main_class.png
|
||||||
|
:alt: Eclipse: Main class
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
* Put a simple OpenCV calls there, e.g.:
|
||||||
|
.. code-block:: java
|
||||||
|
|
||||||
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Mat;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.loadLibrary("opencv_java244");
|
||||||
|
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
|
||||||
|
System.out.println("m = " + m.dump());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
* Press :guilabel:`Run` button and see the identity matrix content in the Eclipse ``Console`` window.
|
||||||
|
|
||||||
|
.. image:: images/eclipse_run.png
|
||||||
|
:alt: Eclipse: run
|
||||||
|
:align: center
|
||||||
|
|
||||||
|
Create an SBT project and samples in Java and Scala
|
||||||
|
***************************************************
|
||||||
|
|
||||||
|
Now we'll create a simple Java application using SBT. This serves as a brief introduction to
|
||||||
|
those unfamiliar with this build tool. We're using SBT because it is particularly easy and powerful.
|
||||||
|
|
||||||
|
First, download and install `SBT <http://www.scala-sbt.org/>`_ using the instructions on its `web site <http://www.scala-sbt.org/>`_.
|
||||||
|
|
||||||
|
Next, navigate to a new directory where you'd like the application source to live (outside opencv).
|
||||||
|
Let's call it "JavaSample" and create a directory for it:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -91,40 +336,42 @@ Now we will create the necessary folders and an SBT project:
|
|||||||
mkdir -p src/main/java # This is where SBT expects to find Java sources
|
mkdir -p src/main/java # This is where SBT expects to find Java sources
|
||||||
mkdir project # This is where the build definitions live
|
mkdir project # This is where the build definitions live
|
||||||
|
|
||||||
Now open project/build.scala in your favorite editor and paste the following. It defines your project:
|
Now open :file:`project/build.scala` in your favorite editor and paste the following.
|
||||||
|
It defines your project:
|
||||||
|
|
||||||
.. code-block:: scala
|
.. code-block:: scala
|
||||||
|
|
||||||
import sbt._
|
import sbt._
|
||||||
import Keys._
|
import Keys._
|
||||||
|
|
||||||
object JavaSampleBuild extends Build {
|
object JavaSampleBuild extends Build {
|
||||||
def scalaSettings = Seq(
|
def scalaSettings = Seq(
|
||||||
scalaVersion := "2.10.0",
|
scalaVersion := "2.10.0",
|
||||||
scalacOptions ++= Seq(
|
scalacOptions ++= Seq(
|
||||||
"-optimize",
|
"-optimize",
|
||||||
"-unchecked",
|
"-unchecked",
|
||||||
"-deprecation"
|
"-deprecation"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def buildSettings =
|
def buildSettings =
|
||||||
Project.defaultSettings ++
|
Project.defaultSettings ++
|
||||||
scalaSettings
|
scalaSettings
|
||||||
|
|
||||||
lazy val root = {
|
lazy val root = {
|
||||||
val settings = buildSettings ++ Seq(name := "JavaSample")
|
val settings = buildSettings ++ Seq(name := "JavaSample")
|
||||||
Project(id = "JavaSample", base = file("."), settings = settings)
|
Project(id = "JavaSample", base = file("."), settings = settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Now edit project/plugins.sbt and paste the following. This will enable auto-generation of an Eclipse project:
|
Now edit :file:`project/plugins.sbt` and paste the following.
|
||||||
|
This will enable auto-generation of an Eclipse project:
|
||||||
|
|
||||||
.. code-block:: scala
|
.. code-block:: scala
|
||||||
|
|
||||||
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
|
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
|
||||||
|
|
||||||
Now run sbt from the JavaSample root and from within SBT run "eclipse" to generate an eclipse project:
|
Now run ``sbt`` from the :file:`JavaSample` root and from within SBT run ``eclipse`` to generate an eclipse project:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -137,19 +384,22 @@ You should see something like this:
|
|||||||
:alt: SBT output
|
:alt: SBT output
|
||||||
:align: center
|
:align: center
|
||||||
|
|
||||||
You can now import the SBT project using "Import ... -> Existing projects into workspace" from Eclipse. Whether you actually do this is optional for the guide; we'll be using SBT to build the project, so if you choose to use Eclipse it will just be as a text editor.
|
You can now import the SBT project using :guilabel:`Import ... -> Existing projects into workspace` from Eclipse.
|
||||||
|
Whether you actually do this is optional for the guide;
|
||||||
|
we'll be using SBT to build the project, so if you choose to use Eclipse it will just be as a text editor.
|
||||||
|
|
||||||
To test everything is working, create a simple "Hello OpenCV" application. Do this by creating a file "src/main/java/HelloOpenCV.java" with the following contents:
|
To test everything is working, create a simple "Hello OpenCV" application.
|
||||||
|
Do this by creating a file :file:`src/main/java/HelloOpenCV.java` with the following contents:
|
||||||
|
|
||||||
.. code-block:: java
|
.. code-block:: java
|
||||||
|
|
||||||
public class HelloOpenCV {
|
public class HelloOpenCV {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello, OpenCV");
|
System.out.println("Hello, OpenCV");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Now execute "run" from the sbt console, or more concisely, run "sbt run" from the command line:
|
Now execute ``run`` from the sbt console, or more concisely, run ``sbt run`` from the command line:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -166,7 +416,9 @@ Copy the OpenCV jar and write a simple application
|
|||||||
|
|
||||||
Now we'll create a simple face detection application using OpenCV.
|
Now we'll create a simple face detection application using OpenCV.
|
||||||
|
|
||||||
First, create a "lib/" folder and copy the OpenCV jar into it. By default, SBT adds jars in the lib folder to the Java library search path. You can optionally rerun "sbt eclipse" to update your Eclipse project.
|
First, create a :file:`lib/` folder and copy the OpenCV jar into it.
|
||||||
|
By default, SBT adds jars in the lib folder to the Java library search path.
|
||||||
|
You can optionally rerun ``sbt eclipse`` to update your Eclipse project.
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -176,13 +428,14 @@ First, create a "lib/" folder and copy the OpenCV jar into it. By default, SBT a
|
|||||||
|
|
||||||
Next, create the directory src/main/resources and download this Lena image into it:
|
Next, create the directory src/main/resources and download this Lena image into it:
|
||||||
|
|
||||||
.. image:: images/lena.bmp
|
.. image:: images/lena.png
|
||||||
:alt: Lena
|
:alt: Lena
|
||||||
:align: center
|
:align: center
|
||||||
|
|
||||||
Make sure it's called "lena.bmp". Items in the resources directory are available to the Java application at runtime.
|
Make sure it's called :file:`"lena.bmp"`.
|
||||||
|
Items in the resources directory are available to the Java application at runtime.
|
||||||
|
|
||||||
Next, copy lbpcascade_frontalface.xml into the resources directory:
|
Next, copy :file:`lbpcascade_frontalface.xml` into the resources directory:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -200,7 +453,7 @@ Now modify src/main/java/HelloOpenCV.java so it contains the following Java code
|
|||||||
import org.opencv.core.Scalar;
|
import org.opencv.core.Scalar;
|
||||||
import org.opencv.highgui.Highgui;
|
import org.opencv.highgui.Highgui;
|
||||||
import org.opencv.objdetect.CascadeClassifier;
|
import org.opencv.objdetect.CascadeClassifier;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Detects faces in an image, draws boxes around them, and writes the results
|
// Detects faces in an image, draws boxes around them, and writes the results
|
||||||
// to "faceDetection.png".
|
// to "faceDetection.png".
|
||||||
@ -238,14 +491,17 @@ Now modify src/main/java/HelloOpenCV.java so it contains the following Java code
|
|||||||
System.out.println("Hello, OpenCV");
|
System.out.println("Hello, OpenCV");
|
||||||
|
|
||||||
// Load the native library.
|
// Load the native library.
|
||||||
System.loadLibrary("opencv_java");
|
System.loadLibrary("opencv_java244");
|
||||||
new DetectFaceDemo().run();
|
new DetectFaceDemo().run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Note the call to "System.loadLibrary("opencv_java")". This command must be executed exactly once per Java process prior to using any native OpenCV methods. If you don't call it, you will get UnsatisfiedLink errors. You will also get errors if you try to load OpenCV when it has already been loaded.
|
Note the call to ``System.loadLibrary("opencv_java244")``.
|
||||||
|
This command must be executed exactly once per Java process prior to using any native OpenCV methods.
|
||||||
|
If you don't call it, you will get ``UnsatisfiedLink errors``.
|
||||||
|
You will also get errors if you try to load OpenCV when it has already been loaded.
|
||||||
|
|
||||||
Now run the face detection app using "sbt run":
|
Now run the face detection app using ``sbt run``:
|
||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
@ -257,7 +513,7 @@ You should see something like this:
|
|||||||
:alt: SBT run
|
:alt: SBT run
|
||||||
:align: center
|
:align: center
|
||||||
|
|
||||||
It should also write the following image to faceDetection.png:
|
It should also write the following image to :file:`faceDetection.png`:
|
||||||
|
|
||||||
.. image:: images/faceDetection.png
|
.. image:: images/faceDetection.png
|
||||||
:alt: Detected face
|
:alt: Detected face
|
||||||
|
@ -111,9 +111,9 @@ Here you can read tutorials about how to set up your computer to work with the O
|
|||||||
|
|
||||||
*Compatibility:* > OpenCV 2.4.4
|
*Compatibility:* > OpenCV 2.4.4
|
||||||
|
|
||||||
*Author:* |Author_VsevolodG|
|
*Author:* |Author_EricCh|
|
||||||
|
|
||||||
Explains how to build and run a simple desktop Java application using Eclipse and the Simple Build Tool (SBT).
|
Explains how to build and run a simple desktop Java application using Eclipse, Ant or the Simple Build Tool (SBT).
|
||||||
|
|
||||||
================ =================================================
|
================ =================================================
|
||||||
|
|
||||||
@ -258,7 +258,7 @@ Here you can read tutorials about how to set up your computer to work with the O
|
|||||||
../linux_eclipse/linux_eclipse
|
../linux_eclipse/linux_eclipse
|
||||||
../windows_install/windows_install
|
../windows_install/windows_install
|
||||||
../windows_visual_studio_Opencv/windows_visual_studio_Opencv
|
../windows_visual_studio_Opencv/windows_visual_studio_Opencv
|
||||||
../android_binary_package/java_dev_intro
|
../desktop_java/java_dev_intro
|
||||||
../android_binary_package/android_dev_intro
|
../android_binary_package/android_dev_intro
|
||||||
../android_binary_package/O4A_SDK
|
../android_binary_package/O4A_SDK
|
||||||
../android_binary_package/dev_with_OCV_on_Android
|
../android_binary_package/dev_with_OCV_on_Android
|
||||||
|
49
samples/java/ant/build.xml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<project name="SimpleSample" basedir="." default="rebuild-run">
|
||||||
|
|
||||||
|
<property name="src.dir" value="src"/>
|
||||||
|
|
||||||
|
<property name="lib.dir" value="${ocvJarDir}"/>
|
||||||
|
<path id="classpath">
|
||||||
|
<fileset dir="${lib.dir}" includes="**/*.jar"/>
|
||||||
|
</path>
|
||||||
|
|
||||||
|
<property name="build.dir" value="build"/>
|
||||||
|
<property name="classes.dir" value="${build.dir}/classes"/>
|
||||||
|
<property name="jar.dir" value="${build.dir}/jar"/>
|
||||||
|
|
||||||
|
<property name="main-class" value="${ant.project.name}"/>
|
||||||
|
|
||||||
|
|
||||||
|
<target name="clean">
|
||||||
|
<delete dir="${build.dir}"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="compile">
|
||||||
|
<mkdir dir="${classes.dir}"/>
|
||||||
|
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="jar" depends="compile">
|
||||||
|
<mkdir dir="${jar.dir}"/>
|
||||||
|
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
|
||||||
|
<manifest>
|
||||||
|
<attribute name="Main-Class" value="${main-class}"/>
|
||||||
|
</manifest>
|
||||||
|
</jar>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="run" depends="jar">
|
||||||
|
<java fork="true" classname="${main-class}">
|
||||||
|
<sysproperty key="java.library.path" path="${ocvLibDir}"/>
|
||||||
|
<classpath>
|
||||||
|
<path refid="classpath"/>
|
||||||
|
<path location="${jar.dir}/${ant.project.name}.jar"/>
|
||||||
|
</classpath>
|
||||||
|
</java>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="rebuild" depends="clean,jar"/>
|
||||||
|
|
||||||
|
<target name="rebuild-run" depends="clean,run"/>
|
||||||
|
|
||||||
|
</project>
|
19
samples/java/ant/src/SimpleSample.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import org.opencv.core.Mat;
|
||||||
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Scalar;
|
||||||
|
|
||||||
|
class SimpleSample {
|
||||||
|
|
||||||
|
static{ System.loadLibrary("opencv_java244"); }
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
7
samples/java/eclipse/HelloCV/.classpath
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/opencv-2.4.4"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
17
samples/java/eclipse/HelloCV/.project
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>HelloCV</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
@ -0,0 +1,11 @@
|
|||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.7
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
|
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.7
|
12
samples/java/eclipse/HelloCV/src/Main.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import org.opencv.core.CvType;
|
||||||
|
import org.opencv.core.Mat;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.loadLibrary("opencv_java244");
|
||||||
|
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
|
||||||
|
System.out.println("m = " + m.dump());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,5 +4,3 @@ To run the examples:
|
|||||||
1) Install OpenCV and copy the OpenCV jar to lib/. This jar must match the native libraries installed in your system. If this isn't the case, you may get a java.lang.UnsatisfiedLinkError at runtime.
|
1) Install OpenCV and copy the OpenCV jar to lib/. This jar must match the native libraries installed in your system. If this isn't the case, you may get a java.lang.UnsatisfiedLinkError at runtime.
|
||||||
|
|
||||||
3) Go to the root directory and type "sbt/sbt run". This should generate images in your current directory.
|
3) Go to the root directory and type "sbt/sbt run". This should generate images in your current directory.
|
||||||
|
|
||||||
MIT License
|
|
@ -11,8 +11,8 @@ object OpenCVJavaDemoBuild extends Build {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def buildSettings =
|
def buildSettings =
|
||||||
Project.defaultSettings ++
|
Project.defaultSettings ++
|
||||||
scalaSettings
|
scalaSettings
|
||||||
|
|
||||||
lazy val root = {
|
lazy val root = {
|
@ -1,2 +1 @@
|
|||||||
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
|
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
|
||||||
|
|
0
samples/java/sbt/sbt → samples/java/sbt/sbt/sbt
Executable file → Normal file
44
samples/java/sbt/src/main/java/DetectFaceDemo.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import org.opencv.core.Core;
|
||||||
|
import org.opencv.core.Mat;
|
||||||
|
import org.opencv.core.MatOfRect;
|
||||||
|
import org.opencv.core.Point;
|
||||||
|
import org.opencv.core.Rect;
|
||||||
|
import org.opencv.core.Scalar;
|
||||||
|
import org.opencv.highgui.Highgui;
|
||||||
|
import org.opencv.objdetect.CascadeClassifier;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Detects faces in an image, draws boxes around them, and writes the results
|
||||||
|
* to "faceDetection.png".
|
||||||
|
*/
|
||||||
|
public class DetectFaceDemo {
|
||||||
|
public void run() {
|
||||||
|
System.out.println("\nRunning DetectFaceDemo");
|
||||||
|
|
||||||
|
// Create a face detector from the cascade file in the resources
|
||||||
|
// directory.
|
||||||
|
CascadeClassifier faceDetector = new CascadeClassifier(getClass()
|
||||||
|
.getResource("/lbpcascade_frontalface.xml").getPath());
|
||||||
|
Mat image = Highgui.imread(getClass().getResource(
|
||||||
|
"/AverageMaleFace.jpg").getPath());
|
||||||
|
|
||||||
|
// Detect faces in the image.
|
||||||
|
// MatOfRect is a special container class for Rect.
|
||||||
|
MatOfRect faceDetections = new MatOfRect();
|
||||||
|
faceDetector.detectMultiScale(image, faceDetections);
|
||||||
|
|
||||||
|
System.out.println(String.format("Detected %s faces",
|
||||||
|
faceDetections.toArray().length));
|
||||||
|
|
||||||
|
// Draw a bounding box around each face.
|
||||||
|
for (Rect rect : faceDetections.toArray()) {
|
||||||
|
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
|
||||||
|
+ rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the visualized detection.
|
||||||
|
String filename = "faceDetection.png";
|
||||||
|
System.out.println(String.format("Writing %s", filename));
|
||||||
|
Highgui.imwrite(filename, image);
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
@ -4,9 +4,9 @@
|
|||||||
* demonstrating the generic nature of the interface.
|
* demonstrating the generic nature of the interface.
|
||||||
* The other demos are in Java.
|
* The other demos are in Java.
|
||||||
* Currently, all demos are run, sequentially.
|
* Currently, all demos are run, sequentially.
|
||||||
*
|
*
|
||||||
* You're invited to submit your own examples, in any JVM language of
|
* You're invited to submit your own examples, in any JVM language of
|
||||||
* your choosing so long as you can get them to build.
|
* your choosing so long as you can get them to build.
|
||||||
*/
|
*/
|
||||||
object Main extends App {
|
object Main extends App {
|
||||||
// We must load the native library before using any OpenCV functions.
|
// We must load the native library before using any OpenCV functions.
|
@ -16,7 +16,7 @@ import reflect._
|
|||||||
object ScalaCorrespondenceMatchingDemo {
|
object ScalaCorrespondenceMatchingDemo {
|
||||||
def run() {
|
def run() {
|
||||||
println(s"\nRunning ${classTag[this.type].toString.replace("$", "")}")
|
println(s"\nRunning ${classTag[this.type].toString.replace("$", "")}")
|
||||||
|
|
||||||
// Detects keypoints and extracts descriptors in a given image of type Mat.
|
// Detects keypoints and extracts descriptors in a given image of type Mat.
|
||||||
def detectAndExtract(mat: Mat) = {
|
def detectAndExtract(mat: Mat) = {
|
||||||
// A special container class for KeyPoint.
|
// A special container class for KeyPoint.
|
||||||
@ -27,7 +27,7 @@ object ScalaCorrespondenceMatchingDemo {
|
|||||||
|
|
||||||
println(s"There were ${keyPoints.toArray.size} KeyPoints detected")
|
println(s"There were ${keyPoints.toArray.size} KeyPoints detected")
|
||||||
|
|
||||||
// Let's just use the best KeyPoints.
|
// Let's just use the best KeyPoints.
|
||||||
val sorted = keyPoints.toArray.sortBy(_.response).reverse.take(50)
|
val sorted = keyPoints.toArray.sortBy(_.response).reverse.take(50)
|
||||||
// There isn't a constructor that takes Array[KeyPoint], so we unpack
|
// There isn't a constructor that takes Array[KeyPoint], so we unpack
|
||||||
// the array and use the constructor that can take any number of
|
// the array and use the constructor that can take any number of
|
||||||
@ -44,7 +44,7 @@ object ScalaCorrespondenceMatchingDemo {
|
|||||||
(bestKeyPoints, descriptors)
|
(bestKeyPoints, descriptors)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the images from the |resources| directory.
|
// Load the images from the |resources| directory.
|
||||||
val leftImage = Highgui.imread(getClass.getResource("/img1.bmp").getPath)
|
val leftImage = Highgui.imread(getClass.getResource("/img1.bmp").getPath)
|
||||||
val rightImage = Highgui.imread(getClass.getResource("/img2.bmp").getPath)
|
val rightImage = Highgui.imread(getClass.getResource("/img2.bmp").getPath)
|
||||||
|
|
@ -1,44 +0,0 @@
|
|||||||
import org.opencv.core.Core;
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.MatOfRect;
|
|
||||||
import org.opencv.core.Point;
|
|
||||||
import org.opencv.core.Rect;
|
|
||||||
import org.opencv.core.Scalar;
|
|
||||||
import org.opencv.highgui.Highgui;
|
|
||||||
import org.opencv.objdetect.CascadeClassifier;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Detects faces in an image, draws boxes around them, and writes the results
|
|
||||||
* to "faceDetection.png".
|
|
||||||
*/
|
|
||||||
public class DetectFaceDemo {
|
|
||||||
public void run() {
|
|
||||||
System.out.println("\nRunning DetectFaceDemo");
|
|
||||||
|
|
||||||
// Create a face detector from the cascade file in the resources
|
|
||||||
// directory.
|
|
||||||
CascadeClassifier faceDetector = new CascadeClassifier(getClass()
|
|
||||||
.getResource("/lbpcascade_frontalface.xml").getPath());
|
|
||||||
Mat image = Highgui.imread(getClass().getResource(
|
|
||||||
"/AverageMaleFace.jpg").getPath());
|
|
||||||
|
|
||||||
// Detect faces in the image.
|
|
||||||
// MatOfRect is a special container class for Rect.
|
|
||||||
MatOfRect faceDetections = new MatOfRect();
|
|
||||||
faceDetector.detectMultiScale(image, faceDetections);
|
|
||||||
|
|
||||||
System.out.println(String.format("Detected %s faces",
|
|
||||||
faceDetections.toArray().length));
|
|
||||||
|
|
||||||
// Draw a bounding box around each face.
|
|
||||||
for (Rect rect : faceDetections.toArray()) {
|
|
||||||
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
|
|
||||||
+ rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save the visualized detection.
|
|
||||||
String filename = "faceDetection.png";
|
|
||||||
System.out.println(String.format("Writing %s", filename));
|
|
||||||
Highgui.imwrite(filename, image);
|
|
||||||
}
|
|
||||||
}
|
|