Convert IplImage to Mat in javacv

泪湿孤枕 提交于 2019-12-23 13:33:45

问题


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 ?


回答1:


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);



回答2:


Convert IplImage to Mat is simple.

IplImage iplImage= cvLoadImage("image.png");

Mat matImage = new Mat(iplImage);

vice versa




回答3:


There's a method for that:

opencv_core.cvarrToMat(iplImage);


来源:https://stackoverflow.com/questions/16584528/convert-iplimage-to-mat-in-javacv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!