Android - Opencv如何将黑白垫保存到透明PNG

时间:2014-06-19 08:48:56

标签: android opencv png transparent mat

您好我正在开发一个使用opencv库的Android应用程序。我有一个黑色和白色的垫子,里面只有0.0和255.0。我想将Mat转换为PNG文件,其中所有白色像素都是透明的 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以参考下面的c ++代码,

 Mat alpha = imread("a.jpg",0); //Load input image as gray scale will be alpha channel
 Mat b(alpha.size(),CV_8UC1,Scalar(0)); // create b channel
 Mat g(alpha.size(),CV_8UC1,Scalar(0)); // create g channel
 Mat r(alpha.size(),CV_8UC1,Scalar(0)); // create r channel

 Mat all_channel[4] = {b,g,r,alpha}; //create an array of mat, here the channel order is BGRA
 Mat dst; //ourput mat
 merge(all_channel,4,dst); // merge to get the final result
 imwrite("alp.png",dst); // write in png format to preserve alpha