概述
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);
本文介绍如何使用OpenCV处理PNG图像的透明通道,实现指定颜色变为透明的功能。通过代码示例,详细展示了如何将特定颜色的像素设置为透明,适用于图像处理和计算机视觉项目。
1060





