Optimize - draw gradient function

asked 2015-05-28 17:02:36 -0500

mariusz gravatar image

Hi,

Performance analysis shows me that below function is the slowest function in my program. Is it possible to optimize performance of this gradient function? (i.e. by some matrix operation)

void gradient(cv::Mat& image, cv::Scalar c1, cv::Scalar c2)
{
    auto alphaStep = 1.0 / image.rows;
    auto alpha = 0.0;
    for (int y = 0; y < image.rows; y++) {
        cv::Vec4b* rowImage = image.ptr<cv::Vec4b>(y);
        cv::Scalar color(c1(0)*(1 - alpha) + c2(0)*(alpha), c1(1)*(1 - alpha) + c2(1)*(alpha), c1(2)*(1 - alpha) + c2(2)*(alpha), 255);
        for (int x = 0; x < image.cols; x++) {
            rowImage[x][0] = color[0];
            rowImage[x][1] = color[1];
            rowImage[x][2] = color[2];
            rowImage[x][3] = color[3];
        }
        alpha += alphaStep;
    }
}
edit retag flag offensive close merge delete

Comments

Can't it be written as a matrix operation?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-05-29 02:54:22 -0500 )edit

Could you help me write it as matrix operation?

mariusz gravatar imagemariusz ( 2015-05-29 13:24:57 -0500 )edit