Segmentation fault when showing an mat?

asked 2015-11-07 04:18:40 -0600

215 gravatar image

updated 2015-11-07 04:56:05 -0600

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();

edit retag flag offensive close merge delete

Comments

1
  • dst.at<double> - is your dst Mat double ? is it even allocated ?
  • please run a debug build, this will throw a lot of assertions suppressed in release.
berak gravatar imageberak ( 2015-11-07 04:45:27 -0600 )edit

I've notices that some of the values value computes is nan. The division gives nan, num and den is inf.

215 gravatar image215 ( 2015-11-07 04:53:25 -0600 )edit

so, your types are all wrong. what you read is uchar not double

please use convertTo() , if you need another type or more precision.

berak gravatar imageberak ( 2015-11-07 05:04:17 -0600 )edit

Even if i convert it it does not appear correctly..

215 gravatar image215 ( 2015-11-07 06:51:43 -0600 )edit

I added this to alleviate the issue with inf and nan ..

  if (isnan(value))
        {
          cout << num << " " << den << endl;

          if(isinf(num) && isinf(den))
          {
            dst.at<double>(row,col) = 1;
          }

          else if (isinf(num))
          {
            dst.at<double>(row,col) = den;
          }
          else if (isinf(den))
          {
            dst.at<double>(row,col) = num;
          }

        }
        else
        {
          //cout << "was not nan" << endl;
          dst.at<double>(row,col) = value;
        }

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..

215 gravatar image215 ( 2015-11-07 06:53:39 -0600 )edit

this won't go anywhere, unless you get the type right.

berak gravatar imageberak ( 2015-11-07 07:12:42 -0600 )edit