Monday, December 8, 2014

How to Use OpenCV with Java under Eclipse IDE

Introduction

The project in this, how to use OpenCV with Java under Eclipse IDE platform. This will help developers who love Java and OpenCV environment. The application is totally made in Eclipse INDIGO version environment. The application shows how to use the OpenCV with Eclipse IDE.
In this application, we see how to capture video from webcam. This application is totally a demonstration for how to create applications in Eclipse INDIGO. In this, I explain how to configure the Eclipse INDIGO IDE in order to execute OpenCV applications.

Eclipse

Eclipse is free and an Open source software. Eclipse is multi-language software development environment. Eclipse is IDE, i.e., Integrated Development Environment. Eclipse can be used to develop an application in java. Eclipse is mostly used for JAVA. It contains workspace & plug-ins in system for customizing the environment. By means of various plug-ins, other programming languages including Ada, C, C++, COBOL, Perl, PHP, Python, Ruby (including Ruby on Rails framework), Scala, and Scheme. The IDE is often called Eclipse ADT for Ada, Eclipse CDT for C/C++, Eclipse JDT for Java, and Eclipse PDT for PHP.

Eclipse: How To Start Work?

Video:


Start with Eclipse INDIGO IDE: Configuring
The following steps show how to configure the Eclipse INDIGO version with OpenCV 2.4.8 version. First of all, create the project. Then select the properties from Project Menu. Then follow the steps listed below.

Step 1: Add External JARs File

Select Java Build Path, Select Libraries, Click on "Add External JARs". find opencv-248.jar file in "C:\opencv\build\java", select file, click open.
Properties>>Select Java Build Path>> Select Libraries >> Click Add External JARs >> Select File "C:\opencv\build\java\opencv-248.jar" >> Click Ok.

Step 2: Add Native Library

Select "Native Library Location" then click on "Edit" Button. put Location path "C:\opencv\build\java\x86"
Click OK.
Select Native Library Location >> Edit >> Location path ="C:\opencv\build\java\x86" >> OK.

Step 3: Finish

Finally, click on OK button to add in project.

Package Explorer

Finally all details are seen in Package Explorer. Default Package contains the VideoCap.java file, JRE System Library containing the all JavaSE packages and Reference libraries containing the opencv-2.4.8 package.

Using the Code

The following sample code is for web camera video capture. Below, I will be explaining the program with details, that how the program is executing.

//
// sample program to run in eclipse.
//
import org.opencv.core.*;
import org.opencv.highgui.Highgui;        
import org.opencv.highgui.VideoCapture;        
        
public class VideoCap {
    public static void main (String args[]){
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     VideoCapture camera = new VideoCapture(0);
     
     if(!camera.isOpened()){
      System.out.println("Error");
     }
     else {
      Mat frame = new Mat();
         while(true){
          if (camera.read(frame)){
           System.out.println("Frame Obtained");
           System.out.println("Captured Frame Width " + 
           frame.width() + " Height " + frame.height());
           Highgui.imwrite("camera.jpg", frame);
           System.out.println("OK");
           break;
          }
         } 
     }
     camera.release();
    }
}   

First of all, import the respective package in application. Following are the basic packages for implementing video capture from webcam or video file.
  • CoreCore package includes having basic data structures, including multi dimensional array Mat and basic function used by modules.
  • Highgui: Easy to use interface to images, videocapture, video codec, and simple GUI capabilities.
  • VideoCaptureVideoCapture subpackage of HighguiVideoCapture is responsible for reading video from webcam or video file. It is also responsible for setting or getting video properties like frame height, frame width.
import org.opencv.core.*;
import org.opencv.highgui.Highgui;        
import org.opencv.highgui.VideoCapture;  

Secondly, we need to load the native library. This command must be executed once per Java process to using native OpenCV methods. If it does not get used, it throws an exception like "UnsatisfiedLink errors."

System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 

The following statement states that the camera variable is initialized with VideoCapture with 0 "Zero" parameter. It means that zero is first device ID. It accesses the first web camera. Video Camera is also initialized with video file, just pass video file path.

VideoCapture camera = new VideoCapture(0); 

OR

VideoCapture camera = new VideoCapture("F\\video.avi"); 

isOpned function is used to check if the binding of the class to a video source that means device index or video source file path was successful or not to use.

if(!camera.isOpened()){
      System.out.println("Error");
     } 

Mat class with two data parts, first the matrix header which contains information such as the size of the matrix and second a pointer to the matrix contains the pixel values. The matrix header size is constant.

Mat frame = new Mat();

Following is an infinite loop till the frame is not obtained. If frame has been obtained, it breaks the loop. The following loop prints the frame height and width, it also writes the frame to file and break the loop.

 while(true){
          if (camera.read(frame)){
           System.out.println("Frame Obtained");
           System.out.println("Captured Frame Width " + 
           frame.width() + " Height " + frame.height());
           Highgui.imwrite("camera.jpg", frame);
           System.out.println("OK");
           break;
          }
         }   

imwrite is a static method from the Highgui class. This method is used to write an image to a file. In the following frame variable is written to camera.jpg file.

Highgui.imwrite("camera.jpg", frame); 

For closing the video, we need to call its release function.

camera.release();

Point of Interest

  • Learn how to Open WebCamera
  • Learn how to Configure Eclipse

Reference

  • Java 2: The Complete Reference 5nd Edition - Herbert Schildt
  • The OpenCV Tutorials Release 2.4.8.0 PDF
  • http://www.opencv.org/

Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

Sunday, December 7, 2014

How to Use OpenCV with Java under NetBeans IDE

Introduction

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.

Video 

Video contains the Opencv 2.4.10 Configuration.


OpenCV

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();
String File_path="";

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 Runnable interface.
  • 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) ; 

Here, threadOb is an instance of a class that implements the Runnable interface and the name of the new thread is specified by threadName.
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:

void start(); 

The following run function grabs the video frame from webcam or video file. this block is synchronized.

public void run()
{
   synchronized(this)   {
       while(runnable)       {
          if(webSource.grab())       {
       try           {
    webSource.retrieve(frame);
    Highgui.imencode(".bmp", frame, mem);
    Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
          
     BufferedImage buff = (BufferedImage) im;
     Graphics g=jPanel1.getGraphics();

      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()");
        this.wait();
       }
 }
     catch(Exception ex)
             {
            System.out.println("Error");
             }
        }
     }
  }
}

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

References

  • Java 2: The Complete Reference 5nd Edition - Herbert Schildt
  • The OpenCV Tutorials Release 2.4.8.0 PDF
  • http://www.opencv.org/
  • 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.