0

I searched on the web and didn't find any good answer for my problem. So this is my question: I am using OpenCV and have a function called replaceInvalidDisparties and I have to search through the pixels and check if the actual value is inf and there is a standard function in OpenCv called cvIsInf(double value) to check if the value is inf, but somehow i always get segmentation fault.

using namespace cv;
cv::Mat replaceInvalidDisparities(const cv::Mat &original_disp)
{
  cv::Mat output = orignal_disp.clone();

  //access pixel

  for(int i = 0; i< output.rows; i++)
  {
    for(int j=0; j<output.cols; j++)
   {
      //now here i want to check if the actual pixel is inf
      cvIsInf(output.at<double>(i,j));
   }
  }
}

But somehow it always give me a segmentation fault. Does anyone know the problem?

Vishaal Shankar
  • 1,648
  • 14
  • 26

2 Answers2

0

The first problem, i see is that you don't use return value of the cvIsInf function. According to opencv docs: https://docs.opencv.org/3.1.0/db/de0/group__core__utils.html#gae04046b1211c77ff19a535bd78c15964, The function returns 1 if the argument is a plus or minus infinity (as defined by IEEE754 standard) and 0 otherwise.

So, you should replace your code inside loop to smth like this:

if (cvIsInf(output.at<double>(i,j))) {
  output.at<double>(i,j) = new_value;
}

The second problem is that you doesn't return output matrix from the function:

return output;
Aleksey Petrov
  • 370
  • 2
  • 15
0

First of all, make sure you do return a value. Not returning a value is an undefined behavior: Is a return statement mandatory for C++ functions that do not return void?.

Rest of the function doesn't seems to be the cause of the segmentation fault, I would suggest changing

cv::Mat output = orignal_disp.clone();

to

cv::Mat output(100, 100, CV_64F);

Just to make sure your matrix is not ill-formed. (Was going to comment; didn't have enough reputation.)

hkchengrex
  • 4,361
  • 23
  • 33