简体   繁体   中英

Android - Opencv How to save black&white Mat to transparent PNG

Hi I am developing an Android application which uses opencv library. I have an black&white Mat that includes only 0.0 and 255.0 inside. I want to convert that Mat to PNG file where all of white pixels are transparent.
How can i do this?

You can refer below c++ code,

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM