我需要帮助才能将IplImage
转换为Mat
。我想为我的图片计算HOGDescriptor
,然后使用SVM对其进行分类,但“compute”需要Mat
类型。
您能举例说明如何在java中将IplImage
转换为Mat
吗?
答案 0 :(得分:2)
请勿混淆记录为official OpenCV Java binding的here和没有文档的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);
答案 1 :(得分:2)
将IplImage转换为Mat非常简单。
IplImage iplImage= cvLoadImage("image.png");
Mat matImage = new Mat(iplImage);
反之亦然
答案 2 :(得分:0)
有一种方法:
opencv_core.cvarrToMat(iplImage);