【问题标题】:How to create a semi transparent shape?如何创建一个半透明的形状?
【发布时间】:2014-08-20 06:30:38
【问题描述】:

我想知道如何在OpenCV中绘制半透​​明的形状,类似于下图(来自http://tellthattomycamera.wordpress.com/

我不需要那些花哨的圆圈,但我希望能够绘制一个矩形,例如,在 3 通道彩色图像上并指定矩形的透明度,例如

rectangle (img, Point (100,100), Point (300,300), Scalar (0,125,125,0.4), CV_FILLED);

其中0,125,125 是矩形的颜色,0.4 指定透明度。 然而,OpenCV 并没有将此功能内置到其绘图功能中。如何在 OpenCV 中绘制形状,以便在其上绘制的原始图像通过该形状部分可见?

【问题讨论】:

    标签: c++ opencv shape alpha-transparency


    【解决方案1】:

    下图说明了使用 OpenCV 的透明度。您需要在图像和矩形之间进行 alpha 混合。下面是执行此操作的一种方法的代码。

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    
    int main( int argc, char** argv )
    {
        cv::Mat image = cv::imread("IMG_2083s.png"); 
        cv::Mat roi = image(cv::Rect(100, 100, 300, 300));
        cv::Mat color(roi.size(), CV_8UC3, cv::Scalar(0, 125, 125)); 
        double alpha = 0.3;
        cv::addWeighted(color, alpha, roi, 1.0 - alpha , 0.0, roi); 
    
        cv::imshow("image",image);
        cv::waitKey(0);
    }
    

    【讨论】:

    • 感谢您的回复和您的耐心,但我用另一种方式解决了它,使用函数 addWeighted。像这样:cap >> img; img.copyTo(复制);矩形(复制,点(100,100),点(300.300),标量(0,125,125),CV_FILLED,CV_AA,0); addWeighted (copy, 0.4, 0.1 to 0 .4,0 img, img); imshow("img",img);
    • @Bull,我有一个疑问,你没有在addWeighted 函数调用中使用image Mat 对象,它是如何更新为图像的?谢谢
    • @Prakash roi 中的数据是image 中数据的矩形切片。也就是说,如果您从roi 读取,您正在从image 读取,如果您修改roi,您也正在修改image
    • 如何为任意形状或多边形做到这一点?我尝试了这个cv::Mat roi = image(cv::Mat(points));(其中points 是cv::Point 的向量),但它不起作用。
    • @OliviaStork,简单的方法是使用 Alexander Taubenkorb 的答案并用代码替换对 cv::rectangle() 的调用以在叠加层中绘制任意形状。
    【解决方案2】:

    在 OpenCV 3 中,这段代码对我有用:

    cv::Mat source = cv::imread("IMG_2083s.png");
    cv::Mat overlay;
    double alpha = 0.3;
    
    // copy the source image to an overlay
    source.copyTo(overlay);
    
    // draw a filled, yellow rectangle on the overlay copy
    cv::rectangle(overlay, cv::Rect(100, 100, 300, 300), cv::Scalar(0, 125, 125), -1);
    
    // blend the overlay with the source image
    cv::addWeighted(overlay, alpha, source, 1 - alpha, 0, source);
    

    来源/灵感来源:http://bistr-o-mathik.org/2012/06/13/simple-transparency-in-opencv/

    【讨论】:

      【解决方案3】:

      添加到 Alexander Taubenkorb 的答案中,您可以通过将 cv::rectangle 线替换为您要绘制的形状来绘制随机(半透明)形状。

      例如,如果你想画一系列半透明的圆,你可以这样做:

      cv::Mat source = cv::imread("IMG_2083s.png");  // loading the source image
      cv::Mat overlay;  // declaring overlay matrix, we'll copy source image to this matrix
      double alpha = 0.3;  // defining opacity value, 0 means fully transparent, 1 means fully opaque
      
      source.copyTo(overlay);  // copying the source image to overlay matrix, we'll be drawing shapes on overlay matrix and we'll blend it with original image
      
      // change this section to draw the shapes you want to draw
      vector<Point>::const_iterator points_it;  // declaring points iterator
      for( points_it = circles.begin(); points_it != circles.end(); ++points_it )  // circles is a vector of points, containing center of each circle
          circle(overlay, *points_it, 1, (0, 255, 255), -1);  // drawing circles on overlay image
      
      
      cv::addWeighted(overlay, alpha, source, 1 - alpha, 0, source);  // blending the overlay (with alpha opacity) with the source image (with 1-alpha opacity)
      

      【讨论】:

        【解决方案4】:

        对于 C++,我个人喜欢标量乘法和矩阵加法的重载运算符的可读性:

        ... same initial lines as other answers above ...
        
        // blend the overlay with the source image
        source = source * (1.0 - alpha) + overlay * alpha;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-09
          • 2014-10-10
          • 1970-01-01
          相关资源
          最近更新 更多