我需要帮助将我的IplImage
转换为Mat
。 我想对图像进行HOGDescriptor
计算,然后用SVM对其进行分类,但“compute”需要Mat
类型。
你能给出Java中将IplImage
转换为Mat
的示例吗?
IplImage iplImage= cvLoadImage("image.png");
Mat matImage = new Mat(iplImage);
如果您使用的是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);