OpenCV draws transparent Mask

The principle of drawing a transparent mask according to the rect is to add the weight of the mask and the original image, so that the desired transparent mask can be obtained. Below I set a transparency alpha, in order to prevent overflow when weighting, so that the sum of the two weights is 1. Different colors can be set here, and it should be noted that the color mapping is BGR.

Scalar color[10] =
{
    
    
	Scalar(203,192,255), //粉色
	Scalar(238,130,238), //紫罗兰
	Scalar(250,206,135), //亮天蓝
	Scalar(154,250,0),   //中春绿色
	Scalar(0,165,255)    //橙色
};
void setMask(Mat &srcImage,  Rect r, double alpha = 1)
{
    
    
	Mat temp = Mat::zeros(srcImage.size(), srcImage.type());
	cv::rectangle(temp, r, color[3], -1);

	Mat roi = srcImage(r);
	Mat mask = temp(r);
	Scalar m = cv::mean(mask);
	if(m[0] > 0 || m[1] > 0 || m[2] > 0)
		roi = roi*(1 - alpha) + mask*alpha;
}
setMask(image, rect, alpha);

The effect is as follows:

insert image description here

Guess you like

Origin blog.csdn.net/qq_44924694/article/details/130975092