Generate transparent shape on image

Here is an example code to generate transparent shape on image. Need to pay attention can not use cv::Mat mask(mat) to create the mask image, because mask will be just a shallow copy of mat.

int GenerateTransparentMask()
{
Mat mat = imread("test.jpg");
if (mat.empty())
return -; Size size = mat.size();
cv::Mat mask ( size, mat.type() );
mat.copyTo(mask); //cv::Mat mask(mat); //Can not work, becuase mask will be just a copy of mat, if draw on mask, means draw on mat too Rect rect(, , , );
rectangle(mask, rect, Scalar(, , ), CV_FILLED, CV_AA, ); double alpha = 0.3;
double beta = 1.0 - alpha;
cv::addWeighted(mask, alpha, mat, beta, 0.0, mat); cvNamedWindow("Result");
imshow("Result", mat); cvWaitKey(); //wait for a key press
return ;
}

The result will as below:

Generate transparent shape on image

If you need to generate complicated mask on an image, need to use a little different method. Below is the example code.

int GenerateTransparentMask()
{
const Mat mat = imread("DetectingContours.jpg");
if (mat.empty())
return -; imshow("origianl", mat); Size size = mat.size();
cv::Mat copy ( size, mat.type() );
mat.copyTo ( copy ); //cv::Mat mask(mat); //Can not work, becuase mask will be just a copy of mat, draw on mask, means draw on mat too cv::Mat mask(size, CV_8U);  //mask must be CV_8U
mask.setTo(Scalar());
Rect rect(, , , );
rectangle(mask, rect, Scalar(, , ), CV_FILLED, CV_AA, );
rectangle(mask, Rect(, , , ), Scalar(, , ), CV_FILLED);
circle(mask, Point(, ), , Scalar(), CV_FILLED); copy.setTo(Scalar(, , ), mask);
//imshow("copy", copy); double alpha = 0.5;
double beta = 1.0 - alpha; cv::Mat result(size, mat.type());
cv::addWeighted(copy, alpha, mat, beta, 0.0, result); cvNamedWindow("Result");
imshow("Result", result); cvWaitKey(); //wait for a key press
return ;
}

Generate transparent shape on image

上一篇:Java system.getproperty获取环境属性


下一篇:Qt 检验器 三种典型类的用法