user2389417 user2389417 - 6 months ago 79
Java Question

Convert IplImage to Mat in javacv

I need help to convert my

IplImage
into
Mat
. I want to compute
HOGDescriptor
for my image and then classify it with SVM, but "compute" requires
Mat
type.

Can you give some example of how to convert
IplImage
into
Mat
in java ?

Answer

Don't confuse the official OpenCV Java binding which is documented here and the JavaCV project which has no documentation.

If you're using JavaCV, you don't need to convert your IplImage in order to use the HOGDescriptor, as you can see in the JavaCV source, the HOGDescriptor object wrapper manipulates CvArr objects :

// javacv/cpp/opencv_objdetect.java:527
public static class HOGDescriptor extends Pointer {
    public HOGDescriptor();
    ...
    public native void setSVMDetector(CvArr _svmdetector);
    ...
    public native void compute(CvArr img, FloatPointer descriptors, CvSize winStride, CvSize padding, CvPoint locations);
    public native void detect(CvArr img, CvPoint foundLocations, DoublePointer weights, double hitThreshold, CvSize winStride, CvSize padding, CvPoint searchLocations);
    public native void detect(CvArr img, CvPoint foundLocations, double hitThreshold, CvSize winStride, CvSize padding, CvPoint searchLocations);
    public native void detectMultiScale(CvArr img, CvRect foundLocations, double hitThreshold, CvSize winStride, CvSize padding, double scale, int groupThreshold);
    public native void detectMultiScale(CvArr img, CvRect foundLocations, DoublePointer foundWeights, double hitThreshold, CvSize winStride, CvSize padding, double scale, double finalThreshold, boolean useMeanshiftGrouping);
    public native void detectMultiScale(CvArr img, CvRect foundLocations, double hitThreshold, CvSize winStride, CvSize padding, double scale, double finalThreshold, boolean useMeanshiftGrouping);
    ...
};

Now, as you can see in opencv_core.java, the IplImage wrapper object extends CvArr :

// javacv/cpp/opencv_core.java:410
public static class IplImage extends CvArr {
    ...
};

So you shouldn't have to do any conversion.

Here is an example using HOGDescriptor.detectMultiScale:

IplImage img = cvLoadImage("image.jpg");
CvRect foundRects = new CvRect(null);
HOGDescriptor hog = new HOGDescriptor(); 
FloatPointer svm = HOGDescriptor.getDefaultPeopleDetector();
hog.setSVMDetector(svm);
hog.detectMultiScale(img, foundRects, 0, cvSize(8,8), cvSize(32,32), 1.05, 2);
Comments