Speed test for pixel access - OpenCV Mat vs. array

The speed test

What is the faster access to pixels?

  • cv::Mat.at<TYPE>(y,x) OR
  • write values to a 2D array and access these values
void TestSpeedToPixelAccess(Mat& img)
{
    int w = img.cols;
    int h = img.rows;
    static const int N = 10000;
 
    int startTime;
    unsigned long sum;
 
    Mat gray;
    cvtColor(img,gray,CV_RGB2GRAY);
 
    uchar** a = new uchar*[h];
    for (int y=0; y<h; y++)
    {
        a[y] = new uchar[w];
        for (int x=0; x<w; x++)
            a[y][x] = gray.at<uchar>(y,x);
    }
 
 
    // access speed test: gray cv::Mat
    startTime = clock();
    sum = 0;
    for (int i=0; i<N; i++)
    {
        for (int y=0; y<h; y++)
            for (int x=0; x<w; x++)
            {
                sum += gray.at<uchar>(y,x);
            }       
    }
    XLogger::Access()->Log( QString("gray cv::Mat access time: %0 --> sum=%1").arg( clock() - startTime ).arg(sum) );
 
 
    // access speed test: a array
    startTime = clock();
    sum = 0;
    for (int i=0; i<N; i++)
    {
        for (int y=0; y<h; y++)
            for (int x=0; x<w; x++)
            {
                sum += a[y][x];
            }       
    }
    XLogger::Access()->Log( QString("a access time: %0 --> sum=%1").arg( clock() - startTime ).arg(sum) );
}

Results & Conclusions

In Release mode I get the following numbers:

gray cv::Mat access time: 289 --> sum=1621590304
a access time: 294 --> sum=1621590304

So accessing 10.000 times each pixel of the image individually results in a total access time of 289 ms and 294 ms respectively.

So there is not a significant difference between both access methods!

 
public/speed_test_for_pixel_access_-_opencv_mat_vs._array.txt · Last modified: 2012/08/29 11:52 (external edit) · []
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki