Question

If i draw a line or some figure on a transparent image(4 channels) with a certain colour, after i save the image, the line/figure it's white, even though i draw with a red colour and i create a transparent image or i open one from internet. I'm missing something. Any suggestions?

Original picture http://i.imgur.com/Bfy5tNq.png?1

Picture after drawing 2 lines with red colour http://i.imgur.com/Pl7BHPT.png?1

    Mat image;
    image = imread("ball.png", -1);

    if(image.channels() == 4)
    {
        //vector<Mat> ch;
        //split(image, ch);

        cv::line(image,cv::Point(0,0),cv::Point(image.cols,image.rows),CV_RGB(255,0,0),7,CV_AA,0);
        cv::line(image,cv::Point(0,image.rows),cv::Point(image.cols,0),CV_RGB(255,0,0),7,CV_AA,0);

        imwrite("out.png", image); 

        cv::namedWindow("Transparent", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
        cv::imshow("Transparent",image);
        waitKey(0);

        Mat temp(image);
         cv::line(temp,cv::Point(0,0),cv::Point(temp.cols,temp.rows),CV_RGB(255,0,0),7,CV_AA,0);
        cv::line(temp,cv::Point(0,temp.rows),cv::Point(temp.cols,0),CV_RGB(255,0,0),7,CV_AA,0);
        double opacity = 0.9;
        cv::addWeighted(temp,opacity,image,1 - opacity,0,image);
        cv::namedWindow("opacity",CV_WINDOW_AUTOSIZE);
        cv::imshow("opacity",image);
        imwrite("opacity.png",image);
        waitKey(0);



        string path="myimage.png";
        Mat myImage(image.rows,image.cols,CV_8UC4);
        myImage=cv::Scalar(255,255,255,0);
        cv::line(myImage,cv::Point(0,0),cv::Point(image.cols,image.rows),CV_RGB(255,0,0),7,CV_AA,0);
        cv::line(myImage,cv::Point(0,image.rows),cv::Point(image.cols,0),CV_RGB(255,0,0),7,CV_AA,0);
        imwrite(path,myImage);

I've tried all this 3 methods and all result in the same picture.

Was it helpful?

Solution

I think the macro CV_RGB creates you a cv::Scalar with entries (b,g,r,0).

this code works for me:

    cv::line(image,cv::Point(0,0),cv::Point(image.cols,image.rows),cv::Scalar(0,0,255,255),7,CV_AA,0);
    cv::line(image,cv::Point(0,image.rows),cv::Point(image.cols,0),cv::Scalar(0,0,255,255),7,CV_AA,0);

with this result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top