概述
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);