Repository: opencv-java/opencv-java-tutorials Branch: master Commit: 3ac9e2bf7785 Files: 17 Total size: 108.1 KB Directory structure: gitextract_0mwx2_eh/ ├── .gitattributes ├── .gitignore ├── .readthedocs.yaml ├── README.md └── docs/ ├── Makefile ├── make.bat └── source/ ├── 01-installing-opencv-for-java.rst ├── 02-first-java-application-with-opencv.rst ├── 03-first-javafx-application-with-opencv.rst ├── 04-opencv-basics.rst ├── 05-fourier-transform.rst ├── 06-face-detection-and-tracking.rst ├── 07-image-segmentation.rst ├── 08-object-detection.rst ├── 09-camera-calibration.rst ├── conf.py └── index.rst ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitattributes ================================================ # Auto detect text files and perform LF normalization * text=auto # Custom for Visual Studio *.cs diff=csharp *.sln merge=union *.csproj merge=union *.vbproj merge=union *.fsproj merge=union *.dbproj merge=union # Standard to msysgit *.doc diff=astextplain *.DOC diff=astextplain *.docx diff=astextplain *.DOCX diff=astextplain *.dot diff=astextplain *.DOT diff=astextplain *.pdf diff=astextplain *.PDF diff=astextplain *.rtf diff=astextplain *.RTF diff=astextplain ================================================ FILE: .gitignore ================================================ # Windows image file caches Thumbs.db ehthumbs.db # Folder config file Desktop.ini # Recycle Bin used on file shares $RECYCLE.BIN/ # Windows Installer files *.cab *.msi *.msm *.msp # ========================= # Operating System Files # ========================= # OSX # ========================= .DS_Store .AppleDouble .LSOverride # Icon must end with two \r Icon # Thumbnails ._* # Files that might appear on external disk .Spotlight-V100 .Trashes # Directories potentially created on remote AFP share .AppleDB .AppleDesktop Network Trash Folder Temporary Items .apdisk # ======= # Other # ======= build/ ================================================ FILE: .readthedocs.yaml ================================================ # Read the Docs configuration file for Sphinx projects # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details # Required version: 2 # Set the OS, Python version and other tools you might need build: os: ubuntu-22.04 tools: python: "3.11" # You can also specify other tool versions: # nodejs: "19" # rust: "1.64" # golang: "1.19" # Build documentation in the "docs/" directory with Sphinx sphinx: configuration: docs/source/conf.py # Optionally build your docs in additional formats such as PDF and ePub # formats: # - pdf # - epub # Optional but recommended, declare the Python requirements required # to build your documentation # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html # python: # install: # - requirements: docs/requirements.txt ================================================ FILE: README.md ================================================ OpenCV Java Tutorials ============================ [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) Look at the tutorials on using OpenCV with Java (and JavaFX) at: http://opencv-java-tutorials.readthedocs.org/en/latest/index.html If you are interested in mantaining these repos, please, contact me: https://github.com/orgs/opencv-java/people ================================================ FILE: docs/Makefile ================================================ # Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = build # User-friendly check for sphinx-build ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) endif # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " applehelp to make an Apple Help Book" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " xml to make Docutils-native XML files" @echo " pseudoxml to make pseudoxml-XML files for display purposes" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" @echo " coverage to run coverage check of the documentation (if enabled)" clean: rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/OpenCVJavaTutorials.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/OpenCVJavaTutorials.qhc" applehelp: $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp @echo @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." @echo "N.B. You won't be able to view it unless you put it in" \ "~/Library/Documentation/Help or install it in your application" \ "bundle." devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/OpenCVJavaTutorials" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/OpenCVJavaTutorials" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." latexpdfja: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through platex and dvipdfmx..." $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." coverage: $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage @echo "Testing of coverage in the sources finished, look at the " \ "results in $(BUILDDIR)/coverage/python.txt." xml: $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml @echo @echo "Build finished. The XML files are in $(BUILDDIR)/xml." pseudoxml: $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml @echo @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." ================================================ FILE: docs/make.bat ================================================ @ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source set I18NSPHINXOPTS=%SPHINXOPTS% source if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. xml to make Docutils-native XML files echo. pseudoxml to make pseudoxml-XML files for display purposes echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled echo. coverage to run coverage check of the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) REM Check if sphinx-build is available and fallback to Python version if any %SPHINXBUILD% 2> nul if errorlevel 9009 goto sphinx_python goto sphinx_ok :sphinx_python set SPHINXBUILD=python -m sphinx.__init__ %SPHINXBUILD% 2> nul if errorlevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point echo.to the full path of the 'sphinx-build' executable. Alternatively you echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from echo.http://sphinx-doc.org/ exit /b 1 ) :sphinx_ok if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\OpenCVJavaTutorials.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\OpenCVJavaTutorials.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdf" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf cd %~dp0 echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdfja" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf-ja cd %~dp0 echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) if "%1" == "coverage" ( %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage if errorlevel 1 exit /b 1 echo. echo.Testing of coverage in the sources finished, look at the ^ results in %BUILDDIR%/coverage/python.txt. goto end ) if "%1" == "xml" ( %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml if errorlevel 1 exit /b 1 echo. echo.Build finished. The XML files are in %BUILDDIR%/xml. goto end ) if "%1" == "pseudoxml" ( %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml if errorlevel 1 exit /b 1 echo. echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. goto end ) :end ================================================ FILE: docs/source/01-installing-opencv-for-java.rst ================================================ ========================== Installing OpenCV for Java ========================== Introduction to OpenCV for Java -------------------------------- As of OpenCV 2.4.4, OpenCV supports desktop Java development. This tutorial will help you install OpenCV on your desktop operating system. Install the latest Java version -------------------------------- Download the latest Java JDK from the `Oracle `_ website. Now you should be able to install the latest Java JDK by opening the file just downloaded. Install the latest Eclipse version ----------------------------------- Download the latest Eclipse version at the `Eclipse Download page `_ choosing the ``Eclipse IDE for Java Developers`` version (suggested). Extract the downloaded compressed file and put the resulting folder wherever you want to. You don't need to install anything. Alternatively, you can try the Eclipse installer. Install OpenCV 3.x under Windows ------------------------------------ First of all you should download the OpenCV library (version 3.x) from `here `_. Then, extract the downloaded OpenCV file in a location of your choice. Once you get the folder ``opencv`` put in wherever you prefer. Now the only two things that you will need are: the ``opencv-3xx.jar`` file located at ``\opencv\build\java`` and the ``opencv_java3xx.dll`` library located at ``\opencv\build\java\x64`` (for 64-bit systems) or ``\opencv\build\java\x86`` (for 32-bit systems). The `3xx` suffix of each file is a shortcut for the current OpenCV version, e.g., it will be `300` for OpenCV 3.0 and `330` for OpenCV 3.3. Install OpenCV 3.x under macOS --------------------------------- The quickest way to obtain OpenCV under macOS is to use `Homebrew `_. After installing Homebrew, you have to check whether the `XCode Command Line Tools` are already installed on your system. To do so, open the `Terminal` and execute: ``xcode-select --install`` If macOS asks for installing such tools, proceed with the download and installation. Otherwise, continue with the OpenCV installation. As a prerequisite, check that Apache Ant is installed. Otherwise, install it with Homebrew: ``brew install ant``. Ant should be available at ``/usr/local/bin/ant``. To install OpenCV (with Java support) through Homebrew, you need to edit the *opencv* formula in Homebrew, to add support for Java: ``brew edit opencv`` In the text editor that will open, change the line: ``-DBUILD_opencv_java=OFF`` in ``-DBUILD_opencv_java=ON`` then, after saving the file, you can effectively install OpenCV: ``brew install --build-from-source opencv`` After the installation of OpenCV, the needed jar file and the dylib library will be located at ``/usr/local/Cellar/opencv/3.x.x/share/OpenCV/java/``, e.g., ``/usr/local/Cellar/opencv/3.3.1/share/OpenCV/java/``. Please, notice that this method doesn't work if you update OpenCV from a previous version: you need to uninstall OpenCV and install it again. Install OpenCV 3.x under Linux --------------------------------- Please, note: the following instructions are also useful if you want to compile OpenCV under Windows or macOS. Linux package management systems (`apt-get`, `yum`, etc.) *may* provide the needed version of the OpenCV library. As first step, download and install `CMake `_ and `Apache Ant `_, if you don't have any of these. Download the OpenCV library from `its website `_. Extract the downloaded OpenCV file in a location of your choice and open CMake ( cmake-gui ). Put the location of the extracted OpenCV library in the ``Where is the source code field`` (e.g., /opencv/) and insert the destination directory of your build in the ``Where to build the binaries`` field (e.g., /opencv/build). At last, check the ``Grouped`` and ``Advanced`` checkboxes. .. image:: _static/01-00.png Now press ``Configure`` and use the default compilers for ``Unix Makefiles``. Please, be sure to have installed a C/C++ compiler. In the ``Ungrouped Entries`` group, insert the path to the Apache Ant (Add entry with the ``ANT_EXECUTABLE`` name) executable (e.g., ``/apache-ant-1.9.6/bin/ant``). In the ``BUILD`` group, unselect: * ``BUILD_PERF_TESTS`` * ``BUILD_SHARED_LIBRARY`` to make the Java bindings dynamic library all-sufficient * ``BUILD_TESTS`` * ``BUILD_opencv_python`` In the ``CMAKE`` group, set to ``Debug`` (or ``Release``) the ``CMAKE_BUILD_TYPE`` In the ``JAVA`` group: * insert the Java AWT include path (e.g., ``/usr/lib/jvm/java-1.8.0/include/``) * insert the Java AWT library path (e.g., ``/usr/lib/jvm/java-1.8.0/include/jawt.h``) * insert the Java include path (e.g., ``/usr/lib/jvm/java-1.8.0/include/``) * insert the alternative Java include path (e.g., ``/usr/lib/jvm/java-1.8.0/include/linux``) * insert the JVM library path (e.g., ``/usr/lib/jvm/java-1.8.0/include/jni.h``) Press ``Configure`` twice, and the CMake window should appear with a white background. If not, fix the red lines and press ``Configure`` again. Now, press ``Generate`` and close CMake. .. image:: _static/01 - 01.png Now open the terminal, go to the ``build`` folder of OpenCV and compile everything with the command: ``make -j``. Notice that the `-j` flag tells `make` to run in parallel with the maximum number of allowed job threads, which makes the build theoretically faster. Wait for the process to be completed... If everything went well you should have ``opencv-3xx.jar`` in the ``/opencv/build/bin`` directory and ``libopencv_java3xx.so`` in the ``/opencv/build/lib`` directory. The `3xx` suffix of each file is a shortcut for the current OpenCV version, e.g., it will be `300` for OpenCV 3.0 and `330` for OpenCV 3.3. This is everything you need. Set up OpenCV for Java in Eclipse ---------------------------------- Open Eclipse and select a workspace of your choice. Create a User Library, ready to be used on all your next projects: go to ``Window > Preferences...``. .. image:: _static/01 - 02.png From the menu navigate under ``Java > Build Path > User Libraries`` and choose ``New...``. Enter a name for the library (e.g., opencv) and select the newly created user library. Choose ``Add External JARs...``, browse to select ``opencv-3xx.jar`` from your computer. After adding the jar, extend it, select ``Native library location`` and press ``Edit...``. .. image:: _static/01 - 03.png Select ``External Folder...`` and browse to select the folder containing the OpenCV libraries (e.g., ``C:\opencv\build\java\x64`` under Windows). In case of MacOS, if you installed OpenCV *without* Homebrew, you need to create a soft link with .dylib extension for the .so file. E.g., from the terminal, type: ``ln -s libopencv_java300.so libopencv_java300.dylib`` Set up OpenCV for Java in other IDEs --------------------------------------------------- If you are using IntelliJ, you can specify the location of the library with the VM options argument in Run/Debug Configuration ``-Djava.library.path=/opencv/build/lib``. .. image:: _static/01 - 04.png .. image:: _static/01 - 05.png ================================================ FILE: docs/source/02-first-java-application-with-opencv.rst ================================================ ======================================= Your First Java Application with OpenCV ======================================= .. note:: We assume that by now you have already read the previous tutorials. If not, please check previous tutorials at ``_. You can also find the source code and resources at ``_ A Java application with OpenCV ------------------------------ This tutorial will guide you through the creation of a simple Java console application using the OpenCV library in Eclipse. What we will do in this tutorial -------------------------------- In this guide, we will: * Create a new Java Project * Add a User Library to the project * Write some OpenCV code * Build and Run the application Create a New Project -------------------- Open Eclipse and create a new Java project; open the ``File`` menu, go to ``New`` and click on ``Java Project``. .. image:: _static/02-00.png In the ``New Java Project`` dialog write the name of your project and click on ``Finish``. Add a User Library ------------------ If you followed the previous tutorial (``Installing OpenCV for Java``), you should already have the OpenCV library set in your workspace's user libraries; if not please check out the previous tutorial. Now you should be ready to add the library to your project. Inside Eclipse's ``Package Explorer`` just right-click on your project's folder and go to ``Build Path --> Add Libraries...``. .. image:: _static/02-01.png Select ``User Libraries`` and click on ``Next``, check the checkbox of the OpenCV library and click ``Finish``. .. image:: _static/02-02.png Create a simple application --------------------------- Now add a new Class to your project by right-clicking on your project's folder and go to ``New --> Class``. Write a name of your choice for both the package and the class then click on ``Finish``. Now we are ready to write the code of our first application. Let's start by defining the ``main`` method: .. code-block:: java 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()); } } First of all we need to load the OpenCV Native Library previously set on our project. .. code-block:: java System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Then we can define a new Mat. .. note:: The class **Mat** represents an n-dimensional dense numerical single-channel or multi-channel array. It can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms. For more details check out the OpenCV `page `_. .. code-block:: java Mat mat = Mat.eye(3, 3, CvType.CV_8UC1); The ``Mat.eye`` represents an identity matrix, we set the dimensions of it (3x3) and the type of its elements. As you can notice, if you leave the code just like this, you will get some error; this is due to the fact that eclipse can't resolve some variables. You can locate your mouse cursor on the words that seems to be an error(red underlined line) and wait for a dialog box to pop up and click on the voice ``Import...``. If you do that for all the variables we have added to the code the following rows: .. code-block:: java import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; We can now try to build and run our application by clicking on the Run button. You should have the following output: .. image:: _static/02-03.png The whole source code is available on `GitHub `_. ================================================ FILE: docs/source/03-first-javafx-application-with-opencv.rst ================================================ ========================================= Your First JavaFX Application with OpenCV ========================================= .. note:: We assume that by now you have already read the previous tutorials. If not, please check previous tutorials at ``_. You can also find the source code and resources at ``_ A JavaFX application with OpenCV -------------------------------- This tutorial will guide you through the creation of a simple JavaFX GUI application using the OpenCV library in Eclipse. What we will do in this tutorial -------------------------------- In this guide, we will: * Install the ``e(fx)clipse`` plugin and (optionally) *Scene Builder*. * Work with *Scene Builder*. * Write and Run our application. Your First Application in JavaFX -------------------------------- The application you will write by following this tutorial is going to capture a video stream from a webcam and, then, it will display it on the user interface (GUI). We will create the GUI with Scene Builder: it will have a button, which will allow us to start and stop the stream, and a simple image view container where we will put each stream frame. Installing e(fx)clipse plugin and Scene Builder ----------------------------------------------- In Eclipse, install the ``e(fx)clipse`` plugin, by following the guide at ``_. If you choose not to install such a plugin, you have to create a traditional **Java project**, only. Download and install *JavaFX Scene Builder* 2.0 from ``_. Now you can create a new JavaFX project. ``Go to File > New > Project...`` and select ``JavaFX project...``. .. image:: _static/03-00.png Choose a name for your project and click ``Next``. .. image:: _static/03-01.png Now add your OpenCV user library to your project and click ``Next``. .. image:: _static/03-02.png Choose a name for your package, for the *FXML file* and for the *Controller Class*. The *FXML file* will contain the description of your GUI in FXML language, while the *Controller Class* will handle all the method and event which have to be called and managed when the user interacts with the GUI's components. .. image:: _static/03-03.png Working with Scene Builder -------------------------- If you have installed *Scene Builder* you can now right click on your *FXML file* in Eclipse and select ``Open with SceneBuilder``. *Scene Builder* can help construct you gui by interacting with a graphic interface; this allows you to see a real time preview of your window and modify your components and their position just by editing the graphic preview. Let's take a look at what I'm talking about. At fist the *FXML file* will have just an *AnchorPane*. An AnchorPane allows the edges of child nodes to be anchored to an offset from the anchorpane's edges. If the anchorpane has a border and/or padding set, the offsets will be measured from the inside edge of those insets. The anchorpane lays out each managed child regardless of the child's visible property value; unmanaged children are ignored for all layout calculations. You can go ahead and delete the anchorpane and add a *BorderPane* instead. A BorderPane lays out children in top, left, right, bottom, and center positions. .. image:: _static/03-04.png You can add a BorderPane by dragging from the ``Container`` menu a borderpane and then drop it in the ``Hierarchy`` menu. Now we can add the button that will allow us to start and stop the stream. Take a button component from the ``Controls`` menu and drop it on the **BOTTOM** field of our BP. As we can see, on the right we will get three menus (Properties, Layout, Code) which are used to customize our selected component. For example we can change text of our button in "Start Camera" in the ``Text`` field under the ``Properties`` menu and the id of the button (e.g. "start_btn") in the ``fx:id`` field under the ``Code`` menu. .. image:: _static/03-05.png .. image:: _static/03-06.png We are going to need the id of the button later, in order to edit the button properties from our *Controller*'s methods. As you can see our button is too close to the edge of the windows, so we should add some bottom margin to it; to do so we can add this information in the ``Layout`` menu. In order to make the button work, we have to set the name of the method (e.g. "startCamera") that will execute the action we want to preform in the field ``OnAction`` under the ``Code`` menu. .. image:: _static/03-07.png Now, we shall add an *ImageView* component from the ``Controls`` menu into the **CENTER** field of our BP. Let's also edit the id of the image view (e.g. "currentFrame"), and add some margin to it. .. image:: _static/03-08.png Finally we have to tell which Controller class will mange the GUI, we can do so by adding our controller class name in the ``Controller class`` field under the ``Controller`` menu located in the bottom left corner of the window. We just created our first GUI by using Scene Builder, if you save the file and return to Eclipse you will notice that some FXML code has been generated automatically. Key Concepts in JavaFX ---------------------- The **Stage** is where the application will be displayed (e.g., a Windows' window). A **Scene** is one container of Nodes that compose one "page" of your application. A **Node** is an element in the Scene, with a visual appearance and an interactive behavior. Nodes may be hierarchically nested . In the *Main class* we have to pass to the *start* function our *primary stage*: .. code-block:: java public void start(Stage primaryStage) and load the fxml file that will populate our stage, the *root element* of the scene and the controller class: .. code-block:: java FXMLLoader loader = new FXMLLoader(getClass().getResource("FXHelloCV.fxml")); BorderPane root = (BorderPane) loader.load(); FXController controller = loader.getController(); Managing GUI Interactions With the Controller Class --------------------------------------------------- For our application we need to do basically two thing: control the button push and the refreshment of the image view. To do so we have to create a reference between the gui components and a variable used in our controller class: .. code-block:: java @FXML private Button button; @FXML private ImageView currentFrame; The ``@FXML`` tag means that we are linking our variable to an element of the fxml file and the value used to declare the variable has to equal to the id set for that specific element. The ``@FXML`` tag is used with the same meaning for the Actions set under the Code menu in a specific element. for: .. code-block:: xml