0

I have the following code

void Segmentation::CallBackFunc(int event, int x, int y, int flags, void* param)
{
    Vec3b bgrPixel;
    int i = 0;
    //Press 0, if face
    if (event == EVENT_LBUTTONDOWN)
    {
        Mat &img = *((Mat*)(param)); // 1st cast it back, then deref
        circle(img, Point(x, y), 3, Scalar(0, 255, 0), 2);
        try
        {
            bgrPixel = img.at<Vec3b>(x, y);
        }
        catch (Exception ex)
        {
            string message = ex.msg;
        }
        imshow("Display window", img);

Everything works ok until I click on a pixel which is relatively lighter(example white). At that case I receive the following exception:

error: (-215) dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0]
    && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels())
    && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()
    in function cv::Mat::at
Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
  • You should probably first verify whether the `(x,y)` coordinates are within the bounds of the image, before accessing the pixel. The other possibility could be that `img` doesn't have 3 channels. – Dan Mašek Apr 10 '16 at 21:16
  • 1
    change to bgrPixel = img.at(y, x); (it is row-first in matrix notation) or bgrPixel = img.at(cv::Point(x, y)); – Micka Apr 11 '16 at 00:30
  • Possible duplicate of [OpenCV Point(x,y) represent (column,row) or (row,column)](http://stackoverflow.com/questions/25642532/opencv-pointx-y-represent-column-row-or-row-column) – Miki Apr 11 '16 at 09:58
  • the question here is, that it is working, when I am clicking on a dark pixel.So changed places for row, colomn, or image having 3 channels cant be the case – Lusine Mkrtchyan Apr 11 '16 at 18:41

0 Answers0