Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a semi transparent shape?

I would like to know how to draw semi-transparent shapes in OpenCV, similar to those in the image below (from http://tellthattomycamera.wordpress.com/)

enter image description here

I don't need those fancy circles, but I would like to be able to draw a rectangle, e.g, on a 3 channel color image and specify the transparency of the rectangle, something like

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

where 0,125,125 is the color of the rectangle and 0.4 specifies the transparency. However OpenCV doesn't have this functionality built into its drawing functions. How can I draw shapes in OpenCV so that the original image being drawn on is partially visible through the shape?

like image 481
user3788572 Avatar asked Jun 29 '14 22:06

user3788572


People also ask

How do you make a semi box transparent?

You can use opacity:0.5; to make the object half transparent. Form 0 to 1 (100%).

How do you make a semi transparent shape in Canva?

On the toolbar below the editor, tap on Transparency. You may have to swipe through the toolbar to see the Transparency icon. Tap and drag the slider to adjust.


1 Answers

The image below illustrates transparency using OpenCV. You need to do an alpha blend between the image and the rectangle. Below is the code for one way to do this.

enter image description here

#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); } 
like image 104
Bull Avatar answered Oct 02 '22 13:10

Bull