在javacv中将IplImage转换为Mat

我需要帮助将IplImage转换为Mat.我想为图像计算HOGDescriptor,然后用SVM对它进行分类,但是“计算”需要Mat类型.

您能举一些例子说明如何在Java中将IplImage转换成Mat吗?

解决方法:

不要混淆here文档中的official OpenCV Java binding和没有文档的JavaCV项目.

如果您使用的是JavaCV,则无需转换IplImage即可使用HOGDescriptor,如JavaCV source所示,HOGDescriptor对象包装器可处理CvArr对象:

// 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);
    ...
};

现在,如您在opencv_core.java中看到的那样,IplImage包装器对象扩展了CvArr:

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

因此,您不必进行任何转换.

这是使用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);
上一篇:使用AndroidFrameConverter将javacv框架转换为位图


下一篇:关于视频解码总结--javaCV