概述
PNG格式的图像是有透明通道,用PS打开的时候,透明的地方是没有像素的,下面在ps里面显示黑色背景与透明背景的效果。
黑色背景:
白色背景:
透明背景:
把指定颜色变成透明
代码:
void toPng(cv::Mat &src, cv::Mat &dst, int mark)
{
cv::Mat cv_input = src.clone();
if (cv_input.channels() != 4)
{
cv::cvtColor(cv_input, dst, CV_BGR2BGRA);
}
else
{
return;
}
for (int y = 0; y < dst.rows; ++y)
{
for (int x = 0; x < dst.cols; ++x)
{
cv::Vec4b & pixel = dst.at<cv::Vec4b>(y, x);
if (pixel[0] == mark && pixel[1] == mark && pixel[2] == mark)
{
pixel[3] = 0;
}
}
}
}
调用方式:
toPng(src, png, 0);