Ask Your Question

cv::fillPoly unexpected behavior

asked 2019-02-14 11:49:25 -0600

xhuvom gravatar image

I am trying to convert a color image to greyscale and then apply Red color on a specific region. I have tried:

Color Conversion

cv::Mat facadeColor  = cv::imread("color.jpg");
cv::Mat facade;
cv::cvtColor(facadeColor, facade, cv::COLOR_BGR2GRAY);

Region Selection and Apply Solid Color

cv::Size s = facade.size();
    int h = s.height;
    int w = s.width;
    cv::Point rook_points[1][4];
      rook_points[0][0] = cv::Point( 0, h/2 );
      rook_points[0][1] = cv::Point( 0, h );
      rook_points[0][2] = cv::Point( w, h );
      rook_points[0][3] = cv::Point( w, h/2 );
      const cv::Point* ppt[1] = { rook_points[0] };
        int npt[] = { 4 };

        cv::fillPoly( facade, ppt, npt, 1, cv::Scalar( 0, 0, 255 ), 8 );

The problem I'm facing is that when the greyscale conversion is not performed, the fillPoly is filling correctly as red. But when the greyscale conversion is performed, the fillPoly output also comes as greyscale wheres it is expected to fill a specific region as RED.

I am so confused with it. Please help.

edit retag flag offensive close merge delete


it's unclear:

  • what do you need the grayscale image for ?
  • what is in facade if you don't convert it. ?
  • why do you try to draw some color polygon into a grayscale image ? (won't work)
berak gravatar imageberak ( 2019-02-14 12:02:21 -0600 )edit

Yes, exactly I want to draw a color polygon on a greyscale image. I have to do it on purpose. Isn't it possible in OpenCV?

xhuvom gravatar imagexhuvom ( 2019-02-14 12:40:15 -0600 )edit

no, it's not possible. you cannot draw 3 color channels into a 1 channel image

you can, however, convert your grayscale image back to 3 (identical) channels, then it is possible.

berak gravatar imageberak ( 2019-02-14 12:49:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-02-17 14:00:40 -0600

Hwy2Hell gravatar image

As berak indicated I suggest converting the greyscale image back to color before applying the drawing function:

cv::cvtColor(facade, facadeColor, cv::COLOR_GRAY2BGR);
cv::fillPoly(facadeColor, ppt, npt, 1, cv::Scalar( 0, 0, 255 ), 8);
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-02-14 11:49:25 -0600

Seen: 1,089 times

Last updated: Feb 14 '19