I have the following code compiled in linux terminal (c++ in linux) and am using OpenCv 2.4.3.
However, am getting a segmentation fault in run time and I really have no clue as to why. I have placed differnt cout statements to know if the program processed to the particular stage but in vain. Could you please help me? Please explain me what exactly is this segmentation fault. Am stuck here for a long time.
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main()
{
cout<<"check"<<flush;
Mat src,src_gray,dst;
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
char* window_name = "sharpness estimate";
int freq,rows,cols =0;
double *estimate,*min = 0;
Point *minLoc,*maxLoc = 0;
src = imread("/home/siddarth/examplescv/erez images/image53.jpg");
if( !src.data )
{
return -1;
}
namedWindow(window_name,CV_WINDOW_AUTOSIZE);
Mat abs_dst;
cvtColor(src,src_gray,CV_RGB2GRAY);
Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
convertScaleAbs(dst, abs_dst);
minMaxLoc(dst,min,estimate,minLoc,maxLoc,noArray());
Size s = dst.size();
rows = s.height;
cols = s.width;
cout<<rows<<endl<<cols<<endl;
for(int i=0;i<=rows;i++)
{
for(int j=0;j<=cols;j++)
{
if(dst.at<double>(i,j) >= *estimate-100
&& dst.at<double>(i,j) <= *estimate+100)
{
cout<<freq++;
}
}
}
cout<<"estimate :"<<*estimate<<endl;
cout<<"frequency :"<<freq<<endl;
imshow(window_name,abs_dst);
waitKey(1000);
return 0;
}
The code doesn’t cross the first “check” print statement just after the main function declaration. That is the confusing issue. But once I flushed the first print statement, it got executed. I am still facing issues.
Make sure you insert
std::endlintocoutso that the buffer is flushed. This will probably be why you’re not seeing any output.One immediate issue is that your
forloops check the condition with<=, meaning that you’re probably going one past the end. But since you’re usingat, you should have an exception thrown (assuming thisMattype acts like a standard container).Also, you’re creating lots of pointers to pass as some function arguments (for example,
double* estimate). This doesn’t actually give you adoubleobject though, just a pointer. Unless the function you’re passing them to is allocating adoublefor you (which I hope it’s not), you’re doing it wrong. You should be doing:You’ll need to do that with all of the values you’re getting through output parameters.
Another thing to note: Doing
int i, j = 0;only initialisesjto0, but noti. You need to doint i = 0, j = 0;.Okay, I’m going to explain why fixing the initialisers works. I had to look up the definition of minMaxLoc to see what happens. Basically, the function is something like the following:
This function will take a pointer to an
int, and then set thatintto the value5. However, if the pointer passed is a null pointer, the value will not be set (otherwise there’ll be undefined behaviour because you’re derefencing a null pointer). Basically, passing a null pointer says “I don’t care about this value so don’t give it to me”.Now when you were initialising your pointers, you were doing:
This only sets
minto the null pointer. Sinceestimateis left uninitialized, you can’t rely on its value being null. You need to provide an initialiser for each declarator: