我需要帮助将我的转换IplImage
为Mat
. 我想计算HOGDescriptor
我的图像,然后用 SVM 对其进行分类,但“计算”需要Mat
类型。
你能举一些如何在java中IplImage
转换的例子吗?Mat
将 IplImage 转换为 Mat 很简单。
IplImage iplImage= cvLoadImage("image.png");
Mat matImage = new Mat(iplImage);
反之亦然
不要混淆此处记录的官方 OpenCV Java 绑定和没有文档的JavaCV项目。
如果您正在使用JavaCV
,则无需转换IplImage
即可使用HOGDescriptor
,正如您在JavaCV
源代码中看到的那样,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);
有一个方法:
opencv_core.cvarrToMat(iplImage);