3

Suppose, I have two points with different colors. How can I draw the line between them using OpenCV (whatever python or c++), so that color of the line gradiently changes from one color to another? It seems, that it can not be easy done with OpenCV, but if somebody knows a simple solution, please, help me)

Thank you.

Alex Chudinov
  • 654
  • 1
  • 6
  • 12

2 Answers2

5

OpenCV contains a LineIterator.

it handles the math and gives you all the pixel coordinates.

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

int main()
{
    cv::Mat canvas { 50, 50, CV_32FC3, cv::Scalar::all(0.2) };
    
    cv::Point pt1 { 10,30 };
    cv::Point pt2 { 40,20 };

    cv::Vec3f color1 { 0,1,1 }; // yellow
    cv::Vec3f color2 { 1,1,0 }; // cyan

    // see https://docs.opencv.org/master/dc/dd2/classcv_1_1LineIterator.html
    cv::LineIterator it { canvas, pt1, pt2, 8 };

    for (int i = 0; i < it.count; ++i, ++it)
    {
        float alpha = i / (float)(it.count - 1); // 0..1 along the line

        cv::Vec3f blended = color1 * (1-alpha) + color2 * (alpha);

        canvas.at<cv::Vec3f>(it.pos()) = blended;
    }

    // show picture
    cv::namedWindow("canvas", cv::WINDOW_NORMAL);
    cv::resizeWindow("canvas", 500, 500);
    cv::imshow("canvas", canvas);
    cv::waitKey(-1);

    return 0;
}

enter image description here

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
1

A simple solution would be to generate intermediate points and draw smaller lines between them, each with the following shade of the gradient. A code similar to the following might do the job:

past_center = intermediate_points[0]
for i, point in enumerate(intermediate_points, start=1):
        color_val = int(np.sqrt(64 / float(i + 1)) * 2)
        if color_val < 1:
            color_val = 0                              
        cv2.line(frame, (int(past_point[0]), int(past_point[1])), (int(point[0]), int(point[1])), (color_val, color_val, color_val), thickness)
        past_point = point
Bedir Yilmaz
  • 3,823
  • 5
  • 34
  • 54