Home > OpenCV > Javacv Example

Javacv Example

September 17, 2012 Leave a comment Go to 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: ,
  1. nt162
    October 1, 2012 at 6:55 PM

    Hello!

    I’ve seen several examples on how to do this type of procedure and they all use the former IplImage data type. Is it possible for you to show me how to do it with the Mat data type? I’ve been trying and searching and can’t figure out how to do it…

    Thanks a lot!

  2. January 1, 2013 at 6:31 AM

    hello,i am trying to make the opencv work on the hadoop,did u know how to install it for hadoop?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: