opencv-生成一幅png图像并写入到当前文件夹下

#include “pch.h”
#include
#include <opencv2/opencv.hpp>
#include
using namespace cv;
using namespace std;

void createAlphaMat(Mat &mat)
{
for (int i = 0; i < mat.rows; ++i)
{
for (int j = 0; j < mat.cols; ++j)
{
Vec4b&rgba = mat.at(i, j);
rgba[0] = UCHAR_MAX;
rgba[1] = saturate_cast((float(mat.cols - j)) / ((float)mat.cols)*UCHAR_MAX);
rgba[2] = saturate_cast((float(mat.rows - i)) / ((float)mat.rows)UCHAR_MAX);
rgba[3] = saturate_cast(0.5
(rgba[1] + rgba[2]));
}
}
}

int main()
{
Mat mat(480, 640, CV_8UC4);
createAlphaMat(mat);
vectorcompression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
try
{
imwrite(“透明Alpha图.png”, mat, compression_params);
imshow(“生成的png图”, mat);
fprintf(stdout, “png图片文件的Alpha数据保存完毕\n可以在工程目录下查看由imwrite函数生成的图片\n”);
waitKey(0);
}
catch (runtime_error&ex)
{
fprintf(stderr, “图像转换成PNG格式发生错误:%s\n”, ex.what());
return 1;
}
return 0;
}
在这里插入图片描述
图片在文件夹下的位置

猜你喜欢

转载自blog.csdn.net/weixin_44270056/article/details/86629932