Language: Java
Computer Vision / ML
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.
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.
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.8</version>
</dependency>implementation 'org.bytedeco:javacv-platform:1.5.8'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.
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.
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.
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.
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.
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.
// Load DNN model using OpenCV DNN module for object detection or classificationJavaCV can interface with OpenCV’s DNN module to run pre-trained neural networks for computer vision tasks.
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.