Ask Your Question

ColorConversionCodes COLOR_GRAY2RGB seems to behave like BGR

asked 2017-01-17 19:55:06 -0500

j0h gravatar image

This code sample will draw a red circle on a white background. using the ColorConversionCodes COLOR_GRAY2RGB marker, the color identity Scalar(0,0,255) produces red. why not blue? Does the circle function convert the color code back to BGR?

 #include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char ** argv){

    Mat md0, md1, md2;
    md0 = Mat::zeros(200,400,CV_32F);
    md1 = Mat::ones(200,400,CV_32F);

//drawing function
    Point2f p(77, 100);
    cvtColor(md1, md2, COLOR_GRAY2RGB );
    circle( md2, p,50,Scalar(0,0,255),2,8,0);

    imshow("MD2", md2);

    return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-17 20:30:09 -0500

OpenCV uses BGR Color Space. In your code md2 is in RGB Color Space but you are expecting to display this using imshow directly without any RGB to BGR conversion. So the correct way is to convert the Mat to BGR color space before imshow. i.e like this

cvtColor(md2, md2, COLOR_RGB2BGR );
imshow("MD2", md2);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-01-17 19:55:06 -0500

Seen: 106 times

Last updated: Jan 17 '17