【问题标题】:HOGDescriptor OpenCV dsize.area() assertion failedHOGDescriptor OpenCV dsize.area() 断言失败
【发布时间】:2014-10-21 19:26:11
【问题描述】:

我正在尝试训练 SVM 并在 OpenCV 的 HOGDescrpitor 中使用它。

HOGDescriptor 成功生成并加载了 xml 文件,但是当我尝试检测某个对象时,就会发生断言:

OpenCV 错误:断言失败 (dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0)) 调整大小,文件 /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/imgwarp.cpp,行 1825 抛出一个实例后调用终止 'tbb::captured_exception' 什么(): /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/imgwarp.cpp:1825: 错误:(-215) dsize.area() || (inv_scale_x > 0 && inv_scale_y > 0) 在 函数调整大小

为了实现 SVM 训练器,我使用了来自 using OpenCV and SVM with images 的提示

生成的 XML 文件大约有 144Kbytes。对于正样本和负样本,我使用大小为 64x128 的图像(正样本为 2000,负样本为 2000)

SVM 训练器参数:

CvSVMParams svmParams;
svmParams.svm_type = CvSVM::C_SVC;
svmParams.kernel_type = CvSVM::LINEAR;
svmParams.term_crit = cvTermCriteria( CV_TERMCRIT_ITER, 10000, 1e-6 );

检测代码:

int main()
{
    HOGDescriptor hog();
    if(!hog.load("/home/bin/hogdescriptor.xml"))
    {
        std::cout << "Failed to load file!" << std::endl;
        return -1;
    }

    VideoCapture cap(0);
    if(!cap.isOpened())
    {
        std::cout << "Error opening camera!" << std::endl;
        return 1;
    }

    Mat testImage;

    while ((cvWaitKey(30) & 255) != 27)
    {
        cap >> testImage;
        detectTest(hog, testImage);
        imshow("HOG custom detection", testImage);
    }

    return EXIT_SUCCESS;
}

void showDetections(const vector<Rect>& found, Mat& imageData) {
    for (const Rect& rect : found)
    {
        Point rectPoint1;
    rectPoint1.x = rect.x;
        rectPoint1.y = rect.y;

        Point rectPoint2;
        rectPoint2.x = rect.x + rect.width;
        rectPoint2.y = rect.y + rect.height;

        std::cout << "detection x: " << rect.x << ", y: " << rect.y << std::endl;

        rectangle(imageData, rectPoint1, rectPoint2, Scalar(0, 255, 0));
    }
}

void detectTest(const HOGDescriptor& hog, Mat& imageData)
{
    std::cout << "Trying to detect" << std::endl;

    vector<Rect> found;
    int groupThreshold = 2;
    Size padding(Size(32, 32));
    Size winStride(Size(8, 8));
    double hitThreshold = 0.; // tolerance
    hog.detectMultiScale(imageData, found, hitThreshold, winStride, padding, 1.05, groupThreshold);
//    hog.detectMultiScale(imageData, found);

    std::cout << "Trying to show detections" << std::endl;
    showDetections(found, imageData);
}

XML:

<?xml version="1.0"?>
<opencv_storage>
<my_svm type_id="opencv-ml-svm">
  <svm_type>C_SVC</svm_type>
  <kernel><type>LINEAR</type></kernel>
  <C>1.</C>
  <term_criteria><epsilon>2.2204460492503131e-16</epsilon>
    <iterations>10000</iterations></term_criteria>
  <var_all>8192</var_all>
  <var_count>8192</var_count>
  <class_count>2</class_count>
  <class_labels type_id="opencv-matrix">
    <rows>1</rows>
    <cols>2</cols>
    <dt>i</dt>
    <data>
      -1 1</data></class_labels>
  <sv_total>1</sv_total>
  <support_vectors>
    <_>
      -9.25376153e-05 -9.25376153e-05 -9.25376153e-05 -9.25376153e-05 ...and many, many...</_></support_vectors>
  <decision_functions>
    <_>
      <sv_count>1</sv_count>
      <rho>-1.</rho>
      <alpha>
        1.</alpha>
      <index>
        0</index></_></decision_functions></my_svm>
</opencv_storage>

有人可以向我解释这个断言,或者可以为这个问题提供一些解决方案吗?我花了将近 3 天的时间来解决这个问题,但没有任何成功...提前致谢!

【问题讨论】:

    标签: c++ opencv detection


    【解决方案1】:

    这是我得到的更接近...仍在尝试使用此 xml

    private static void buscar_hog_svm() {
    
        if (clasificador == null) {
            clasificador = new CvSVM();
            clasificador.load(path_vectores);
        }
    
        Mat img_gray = new Mat();
        //gray  
        Imgproc.cvtColor(imag, img_gray, Imgproc.COLOR_BGR2GRAY);
    
        //Extract HogFeature  
        hog = new HOGDescriptor(
                _winSize //new Size(32, 16)
                , _blockSize, _blockStride, _cellSize, _nbins);
        MatOfFloat descriptorsValues = new MatOfFloat();
        MatOfPoint locations = new MatOfPoint();
        hog.compute(img_gray,
                descriptorsValues,
                _winSize,
                _padding, locations);
    
    
        Mat fm = descriptorsValues;
    
        System.out.println("tamano fm: " + fm.size());
        //Classification whether data is positive or negative 
        float result = clasificador.predict(fm);
        System.out.println("resultado= " + result);
    }
    

    如果你有更多线索,请分享

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2014-02-10
      • 2012-11-26
      • 2014-11-24
      • 2014-05-28
      • 2020-12-31
      • 2015-09-12
      相关资源
      最近更新 更多