9
votes

I'm trying to draw gradient lines on an image. I want my lines to be green colored and I use Scalar(0,255,0). Still, I'm getting only black color. For Scalar(0,0,0) also I'm getting black. For Scalar(255,255,255) I get white, but no other color for any combination. Part of the code is given below:

line(visual_image,
     Point(x1*scaleFactor, y1*scaleFactor),
     Point(x2*scaleFactor, y2*scaleFactor),
     Scalar(0,255,0),
     1,8,0);
2
check the type() of visual_image. if it is 0(gray) , not 16 (rgb) you can't draw colors into it.berak
great! that makes sense. Thanks. So is there no way I can draw colored lines on a gray image?Harsh Wardhan
@HarshWardhan how many channels have a gray image? Then where do you think information about green should be stored?"Moia

2 Answers

19
votes

since you can't draw coloured lines,circles,etc into a grayscale image, you have to convert it to 3 channels first :

Mat rgb;
cvtColor(visual_image, rgb, CV_GRAY2BGR); 
// now draw your lines:
line( rgb,
   Point(x1*scaleFactor, y1*scaleFactor),
   Point(x2*scaleFactor, y2*scaleFactor),
   Scalar(0,255,0),
   1,8,0);