Speeding up pixelwise operations

asked 2014-10-02 17:28:21 -0600

kovand11 gravatar image

I would like to compute an average LoG (Laplacian of Gauss) score over a given AOI. The problem, with the naive approach is, that it takes a really long time, and i have a stong feeling that it may have been done faster.

cv::Mat src = cv::Mat( imgSize.height(), imgSize.width(), CV_8UC4, (void*)imgPtr, (size_t)pitch );

cv::Mat roi;
if( subROI.isNull() ) {
    roi = src;
} else {
    cv::Rect rect = cv::Rect( subROI.x(), subROI.y(), subROI.width(), subROI.height() );
    roi = src(rect);
cv::Mat grey;
cv::cvtColor(roi, grey, cv::COLOR_RGB2GRAY);
cv::GaussianBlur(grey, grey, cv::Size(3, 3), 0, 0, cv::BORDER_DEFAULT);
cv::Mat laplaced;
cv::Laplacian(grey, laplaced, CV_16S, 3, 1, 0, cv::BORDER_DEFAULT);
cv::convertScaleAbs(laplaced, laplaced);

float avg = 0.0;
for (int i = 0; i < laplaced.rows; i++)
    for (int j = 0; j < laplaced.cols; j++)
        avg += (float)laplaced.at<unsigned char>(i, j);
avg /= laplaced.rows*laplaced.cols;

return avg;

Any tip, to make the code run faster? (gonna run on mobile ivy bridge CPU)

edit retag flag offensive close merge delete



you can replace the avg i,j loops by mean(), but i bet, most of the time spent is in GaussianBlur() and Laplacian()

berak gravatar imageberak ( 2014-10-03 01:15:43 -0600 )edit

don't 'feel', profile it:

uint64 t0 = cv::getTickCount();

// some operation

uint64 t1 = cv::getTickCount();

cerr << "operation took : " << ((t1-t0)/cv::getTickFrequency()) << " sec." << endl

berak gravatar imageberak ( 2014-10-03 01:18:10 -0600 )edit

Even better, use a profiler tool like valgrind to really look into what is taking the longest time! It will also show you where memory leaks are happening and such. Besides that you could try the GPU and OpenCL implementations to speed things up? Also running over Mat elements by row and column seems the dumb approach, look here on how to loop efficiently.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-03 03:50:47 -0600 )edit