Skip to content

JavaCV / OpenCV Java

AI & Machine LearningComputer Vision / MLJava

What it is

JavaCV provides Java bindings for OpenCV and other computer vision libraries, allowing developers to process images and videos, detect objects, and apply advanced computer vision algorithms in Java applications.

JavaCV allows capturing video from cameras, reading/writing image files, performing image transformations, object detection using Haar cascades, and interfacing with machine learning models. It leverages OpenCV's high-performance image processing capabilities from Java.

Installation

<dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.8</version> </dependency>

Getting started

The smallest useful thing you can do with it, and what each part means.

Reading and displaying an image
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.global.opencv_imgcodecs;
import org.bytedeco.opencv.global.opencv_highgui;

Mat image = opencv_imgcodecs.imread("image.jpg");
opencv_highgui.imshow("Display", image);
opencv_highgui.waitKey(0);
Reads an image file into a Mat object and displays it in a window.
Capturing video from webcam
import org.bytedeco.opencv.opencv_videoio.VideoCapture;
import org.bytedeco.opencv.opencv_core.Mat;

VideoCapture cap = new VideoCapture(0);
Mat frame = new Mat();
while(cap.read(frame)) {
    // Process frame
}
cap.release();
Captures live video from the default webcam and allows processing of each frame.

Advanced usage

Where the library earns its place over a simpler alternative.

Grayscale conversion and edge detection
import static org.bytedeco.opencv.global.opencv_imgproc.*;
Mat gray = new Mat();
cvtColor(image, gray, COLOR_BGR2GRAY);
Mat edges = new Mat();
Canny(gray, edges, 100, 200);
Converts an image to grayscale and applies the Canny edge detection algorithm.
Face detection with Haar cascades
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_default.xml");
RectVector faces = new RectVector();
faceDetector.detectMultiScale(gray, faces);
Detects faces in an image using a pre-trained Haar cascade classifier.
Video recording
import org.bytedeco.opencv.opencv_videoio.VideoWriter;
VideoWriter writer = new VideoWriter("output.avi", VideoWriter.fourcc('M','J','P','G'), 30, new Size(640,480));
writer.write(frame);
Writes frames to a video file in real-time.
Integration with deep learning models
// Load DNN model using OpenCV DNN module for object detection or classification
JavaCV can interface with OpenCV’s DNN module to run pre-trained neural networks for computer vision tasks.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

org.bytedeco.javacv.FrameGrabber.Exception
Occurs if camera or video input cannot be accessed. Check device availability.
NullPointerException
Occurs if image or frame is empty. Ensure file paths are correct and capture works.
opencv_core.CvException
Thrown by OpenCV native methods. Check input Mat types, sizes, and parameters.

Best practices

  • Release Mat objects and video capture resources to avoid memory leaks.
  • Use smaller image resolutions for real-time processing to maintain performance.
  • Leverage native OpenCV methods for heavy image processing tasks.
  • Combine with JavaFX or Swing for GUI integration.
  • Keep pre-trained classifiers and models organized for reusability.

Background

Why it exists, and what it was reacting to.

OpenCV is a widely used open-source computer vision library originally written in C/C++. JavaCV provides wrappers to use OpenCV functionality from Java. It allows real-time image processing, video capture, object detection, and integration with deep learning frameworks. JavaCV is used in robotics, surveillance, augmented reality, and multimedia applications.