OpenCV 3.1.0断言在cv :: HOGDescriptor :: setSVMDetector中的checkDetectorSize失败

时间:2016-04-01 12:23:44

标签: java opencv opencv3.0

我正在尝试使用自己的图像训练SVM并将其提供给OpenCV中的HOGDescriptor。 训练似乎进展顺利,但是当我尝试使用setSVMDetector函数将模型提供给HOGDescriptor时,它失败了。 培训代码

    Mat trainingImages = new Mat();
    Mat trainingLables = new Mat();

    String personsPath = "C:\INRIAPerson\\INRIAPerson\\96X160H96\\Train\\pos";

    for (File file : new File(personsPath).listFiles()) {
        if(file.isFile()){
            Mat img = Imgcodecs.imread(file.getPath());
            Mat newImg = new Mat();
            Imgproc.cvtColor(img, newImg, Imgproc.COLOR_BGR2GRAY);
            newImg = newImg.reshape(1,1);

            trainingImages.push_back(newImg);
            trainingLables.push_back(Mat.ones(new Size(1,1), CvType.CV_32SC1));
        }
    }


    String nonPersonsPath = "C:\\NRIAPerson\\INRIAPerson\\train_64x128_H96\\neg";
    for (File file : new File(nonPersonsPath).listFiles()) {
        if(file.isFile()){
            Mat img = Imgcodecs.imread(file.getPath());
            Mat newImg = new Mat();
            Imgproc.cvtColor(img, newImg, Imgproc.COLOR_BGR2GRAY);

            Imgproc.resize(newImg, newImg, new Size(160 , 96));
            newImg = newImg.reshape(1,1);

            trainingImages.push_back(newImg);
            trainingLables.push_back(Mat.zeros(new Size(1,1), CvType.CV_32SC1));
        }
    }

    trainingImages.convertTo(trainingImages, CvType.CV_32FC1);

    SVM trainer = SVM.create();
    trainer.setKernel(SVM.LINEAR);
    trainer.train(trainingImages, Ml.ROW_SAMPLE, trainingLables);

    HOGDescriptor hog = new HOGDescriptor(new Size(160, 96), new Size(8,8), new Size(4,4), new Size(4,4), 9);
    Mat supportVector = trainer.getSupportVectors();
    hog.setSVMDetector(supportVector);

错误消息

libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
OpenCV Error: Assertion failed (checkDetectorSize()) in cv::HOGDescriptor::setSVMDetector, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\hog.cpp, line 117
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\objdetect\src\hog.cpp:117: error: (-215) checkDetectorSize() in function cv::HOGDescriptor::setSVMDetector
]
    at org.opencv.objdetect.HOGDescriptor.setSVMDetector_0(Native Method)
    at org.opencv.objdetect.HOGDescriptor.setSVMDetector(HOGDescriptor.java:302)
    at feature.extraction.App.main(App.java:86)

我不知道libpng警告是否与它有关。 如果有人有这个想法,这让我发疯了吗?

1 个答案:

答案 0 :(得分:1)

我明白了。我做错了几件事

  1. 我正在训练图像,而不是<package>
  2. 我忘了将features添加到supportvector
  3. 首先我创建了一个-rho。使用2d float array类中的compute函数使用图像的功能填充它。用阵列训练SVM。培训结束后,我使用HOGDescriptor函数获取getDecisionFunction并将其附加到rho