void createAlpaMat(Mat &mat)
{
for(int i=0;i<mat.rows;i++)
{
for(int j=0;j<mat.cols;j++)
{
Vec4b &rgba=mat.at<Vec4b>(i,j);
rgba[0]=UCHAR_MAX;
rgba[1]=saturate_cast<uchar>((float(mat.cols-1))/((float)mat.cols)*UCHAR_MAX);
rgba[2]=saturate_cast<uchar>((float(mat.rows-1))/((float)mat.rows)*UCHAR_MAX);
rgba[3]=saturate_cast<uchar>(0.5*(rgba[1]+rgba[2]));
}
}
}
Mat mat(480,640,CV_8UC4);
createAlpaMat(mat);
vector<int>compression_param;
compression_param.push_back(IMWRITE_PNG_COMPRESSION);
compression_param.push_back(9);
try {
imwrite("res.png",mat,compression_param);
} catch (...) {
cout<<"write error"<<endl;
}
标签:mat,cast,float,param,opencv,透明图,rgba,绘制,compression
From: https://www.cnblogs.com/xiaohai123/p/17213044.html