0
votes

I had a similar question yesterday and was able to reason out the error, thanks to your help. Well, today I have hit a very similar problem, but, with my level best, am still not able to locate the cause of segmentation fault in this specific code.

    int main()
    {
        Mat src,dst,src_gray;
        int kernel_size = 3;
        int scale = 1;
        int delta = 0;
        int ddepth = CV_16S;
        char* window_name = "sharpness estimate";
        string dir = string("erez images");
        vector<string> files = vector<string>();
        getdir(dir,files);
        cout<<files.size()<<endl;
        int c;
        double *estimate=0,*min=0;
        Point *minLoc=0,*maxLoc=0;
        string parent = "/home/siddarth/examplescv/erez images/";
        for(int i=1; i<=files.size()-2;i++)
        {
            cout<<files.data()[i]<<endl<<i<<endl;
            string path = parent+files.data()[i];
            cout<<path<<endl;
            src = imread(path);

            if( !src.data )
            {
                    return -1;
            }

            cout<<"check1"<<endl;
            cvtColor(src,src_gray,CV_RGB2GRAY);

            Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
            //convertScaleAbs(dst,abs_dst);
            cout<<"check2"<<endl;
            minMaxLoc(dst,min,estimate,minLoc,maxLoc,noArray());
            cout<<"estimate :"<<*estimate<<endl;

        }
        waitKey(100000000000);
        return 0;
    }

Am able to proceed till check2 during run time. My guess is the segmentation fault is arising due to minMaxLoc. Please help me to solve this issue. I would also be really glad if you can tell me how to tackle segmentation fault in future and why segmentation fault occurs. What it actually means?

Note: The getDir function is written by myself, and not inbuilt. it just gives me list of directories and files in the given directory.

Am executing opencv in linux Ubuntu 11.10 and OpenCv2.4.3

Output of the debugger :

"Program received signal SIGSEGV, Segmentation fault. 0x001b7c5c in cv::minMaxIdx(cv::_InputArray const&, double*, double*, int*, int*, cv::_InputArray const&) () from /usr/local/lib/libopencv_core.so.2.4"

2
Why you didn't try to use debugger? - Denis Ermolin
I used the gdb to debug my code. Please check the edit for the output of the debugger. As I guessed, it is something with the minMaxLoc function. - Lakshmi Narayanan

2 Answers

3
votes

Looking at your code, without debugging it, I'm guessing the problem is dst, which has not been initialized.

You have to allocate memory for the matrix before trying to use it.

Check this link to the OpenCv documentation, it has a very good example on how to use minMaxLoc(). Pay special attention to the item nº7 there.

But, as stated in the comments, learning to use the debugger, to inspect all variables at runtime, to check if there's something NULL that should have a value, is a good thing to learn. The debugger is a programmer's best friend ;)

Another problem I saw there is estimation, which is NULL, and you try to dereference it just after the call to minMaxLoc. From what I remember from this function, it does not change the value of estimation, but I can be wrong.

0
votes

The error was in the usage of minMaxLoc, as pointer out by #Castilho. The following changes helped me to resolve the segmentation fault.

    double estimation,min;
    Point minLoc,maxLoc;
    .
    .
    .
    .
    minMaxLoc(dst,&min,&estimation,&minLoc,&maxLoc,noArray());
    cout<<"estimate :"<<estimate<<endl;

Thank you for your help :).