Ask Your Question
0

drawContours with Gradient color

asked 2018-07-18 05:54:48 -0600

kk143g gravatar image

How to drawContours with gradient colors, not with single color.

cv::drawContours( img, contours, i, color, CV_FILLED, 8, std::vector<cv::vec4i>(), 0, cv::Point() );

As drawContours require a single color, So my question is how to pass a gradient instead of single color.

edit retag flag offensive close merge delete

Comments

there is no such thing builtin.

berak gravatar imageberak ( 2018-07-18 06:01:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-07-18 07:31:45 -0600

kbarni gravatar image

You have to do it manually. Use the LineIterator to draw lines algorithmically; you can change the color of each pixel.

Then you have to iterate the lines that form the polygon of a contour and through the different contours.

Here is the pseudocode of the algorithm. It's almost ready, but it isn't tested, so you'll have to finish it and adapt it to your needs.

for(int k=0;k<contours.size();k++){
    contour = contours[k];  //the current contour
    for(int l=0;l<contour.size();l++){
        p0=contour[l];p1=contour[(l+1)%contour.size()]; //the endpoints of the line
        //the last line will be between the last and first points, that's why the % operator
        LineIterator it(img, p0, p1, 8); //this will generate the list of points on the line.
        for(int i=0; i<it.count; i++)
        {
            Vec3b(*it)=nextColor(); //it's up to you to generate the gradient as you like
            it++;
        }
    }
}
edit flag offensive delete link more

Comments

what is nextColor()?

jsxyhelu gravatar imagejsxyhelu ( 2018-07-20 07:26:46 -0600 )edit

It's just a function to generate the color of the next pixel. It depends what kind of gradient you want. You might also generate a look-up table (LUT) to get the next color.

For a grayscale (and one channel) gradient, it is:

uchar color=0;

uchar nextColor() { return (color++)%256; }

You can also imagine a gradient dependent on the Y parameter of the pixel, and so on...

kbarni gravatar imagekbarni ( 2018-07-23 06:33:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-18 05:54:48 -0600

Seen: 2,677 times

Last updated: Jul 18 '18