Tuesday 25 February 2014

Accessing the elements of IplImage

The following code demonstrates the way of accessing the elements of an IplImage. It accesses one by one the elements of the IplImage, increases them by 1 and prints them to screen.

NOTE:  Every time you call put or get, that's a java member function call, which in turn calls a C wrapper function, which again calls a C opencv library function. Therefore, in the general case you should avoid accessing elements one by one unless necessary, as it can make the program considerably slower! 

    // Load IplImage (ImgName is a variable of type String)
    IplImage image = cvLoadImage(ImgName);

    // Define which of the 3 channels of a color image 
    // you want to access (for grayscale images, the 
    // channel is always 0). RGB images are stored as 
    // BGR in JavaCV so: 0-> Blue, 1-> Green, 2-> Red
    int channel = 0; // Blue channel  

    ByteBuffer ImageBuffer = image.getByteBuffer();

    for(int y = 0; y <image.height(); ++y) { 
        for(int x = 0; x <image.width(); ++x) {

            int Index =  y * image.widthStep() + x* image.nChannels() + channel;
            
            // Read the pixel value - the 0xFF is needed to cast 
            // from an unsigned byte to an int.
            int value = ImageBuffer.get(Index) & 0xFF;

            // Put the increased by 1 value in IplImage
            ImageBuffer.put(Index, (byte) (value+1));

            System.out.print( " " + (ImageBuffer.get(Index) & 0xFF));
        }
        System.out.println("\n");
    }

Wednesday 12 February 2014

How to Import JavaCV libraries to Android Project

If you haven't yet installed the Android Development Environment, you can follow this link which contains everything you need to get started quickly. Watch the tutorial video and follow the instructions underneath to install Eclipse, the Integrated Development Environment (IDE) for Android development and create a new project.

Once you have installed everything and have created the project, use the following procedure to import JavaCV libraries to the Android project:
  1. Download the JavaCv libraries:
    a. From this link download "javacv-0.7-bin.zip" and extract the files:
    - javacpp.jar
    - javacv.jar
    - javacv-android-arm.jar
    b. From this link download "javacv-0.7-cppjars.zip" and extract the files:
    - opencv-2.4.8-android-arm.jar
    -  ffmpeg-2.1.1-android-arm.jar

  2. Go to File > New > Folder, select your project as parent folder, type "libs/armeabi" as Folder name, and click Finish.

  3. Copy javacpp.jar and javacv.jar into the "libs" folder.

  4. Extract all the *.so files from javacv-android-arm.jar, opencv-2.4.8-android-arm.jar, and ffmpeg-2.1.1-android-arm.jar directly into the "libs/armeabi" folder, without creating any of the subdirectories found in the JAR files. (The easiest way to extract the .so files from the .jar is by using 7-ZIP software which can be downloaded here).

  5. Navigate to Project > Properties > Java Build Path > Libraries and click "Add JARs...".

  6. Select both javacpp.jar and javacv.jar from the "libs" folder.

Finally,import JavaCV functions to your code
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
and use some sample code to check that everything is working correctly.