Search code examples
opencvlinecontourpolyfills

poly opencv semi-transperent


My question is for Opencv experts, I've detected road lines (left and right lines) so I was aiming to paint the road area with semi-transparent blue. So I used :

cv::fillPoly(image, ppt, npt, 1, CV_RGB(0, 0,200), lineType);

ppt- contain the points for right and left, npt- number of points

But, what I got it filled area over the road which is not my aim.

So, my question is there any solution to paint the road area with semi-transparent? I was told to add another channel like:

cv::Mat channel[3];
split(image, channel); 
    channel[0] = cv::Mat::zeros(image.rows, image.cols, CV_8UC1);
     merge(channel, 3, image);cv::imshow("kkk",image); 

But the thing is I got all the image in semi-transparent and I want only the road area. Any ideas appreciated!!

thanks


Solution

  • try this code (couldnt test it on the mobile):

    cv::Mat polyImage = cv::Mat(image.rows, image.cols, CV_8UC3,cv::Scalar (0,0,0));
    cv::fillPoly(polyImage, ppt, npt, 1, CV_RGB(0, 0,200), lineType);
    
    float transFactor = 0.5f; // the bigger the more transparent
    
    for(int y=0;y <image.rows;++y)
        for(int x=0;x <image.cols; ++x)
        {
            if(polyImage.at<cv::Vec3b>(y,x) != cv::Vec3b(0,0,0) )
                image.at<cv::Vec3b>(y,x) = (transFactor)*image.at<cv::Vec3b>(y,x) + (1.0f - transFactor)*polyImage.at<cv::Vec3b>(y,x);
        }