cvtColor vs. manual conversion time

asked 2014-04-10 10:56:50 -0600

João M. S. Silva gravatar image

Why is cvtColort ~10 times faster than:

float coeffs[3];
coeffs[0] = 0;
coeffs[1] = 1;
coeffs[2] = 0;

gray = Mat(color.rows, color.cols, CV_8UC1);

for (int i = 0; i < color.rows; i++) {
    for (int j = 0; j < color.cols; j++) {
        gray.at<uchar>(i, j) = 0;
        for (int k = 0; k < 3; k++) {
            gray.at<uchar>(i, j) += coeffs[k] * color.at<Vec3b>(i, j)[k];
        }
    }
}

I tried changing row/col for order but it does not change the time.

edit retag flag offensive close merge delete

Comments

1

Because you have a double for loop, which is basically a quite intensive memory operation. OpenCV uses internal optimalisations like the use of parallelized for loops and NEON optimizations. So it is quite normal you will never reach the same performance with a double for loop, which is always a kind of bad programming if you want to go for processing speed!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-11 04:12:56 -0600 )edit