JavaCV

repository·master·Indexed 27 days ago

https://github.com/bytedeco/javacv

A Java wrapper for various computer vision and multimedia libraries, including OpenCV, FFmpeg, Tesseract, Leptonica, and others. It provides a Java interface for video and image processing, featuring utilities for hardware-accelerated display, parallelism, calibration, and integration with UI frameworks like JavaFX via JavaFXFrameConverter.

Tokens
1.4K
Snippets
3
Records
8
Agent score
44%

What's inside JavaCV

  1. Overview of JavaCV Features and Utilities

    master

    JavaCV provides wrappers for several computer vision and multimedia libraries, including:

    • OpenCV, FFmpeg, Tesseract, Leptonica, libdc1394, FlyCapture, Spinnaker, OpenKinect, librealsense, CL PS3 Eye Driver, videoInput, and ARToolKitPlus.

    Key Utility Classes:

    • Display: CanvasFrame and GLCanvasFrame for hardware-accelerated full-screen image display.
    • Parallelism: Parallel for executing code on multiple cores.
    • Calibration: GeometricCalibrator, ProCamGeometricCalibrator, and ProCamColorCalibrator.
    • Feature Detection: ObjectFinder for detecting and matching feature points.
    • Alignment: GNImageAligner, ProjectiveTransformer, ProjectiveColorTransformer, ProCamTransformer, and ReflectanceInitializer for projector-camera alignment.
    • Analysis: Blobs for blob analysis.
    • Hardware Acceleration: Classes ending in CL (OpenCL) or starting with GL (OpenGL), such as JavaCVCL and GLCanvasFrame.
  2. Required Software for JavaCV

    master

    JavaCV requires:

    • Java SE 8 or newer (OpenJDK, Oracle JDK, IBM JDK, or Microsoft JDK).

    Optional/Platform-specific requirements:

    • CL Eye Platform SDK (Windows only).
    • Android SDK API 24 or newer.
    • JOCL and JOGL from JogAmp.

    CRITICAL: Ensure all modules have the same bitness. 32-bit and 64-bit modules must not be mixed.

  3. Install JavaCV via Maven, Gradle, Leiningen, or sbt

    master

    You can automatically download and install JavaCV and its platform-specific binaries using the following dependency management tools. Using javacv-platform ensures that all necessary binary dependencies for all platforms are included.

    Important: Ensure you use the artifact ID javacv-platform rather than just javacv to include the required binaries.

    <!-- Maven -->
    <dependency>
      <groupId>org.bytedeco</groupId>
      <artifactId>javacv-platform</artifactId>
      <version>1.5.13</version>
    </dependency>
    
    <!-- Gradle -->
    dependencies {
      implementation("org.bytedeco:javacv-platform:1.5.13")
    }
    
    <!-- Leiningen -->
    :dependencies [
      [org.bytedeco/javacv-platform "1.5.13"]
    ]
    
    <!-- sbt -->
    libraryDependencies += "org.bytedeco" % "javacv-platform" % "1.5.13"
  4. Limit JavaCV binaries to a specific platform

    master

    By default, javacv-platform downloads binaries for all platforms. To reduce the footprint and download only binaries for a specific platform, set the javaccpp.platform system property using the -D command line option.

    Examples of platform values:

    • android-arm
    • linux-x86_64
    • macosx-x86_64
    • windows-x86_64
  5. Run JavaCV Demo with Maven

    master

    If you have a pom.xml configured with javacv-platform and a source file named Demo.java, you can compile and execute the application using the following Maven command:

    $ mvn compile exec:java -Dexec.mainClass=Demo
    $ mvn compile exec:java -Dexec.mainClass=Demo
  6. Use JavaCV OpenCV and FFmpeg APIs

    master

    JavaCV provides wrapper classes that closely follow the original C/C++ header syntax. You can use imread, imwrite, and other OpenCV functions by importing the corresponding global classes.

    Example: Loading and smoothing an image using OpenCV wrappers:

    import org.bytedeco.opencv.opencv_core.*;
    import org.bytedeco.opencv.opencv_imgproc.*;
    import static org.bytedeco.opencv.global.opencv_core.*;
    import static org.bytedeco.opencv.global.opencv_imgproc.*;
    import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
    
    public class Smoother {
        public static void smooth(String filename) {
            Mat image = imread(filename);
            if (image != null) {
                GaussianBlur(image, image, new Size(3, 3), 0);
                imwrite(filename, image);
            }
        }
    }
    import org.bytedeco.opencv.opencv_core.*;
    import org.bytedeco.opencv.opencv_imgproc.*;
    import static org.bytedeco.opencv.global.opencv_core.*;
    import static org.bytedeco.opencv.global.opencv_imgproc.*;
    import static org.bytedeco.opencv.global.opencv_imgcodecs.*;
    
    public class Smoother {
        public static void smooth(String filename) {
            Mat image = imread(filename);
            if (image != null) {
                GaussianBlur(image, image, new Size(3, 3), 0);
                imwrite(filename, image);
            }
        }
    }
  7. Manual Installation of JavaCV JARs

    master

    To install JavaCV manually, add the following JAR files to your project's classpath:

    • javacv.jar
    • javacpp.jar
    • Specific child module JARs (e.g., opencv*.jar, ffmpeg*.jar, etc.)

    IDE Specific Instructions:

    NetBeans:

    1. Right-click the Libraries node in the Projects window.
    2. Select Add JAR/Folder... and select the files.

    Eclipse:

    1. Go to Project > Properties > Java Build Path > Libraries.
    2. Click Add External JARs... and select the files.

    Visual Studio Code:

    1. Navigate to Java Projects > Referenced Libraries.
    2. Click the + icon and select the files.

    IntelliJ IDEA (Android):

    1. Copy all JAR files into the app/libs subdirectory.
    2. Navigate to File > Project Structure > app > Dependencies.
    3. Click +, select 2 File dependency, and select the JARs from libs.
    4. In AndroidManifest.xml, add android:extractNativeLibs="true".
  8. Convert JavaCV Frames to JavaFX Images with JavaFXFrameConverter

    master

    Use JavaFXFrameConverter to transform org.bytedeco.javacv.Frame objects into javafx.scene.image.Image objects for use in JavaFX user interfaces.

    Note:

    • Conversion from Image back to Frame is currently not supported and will throw an UnsupportedOperationException.
    • The converter currently only supports frames with exactly 3 image channels (BGR format).