Ask Your Question
0

Error when get color from pixel in visual studio C++

asked 2016-09-24 09:50:01 -0600

greenworld gravatar image

updated 2016-09-25 11:09:21 -0600

LBerger gravatar image

This is function to get the color from a given Mat image,

string getColorFromPx(Mat *src, uint16_t x, uint16_t y){
Point pt = Point(x, y);
//Mat img = (*src).clone();
Vec3b& levelPxImg = imgHSV.at<Vec3b>(y, x);
if ((levelPxImg[0] != NULL) && (levelPxImg[1] != NULL) && (levelPxImg[2] != NULL)){
    //H(openCV) / 2 = H (HSV_Real);
    if (levelPxImg[0] > 170 && levelPxImg[0] < 180)
        return "R";
    else if (levelPxImg[0] > 107 && levelPxImg[0] < 122)
        return "B";
    else
        return "xx";
}

}

the program can work for a period time after that it notify an error as below:

" First-chance exception at 0x00007FF64C1C30A3 in OpenCVex1.exe: 0xC0000005: Access violation reading location 0x0000000000000018. If there is a handler for this exception, the program may be safely continued. "

and current breakpoint is halted at return line as below:

int compare(const _Elem *_Ptr) const {// compare [0, _Mysize) with [_Ptr, <null>)

_DEBUG_POINTER(_Ptr);

-------> return (compare(0, this->_Mysize, _Ptr, _Traits::length(_Ptr))); }

(this function locate in VS/include/xstring file).

Please take a look and help me to show problem.

edit retag flag offensive close merge delete

Comments

You should learn c++ debugger. When error throw use stack trace and select line with function name getColorFromPx. Then you will have all variable values. Check x and y values.

LBerger gravatar imageLBerger ( 2016-09-25 11:37:33 -0600 )edit

some bad things here:

  • you're passing in a Mat *src, but you never use it
  • levelPxImg[0] != NULL <-- it's not a pointer.
  • like said before, you never check, if x,y are inside your image
  • your hsv ranges are off. (0-30 is red, too !)
berak gravatar imageberak ( 2016-09-26 08:39:13 -0600 )edit

Yes, I check it before I pass x and y, I think it has problem with the passing Mat, so I just replace it to the original matrix (for surely)

greenworld gravatar imagegreenworld ( 2016-09-27 07:17:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-09-26 07:38:15 -0600

Guyygarty gravatar image

updated 2016-09-26 07:39:25 -0600

Looks to me like you are trying to access a point outside your Mat.

You need to explicitly check that the values of x and y are smaller than src.cols and src.rows respectively before calling at.

OpenCV checks this internally but throws an exception if they are not.

guy

edit flag offensive delete link more

Comments

I checked it before passing x, y. Thank you

greenworld gravatar imagegreenworld ( 2016-09-27 07:17:40 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-24 09:50:01 -0600

Seen: 243 times

Last updated: Sep 26 '16