0

In order to get the mean value of a disparity map I had to write an own method to ignore negative disparity values:

float Utility::calcMeanDisparity(cv::Mat const& matrix)
{
  int total = 0;
  int numElements = 0;
  for(int r = 0; r < matrix.rows; ++r)
  {
    for(int c = 0; c < matrix.cols; ++c)
    {
      if(static_cast<float>(matrix.at<short>(r,c)) > 0)
      {
        total += static_cast<float>(matrix.at<short>(r,c));
        ++numElements;
      }
    }
  } 
  float mean = total / abs(numElements);
  std::cout << total << " / " << numElements << " = " << mean << std::endl;
  return mean;
}

but my code simply crashes after a certain time with the gdb error:

Program received signal SIGFPE, Arithmetic exception.
0x000000000044a992 in Utility::calcMeanDisparity (matrix=...) at src/utility.cpp:250
250   float mean = total / abs(numElements);

But there is no way that I am doing a division by zero. These are three of the last lines my code is trying to calculate:

950149 / 4275 = 222
804412 / 4429 = 181
873770 / 4253 = 205

I also added the abs(foo) stuff to be completely safe of divisions by zero.

Actually I do not have any idea what to do at the moment.

Maybe one of you guys has an idea for that.

hGen
  • 2,235
  • 6
  • 23
  • 43
  • 2
    Could you give us a [minimal complete example](http://stackoverflow.com/help/mcve)? – Beta May 04 '15 at 22:38
  • how should I make it even smaller? The Problem is the program crashing after a non assessable time. I feed the function with a not normalized disparitymap. I cannot provide more information because I really dont understand whats going on there – hGen May 04 '15 at 22:42
  • What does gdb think the values of `total` and `numElements` are? – Mark Plotnick May 04 '15 at 22:43
  • I dont know, I just know what the program prints. – hGen May 04 '15 at 22:44
  • 1
    Print `numElements` before `mean` calcucation and see what it prints. Right now you don't see the last value (which it crashed with) – Anton Savin May 04 '15 at 22:48
  • 1
    After the program crashes in gdb, type `p total` and `p numElements` – Mark Plotnick May 04 '15 at 22:48
  • "Minimal complete example" doesn't mean "smaller". We can't reproduce the error with what you've given us, and it's possible that the problem is elsewhere. Can you reduce your {code, data} to the smallest, simplest thing that reproduces the error, then give us the whole thing? – Beta May 04 '15 at 22:52
  • Although I solved the problem I dont know whether it would have been more helpful to post just 3 lines of code when there also could have been something wrong in general in the function. But thanks for the 'hint' – hGen May 05 '15 at 12:37

2 Answers2

0

If matrix.rows and matrix.cols are 0, numElements will be 0, and you will get a division by 0.

Since you appear to know how to use a debugger, examine the values of matrix.rows and matrix.cols to verify the cause.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

Thanks to all the help, but the problem was easier than expected.

From time to time the disparity map contained areas with zero / negative values in it. so the whole submatrix I passed into the function had a mean of 0 this is why the if condition was ignored. So the final Division was 0 / 0 --> arithmetic exception.

hGen
  • 2,235
  • 6
  • 23
  • 43