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/)
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?
You can use opacity:0.5; to make the object half transparent. Form 0 to 1 (100%).
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.
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.
#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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With