您好我正在开发一个使用 opencv 库的 Android 应用程序。我有一个黑白垫,里面只有 0.0 和 255.0。我想将该 Mat 转换为所有白色像素都是透明的 PNG 文件。
我怎样才能做到这一点?
最佳答案
你可以引用下面的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
关于Android - Opencv如何将黑白垫保存为透明PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302455/