The project in this tip is how to capture video from webcam and video file (*.AVI). This project is made in OpenCV and NetBeans IDE platform.
This tip will help developers who loves the Java and OpenCV environment. The application is totally made in netbeans 6.9.1 version environment. The application shows the how to use the OpenCV with netbeans IDE. In this application multithreading concept is used, to capture video from file or web camera. This application is totally a demonstration for how to create applications in netbeans. In this tip, I explain how to configure the netbeans IDE in order to execute OpenCV appliactions.
OpenCV 2.4.8 used for Java application, OpenCV supports Java from OpenCV 2.4.4 version. OpenCV 2.4.4 and OpenCV 2.4.6 version does not support VideoCapture (filename) function for read from file. It does not read the video file. In this project, the application reads the *.AVI file format video.
It stands for Open Source Computer Vision, it was designed especially for computational efficiency with strong focus on real time applications. It is written in optimized C/C++, and can take advantage of multi-core processing. In Image processing, it has been a great boon for the developers.
OpenCV is mostly a high-level library implementing algorithms for calibration techniques (Camera Calibration), feature detection (Feature) and tracking (Optical Flow), shape analysis (Geometry, Contour Processing), motion analysis (Motion Templates, Estimators), 3D reconstruction (View Morphing), object segmentation and recognition (Histogram, Embedded Hidden Markov Models, Eigen Objects). The essential feature of the library along with functionality and quality is performance.
OpenCV Basics: How To Start Work?
Start with Netbeans IDE: Configuring
The following steps show how to configure the netbeans 6.9.1 version with OpenCV 2.4.8 version. First of all, create the project. Then select the library shown in projects window. Then follow the following steps.
Step 1: Add Library
Select Libraries from netbeans project, right Click on Libraries, click "Add Library". Select Libraries> right click Libraries>Click Add Library.
Step 2: Create Library
Click on "Create" button to create new library. Enter the Library Name, as "OpenCV2.4.8". Click on "OK" Button.
Step 3: Customize Library
Select Classpath tab to add class path. Click on "Add JAR/Folder" to add class path. Select file path "c:\opencv\build\java\opencv-248.jar". Click on "OK" button.
Step 4: Add Library
Select Configured library as,"opencv2.4.8". Click on "Add Library" button to add library to project work space.
Step 5: Project Properties
Select project, right click on "Properties". Select "Run" option, Change VM options to add native library "
-Djava.library.path="C:\opencv\build\java\x86"". If you have a 32-bit system, you need to select the x86 folder instead of x64.
Using the Code
First, declare all variables and important objects to use:
private DaemonThread myThread = null;
int count = 0;
VideoCapture webSource = null;
Mat frame = new Mat();
MatOfByte mem = new MatOfByte();
Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. A multithreading is a specialized form of multitasking. Multithreading requires less overhead than multitasking processing.
Multithreaded function is used for displaying video frame by frame. On clicking "Start" button, this function starts execution concurrently. On clicking "Stop" button, this function stops execution.
Creating a Thread
Java defines two ways in which this can be accomplished:
- You can implement the
- You can extend the
Thread class itself.
Create Thread by Implementing Runnable
The easiest way to create a thread is to create a class that implements the
Runnable interface. To implement
Runnable, a class needs to only implement a single method called
run(), which is declared like this:
public void run()
You will define the code that constitutes the new thread inside
run() method. It is important to understand that
run() can call other methods, use other classes, and declare variables, just like the main thread can.
After you create a class that implements
Runnable, you will instantiate an object of type
Thread from within that class.
Thread defines several constructors. The one that we will use is shown here:
Thread(Runnable threadOb, String threadName) ;
threadOb is an instance of a class that implements the
Runnable interface and the name of the new thread is specified by
After the new thread is created, it will not start running until you call its
start( ) method, which is declared within Thread. The
start( ) method is shown here:
run function grabs the video frame from webcam or video file. this block is synchronized.
public void run()
Highgui.imencode(".bmp", frame, mem);
Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
BufferedImage buff = (BufferedImage) im;
if (g.drawImage(buff, 0, 0, getWidth(), getHeight() -150 , 0, 0, buff.getWidth(), buff.getHeight(), null))
if(runnable == false)
System.out.println("Going to wait()");
Points of Interest
- Learn how to open video file
- Learn how to open web camera
- Learn how to configure the netbeans 6.9.1 IDE version
- Learn how to use Multithreading
- Java 2: The Complete Reference 5nd Edition - Herbert Schildt
- The OpenCV Tutorials Release 220.127.116.11 PDF
- Head First Java 2nd Edition – Kathy Sierra and Bert Bates
Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.
- 29 Jan 20114- First release