0

I am getting an assertion failed error in line 537 of Mat::at

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3 ) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\users\tim\document s\code\opencv\build\include\opencv2\core\mat.hpp, line 537

I am trying to populate matrices that I will use in the function cv::remap. The part of the code that is causing this failed assertion is below:

void Functions::PopulatedMapY(Mat image)
{
    mapy.create(image.rows, image.cols, CV_32FC1);
    for (int j = 0; j<image.rows; j++)
    {   
        float a = (image.rows - 1) - gazey;
        float b = (image.cols - 1) - gazex;
        for (int i = 0; i<image.cols; i++)
        {       
            mapy.at<float>(j,i) = map2y.at<float>(a+j,b+i);
        }   
    }
}

The matrix map2y was defined in the MapCreator Function as follows:

void Functions::MapCreator(Mat image, float const_a, float const_b)
{
    map2x.create(2*image.rows, 2*image.cols, CV_32FC1);
    map2y.create(2*image.rows, 2*image.cols, CV_32FC1);

    for (int m = 0; m<2*image.rows; m++)
    {
        ty = image.rows - m;
        for (int n = 0; n<2*image.cols; n++)
        {
            tx = image.cols - n;
            map2x.at<float>(m,n) = n;
            map2y.at<float>(m,n) = m +const_b*exp(-pow(tx,2)/pow(const_a, 2))*Signum(ty);

    }
    }

}

Any help would be much appreciated!

user703016
  • 37,307
  • 8
  • 87
  • 112
  • Hi, can you provide more info, like the values of i and j when it crashes and the values of gazey and gazex. – Andrei T Sep 30 '14 at 16:26
  • Hi, gazey and gazex can vary between 0 and image.rows - 1 and image.cols - 1 respectively. The program compiles and crashes as soon as it runs. Is there a way I can find out the value of I and j when it crashes? Thanks! –  Sep 30 '14 at 16:30
  • btw a and b should be ints...you should try to access indices not floats. a + i should be an int. – Andrei T Sep 30 '14 at 16:34
  • So did you manage to do it or not? – Andrei T Sep 30 '14 at 18:47

1 Answers1

0

From your error code you can find that the assertion goes false after the Mat::at call and inside this method your code goes false if:
a. The nr. of channels is less than 2.
b. data is null
c. (unsigned)i0 < (unsigned)size.p[0]
plus some others.
My suggestion in your case is the nr. of channels. CV_32FC1 means like that:
CV_< bit_depth > (S|U|F)C< nr_channels >. I suppose here is the problem, the template parameter or the data is null.
My solution just use CV_32F instead.
As a big reference take a look here:
OpenCV Error: Assertion failed, mat.cpp line 537

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
  • Hi andrei, thanks for getting back to me. I don't believe that CV_32FC1 is the problem. I can have the program run without the error if I manually set gazex and gazey to certain values. Under ideal conditions gazex and gazey should be valid at image.cols-1 and image.rows-1 respectively. However, at any value above image.rows/2 + 1 and image.cols/2 + 1, the assertion failed error arises. This hints that the problem lies in how I have populated the matrices. I will look into this a little bit and see if I made a big error. If not, I will post all these functions I am using and the main cpp file –  Sep 30 '14 at 18:52
  • Then it seems there is a problem with the data. at the specified index, probably it is null. The thing is your error it is pretty clear, there can't be a different error than the first three from above. If the channels are ok then it might be the data. – Andrei T Sep 30 '14 at 18:57
  • Thanks for your input. At this point, I'm pretty sure its due to the data, which is a direct result of how I am populating the matrices. I think there is a flaw with my logic. I will try to debug it for a while, and if I can't, I will post the whole thing up with an explanation of what I'm thinking. It would be great if you could take a look at that at that point. –  Sep 30 '14 at 19:00
  • Yes, this is in C++ and I'm using visual Studio –  Sep 30 '14 at 19:03
  • If you cannot do it, send the code on pastebin and I will take a look. – Andrei T Sep 30 '14 at 19:07