博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Generate transparent shape on image
阅读量:5260 次
发布时间:2019-06-14

本文共 2013 字,大约阅读时间需要 6 分钟。

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 -1;    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(100, 100, 100, 100);    rectangle(mask, rect, Scalar(0, 0, 255), CV_FILLED, CV_AA, 0);    double alpha = 0.3;    double beta = 1.0 - alpha;    cv::addWeighted(mask, alpha, mat, beta, 0.0, mat);    cvNamedWindow("Result");    imshow("Result", mat);    cvWaitKey(0); //wait for a key press    return 0;}

 The result will as below: 

 

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 -1;    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(0));    Rect rect(200, 100, 200, 200);    rectangle(mask, rect, Scalar(255, 255, 255), CV_FILLED, CV_AA, 0);    rectangle(mask, Rect(280, 120, 40, 40), Scalar(0, 0, 0), CV_FILLED);    circle(mask, Point(300, 220), 30, Scalar(0), CV_FILLED);    copy.setTo(Scalar(0, 0, 255), 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(0); //wait for a key press    return 0;}

转载于:https://www.cnblogs.com/shengguang/p/5240548.html

你可能感兴趣的文章
网络穿透
查看>>
2015/8/4 告别飞思卡尔,抛下包袱上路
查看>>
软件开发与模型
查看>>
Codeforces Round #306 (Div. 2) A
查看>>
161017、SQL必备知识点
查看>>
hdu 1541Stars
查看>>
kill新号专题
查看>>
MVC学习系列——Model验证扩展
查看>>
C# GC 垃圾回收机制
查看>>
mysqladmin 修改和 初始化密码
查看>>
字符串
查看>>
java的同步实现
查看>>
H3C 单区域OSPF配置示例一
查看>>
vue2.x directive - 限制input只能输入正整数
查看>>
iOS常用开源库2
查看>>
实现MyLinkedList类深入理解LinkedList
查看>>
核密度图(直方图的拟合曲线)
查看>>
自定义返回模型
查看>>
使用Git、Git GUI和TortoiseGit
查看>>
Servlet 远程服务IO流传输数据
查看>>