【问题标题】:Runtime error SIGSEV in opencv2 HOG codeopencv2 HOG 代码中的运行时错误 SIGSEGV
【发布时间】:2012-10-29 16:08:02
【问题描述】:

我正在处理这段代码,摘自http://smsoftdev-solutions.blogspot.com/2009/08/integral-histogram-for-fast-calculation.html

目前将该页面上的代码导入到 opencv2 c++ 实现中,这就是我目前所拥有的。

主要


using namespace cv;
using namespace std;

//CONSTANTES
#define PI 3.142

//FUNCIONES
vector<Mat> extractIntegralHist(Mat in, const int _nbins);

int main(int argc, char *argv[])
{
//Definicion de variables
    Mat frame;
    vector<Mat> integrals(9);
    namedWindow("main_video",WINDOW_AUTOSIZE);
    namedWindow("temporal_window",WINDOW_AUTOSIZE);

//Inicializa la captura del video
    VideoCapture cap(0);
       if(!cap.isOpened()){
           return -1;
           cout << "No se puede capturar";
       }

//Loop principal
    while(1){
       cap >> frame;
       imshow("main_video",frame);
       waitKey(1);

       extractIntegralHist(frame, 9);

    }
    integrals.clear();
}

功能


//CALCULAR HISTOGRAMAS DE INTEGRALES
vector<Mat> extractIntegralHist(Mat in, const int _nbins){
    Mat in_gray;
    int x,y;
    float temp_gradient,temp_magnitude;
    namedWindow("temporal_window",WINDOW_AUTOSIZE);
//Convierte la imagen a escala de grises y normaliza
    cvtColor(in,in_gray,CV_RGB2GRAY);
    equalizeHist(in_gray,in_gray);
//Calcula la derivada de la imagen en gris en direccion x,y utilizando sobel
    Mat xsobel, ysobel;
    Sobel(in_gray, xsobel, CV_32FC1, 1, 0, 1);
    Sobel(in_gray, ysobel, CV_32FC1, 0, 1, 1);
    in_gray.release();
//Crea la matriz de 9 imagenes contenedoras -- REVISAR ESTE CODIGO
    vector<Mat> bins(_nbins);
    for (int i = 0; i < _nbins ; i++) {
        bins[9] = Mat::zeros(in.size(),CV_32F);
    }
//Crea la matriz donde se guardaran las integrales de la imagen
    vector<Mat> integrals(_nbins);
    for (int i = 0; i < _nbins ; i++) {
        integrals[9] = Mat(Size(in_gray.cols+1,in_gray.rows+1),CV_64F);
    }
//Calcula los contenedores
    for(y=0;y<in.rows;y++){

        float* xSobelPtr = (float*) (xsobel.row(y).data);
        float* ySobelPtr = (float*) (ysobel.row(y).data);

        float** binsRowPtrs = new float *[_nbins];
        for (int i = 0; i < _nbins; i++){
            binsRowPtrs[i] = (float*) (bins[i].row(y).data);
        }
    //Para cada pixel en la fila los valores de magnitud y orientacion son calculados
        for(x=0;x<in.cols;x++){
            if(xSobelPtr==0){
                temp_gradient = ((atan(ySobelPtr[x]/(xSobelPtr[x]+0.00001)))*(180/PI)+90);
            }
            else{
                temp_gradient = ((atan(ySobelPtr[x]/xSobelPtr[x]))+(180/PI)+90);
            }
            temp_magnitude = sqrt((xSobelPtr[x] * xSobelPtr[x]) + (ySobelPtr[x] *       ySobelPtr[x]));
            float binStep = 180/ /*_nbins*/9;
                for (int i=1 ; i<=/*_nbins*/9 ; i++){
                    if (temp_gradient <= binStep*i){
                    binsRowPtrs[i-1][x] = temp_magnitude;
                    break;
                    }
                }
        }
     }

     for (int i = 0; i<_nbins;i++){
        integral(bins[i],integrals[i]);
     }

     for (int i = 0; i <_nbins ; i++){
        bins[i].release();
     }

    return integrals;
}

程序可以编译,但是当我运行它时,它就停止了,调试器向我显示 SIGSEV 错误并显示第 61 行和第 38 行。

38 - extractIntegralHist(frame, 9);

61 - bins[9] = Mat::zeros(in.size(),CV_32F);

我无法查明问题所在,因为我觉得一切都很好,你能看一下代码吗?

编辑。更新。当函数 extractIntegralHist 运行时,程序似乎停止了,据称在(函数的)第 61 行。现在,我知道这是一个内存处理异常,但我找不到它有什么问题,而且我不明白调试器告诉我什么。

谢谢。

【问题讨论】:

    标签: opencv gradient runtime-error histogram integral


    【解决方案1】:

    线

    bins[9] = Mat::zeros(in.size(),CV_32F);
    

    正在尝试访问bins 的第十个(注意零索引)元素。由于_nbins9 的值传入,这超出了向量的范围。此外,如果您使用.at() 而不是使用[] 访问元素,则此类错误将引发out_of_range 异常,这对诊断问题应该更有帮助。

    要将向量的每个元素分配给包含零的cv::Mat,请考虑将循环更改为

    for (int i = 0; i < _nbins ; i++) {
        bins[i] = Mat::zeros(in.size(),CV_32F);
    }
    

    或者更简洁地说,删除循环并将您的 bins 初始化替换为

    vector<Mat> bins(_nbins, Mat::zeros(in.size(),CV_32F));
    

    请注意,您在初始化 integrals 时遇到了同样的问题。

    【讨论】:

    • 谢谢,我已经解决了这个问题,因为这是旧的,但现在我有另一个问题,这个代码是一样的。我需要它更快地执行,因为到目前为止,当我将此代码应用到 QT GUI 应用程序时,我得到了 16FPS,我每毫秒将它作为一个线程调用。有什么办法可以提高这段代码的性能吗?请检查不良编程或资源浪费。
    • 这真的应该是一个单独的问题。在这个网站上,接受正确的答案是个好主意,既要感谢回答的人,又要向其他人表明已经找到了解决方案。您甚至可以发布自己问题的答案(例如本例)。
    • 是的,很抱歉,我总是接受它从我身边溜走的答案。我会发一个新帖子,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    相关资源
    最近更新 更多