Segmentation fault when showing an mat?
I am bit confused on how, and why i get this segmentation fault, when try to show an image..
void filter(Mat src, Mat dst, int kernel, double P)
{
cout << "got it" << endl;
Mat temp = src.clone();
Mat dst111 = src.clone();
copyMakeBorder(temp,temp,kernel-1,kernel-1,kernel-1,kernel-1,BORDER_CONSTANT,Scalar(0,0,0));
for(int row = kernel/2; row < temp.rows - kernel/2-1; row++)
{
for(int col = kernel/2; col < temp.cols - kernel/2-1; col++)
{
double den=0,num=0;
for(int i = -(kernel/2); i <= (kernel/2) ; i++)
{
for(int j = -(kernel/2) ; j <= (kernel/2); j++)
{
den += pow(temp.at<double>(row+i,col+j),P);
num += pow(temp.at<double>(row+i,col+j),P+1);
//cout <<"Row: "<<row+i << " " << "col: "<< col + j << endl;
}
}
//cout << num/den << endl;
double value = num/den;
dst.at<double>(row,col) = value;
}
}
// dst.convertTo(dst111,CV_8U);
imshow("hey",dst);
cout << "done " << endl;
waitKey();
}
--Updated--
Mat src is instantiated as Mat src = imread(img, CV_LOAD_GRAYSCALE)
And mat dst is instantiated as Mat dst = src.clone();
dst.at<double>
- is your dst Mat double ? is it even allocated ?I've notices that some of the values value computes is nan. The division gives nan,
num
andden
isinf
.so, your types are all wrong. what you read is
uchar
notdouble
please use
convertTo()
, if you need another type or more precision.Even if i convert it it does not appear correctly..
I added this to alleviate the issue with inf and nan ..
But yeah.. it doesn't give me segmentation fault.. but the image isn't what i expected it to be.. stripes appear on the image..
this won't go anywhere, unless you get the type right.