Ask Your Question
0

Strange bug in opencv

asked 2018-04-22 00:14:14 -0600

Kafan gravatar image

I am getting a very strange Segmentation fault error in my project and after 1 full day of my debugging, I have narrowed it down to this part. Below sample code is giving me error at when accessing pixel value at row higher than 476 but it should work till 479, given height of matrix is 480. My opencv version is 3.4.1 and using this on Mac OS 10.13.3

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <chrono>
#include <thread>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
  Mat image = Mat::zeros(480,640,CV_8UC1);

  while (true){
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    cout << image.at<int>(477,638)<< endl; // Error is HERE
  }

  return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-22 00:53:14 -0600

berak gravatar image

updated 2018-04-22 01:22:35 -0600

that's not a bug, but a programmer error, aka : your fault.

if the Mat's type is CV_8U, you have to use

image.at<uchar>(row,col);

easy to get wrong, so rather try to avoid per-pixel access at all.

what you're doing is basically like:

image[ (width * y + x) * sizeof(int) ]

and this will get out of bounds easily near the edges, causing undefined behaviour.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-22 00:14:14 -0600

Seen: 276 times

Last updated: Apr 22 '18