Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Understand the memory managment while using imread() on allocated Mat

Hello everyone.

I'm currently using openCV 2.4.2 for C++ development in the fields of image processing. I just noticed a behaviour extracting some edges with the Canny operator which i can't really make sense of. In fact i dont believe it really has to do with the Canny implementation but more with some memory managment which eiterhway is bugged somehow or i'm using it the wrong way (or in an unsupported one).

Please help me to understand what is going on. See the following code snippet which produces this behaviour.

Mat grayImage;
for (int k = 0; k < 20; k++) 
{
   Mat edgeMat;
   grayImage = imread( "myImage.jpg", 0 );

   /// Canny detector
   Canny( grayImage, edgeMat, 80, 200, 3, true );

   // Count the number of edge pixels
    unsigned int count = 0;
    for (int i = 0; i < edgeMat.rows; i++)
    {
        // Use the raw pointer for fastest access
        const unsigned* Mi = edgeMat.ptr<unsigned>(i);
        for (int j = 0; j < edgeMat.cols; j++)
        {
            // If the pixel is not black (it belongs to an edge), count.
            if (Mi[j] != 0)
            {
                count++;
            }
        }

    }

    cout << count << endl;
  }

Now the output is something like (showing the number of edge pixels per iteration):

2651
2651
2398
2651
2398
...

To sum it up. It seems that the Canny produces different edges allthough the same image and parameters are used. But when i release the image Mat first with edgeMat.release() The every time the same number of edges is found.

But why is it that way?

Understand the memory managment while using imread() on allocated Mat

Hello everyone.

I'm currently using openCV 2.4.2 for C++ development in the fields of image processing. I just noticed a behaviour extracting some edges with the Canny operator which i can't really make sense of. In fact i dont believe it really has to do with the Canny implementation but more with some memory managment which eiterhway is bugged somehow or i'm using it the wrong way (or in an unsupported one).

Please help me to understand what is going on. See the following code snippet which produces this behaviour.

Mat grayImage;
for (int k = 0; k < 20; k++) 
{
   Mat edgeMat;
   grayImage = imread( "myImage.jpg", 0 );

   /// Canny detector
   Canny( grayImage, edgeMat, 80, 200, 3, true );

   // Count the number of edge pixels
    unsigned int count = 0;
    for (int i = 0; i < edgeMat.rows; i++)
    {
        // Use the raw pointer for fastest access
        const unsigned* Mi = edgeMat.ptr<unsigned>(i);
        for (int j = 0; j < edgeMat.cols; j++)
        {
            // If the pixel is not black (it belongs to an edge), count.
            if (Mi[j] != 0)
            {
                count++;
            }
        }

    }

    cout << count << endl;
  }

Now the output is something like (showing the number of edge pixels per iteration):

2651
2651
2398
2651
2398
...

To sum it up. It seems that the Canny produces different edges allthough the same image and parameters are used. But when i release the image Mat first with edgeMat.release() The every time the same number of edges is found.

But why is it that way?