Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Optimize - draw gradient function

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;
    }
}