Archive

Posts Tagged ‘Javac’

Javacv Example

September 17, 2012 2 comments

First try out java opencv, which is java wrapper for opencv. The first thing you should do is to follow the instruction install opencv. You will need to know where are your lib files locate. In this example, I have them all in ‘opencv24lib’.  Here’s what it contains:

Download javacv zip files, unzip it, you will get several jar files(wrappers) which needed for your project. Below is a link to a good warming up example of javacv:

//static imports
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
//non-static imports
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorDetect {
    //color range of red like color
    static CvScalar min = cvScalar(0, 0, 130, 0);//BGR-A
    static CvScalar max= cvScalar(140, 110, 255, 0);//BGR-A    public static void main(String[] args) {
        //read image
        IplImage orgImg = cvLoadImage("colordetectimage.jpg");
        //create binary image of original size
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);
        //apply thresholding
        cvInRangeS(orgImg, min, max, imgThreshold);
        //smooth filter- median
        cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);
        //save
        cvSaveImage("threshold.jpg", imgThreshold);
    }
}

To compile and run the code (inside the folder of your code):

mkdir classes
javac -classpath .:/yourpathto/javacpp.jar:/yourpathto/javacv.jar:/yourpathto/javacv-linux-x86_64.jar:/yourpathto/javacv-linux-x86.jar *.java -d ./classes
jar cf JOpencvTest.jar -C classes .
java -Djava.library.path=/yourpathto/opencv24lib -cp .:/yourpathto/javacpp.jar:/yourpathto/javacv.jar:/yourpathto/javacv-linux-x86_64.jar:/yourpathto/javacv-linux-x86.jar:JOpencvTest.jar ColorDetect

Input Image:

output Image:

 

Here’s a link how to configure it on Win7, I didn’t give it a try since my windows machine installed of opencv2.3 not 2.4 version.

 

Advertisements
Categories: OpenCV Tags: ,