How to connect OpenCV and JavaCV properly (version compatibility)

2,474 views
Skip to first unread message

Michael

unread,
May 17, 2015, 6:34:41 AM5/17/15
to jav...@googlegroups.com
Hello people. Please clarify some simple moments.

I can't properly run JavaCV. For example i have Opencv 2.4.11 and JavaCV 0.9. I have succsessfully connected libraries and trying to compile this code:

package java_cv_ColoredObjTracking;

//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 Main_ColoredObjTracking {
    //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("D:/SOFT/eclipse_keplerClassic/workspace/java_cv_ImageDifference/imageDifference1.png");
        //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("result.jpg", imgThreshold);
    }
}


There are an errors occured:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_core in java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:711)
    at com.googlecode.javacpp.Loader.load(Loader.java:586)

etc...

Ok, ive found on the web that OpenCV version should be compaliable with JavaCV. JavaCV 0.9 = OpenCV 2.4.8. But thereis no vers2.4.8 in the internet.

Next. At the official site of JavaCV i have download https://github.com/bytedeco/javacv JavaCV 0.11 and connect to my project. And now i should uptade imports, there is only 2 now:
import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.IplImage;

but  there is a lot of errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method cvLoadImage(String) is undefined for the type Main_ColoredObjTracking
    The method cvGetSize(opencv_core.IplImage) is undefined for the type Main_ColoredObjTracking
    The method cvInRangeS(opencv_core.IplImage, opencv_core.CvScalar, opencv_core.CvScalar, opencv_core.IplImage) is undefined for the type Main_ColoredObjTracking
    CV_MEDIAN cannot be resolved to a variable

    The method cvSaveImage(String, opencv_core.IplImage) is undefined for the type Main_ColoredObjTracking


P.S. Pure Java running Ok, for example:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;

    // *** laserRanger - find a dot ***
    // Load img with laser dot and create another imgs
    Mat imageDot = Highgui.imread("D:/SOFT/eclipse_keplerClassic/workspace/java_cv_ImageDifference/road.jpg");
    Mat hsv_image = new Mat();
    Mat thresholded = new Mat();
    Mat thresholded2 = new Mat();
  
    //Draw rechtangle above the contors
    //http://answers.opencv.org/question/12056/findcontours-in-java-not-giving-desired-results/
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();   
    Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    //Imgproc.drawContours(imageBlurr, contours, 1, new Scalar(0,0,255));
    for(int i=0; i< contours.size();i++){
        System.out.println("Imgproc.contourArea: " + Imgproc.contourArea(contours.get(i)));
        if (Imgproc.contourArea(contours.get(i)) > 10 ){
            Rect rect = Imgproc.boundingRect(contours.get(i));
            //System.out.println(rect.height);
            if (rect.height > 10){
            System.out.println("(rect.XY, rect.HW: " + rect.x +","+rect.y+","+rect.height+","+rect.width);
            Core.rectangle(imageDot, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
            }
        }
    }

Can anyone told me what is the solution?)

Thereis a lot of examples in JavaCV, there are much more then pure Java and OpenCV. Thank you people.

Samuel Audet

unread,
May 17, 2015, 6:50:06 AM5/17/15
to jav...@googlegroups.com
On 05/17/2015 07:28 PM, Michael wrote:
> Next. At the official site of JavaCV i have download
> https://github.com/bytedeco/javacv JavaCV 0.11 and connect to my
> project. And now i should uptade imports, there is only 2 now:
> import org.bytedeco.javacpp.opencv_core.CvScalar;
> import org.bytedeco.javacpp.opencv_core.IplImage;

What about the following 3 imports?

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_highgui.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;

Samuel

Michael

unread,
May 17, 2015, 7:51:55 AM5/17/15
to jav...@googlegroups.com
IT WORKS! One funciton cant detect:
cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);

I have slashed this string it and IT WORKS))) Can I somehow teach eclipse to organize good automatically import?
God bless You man!


неділя, 17 травня 2015 р. 13:50:06 UTC+3 користувач Samuel Audet написав:

Samuel Audet

unread,
May 17, 2015, 7:56:22 AM5/17/15
to jav...@googlegroups.com
On 05/17/2015 08:51 PM, Michael wrote:
> T WORKS! One funciton cant detect:
> cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);

The signature of that changed a bit. This should work:
cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13, 0, 0, 0);

> I have slashed this string it and IT WORKS))) Can I somehow teach
> eclipse to organize good automatically import?
> God bless You man!

I don't know, you'll have to ask the Eclipse team about that! It works
fine in NetBeans...

Samuel
Reply all
Reply to author
Forward
0 new messages