2.5BGRA图像保存为PNG格式文件

目录

1.实验原理

2.实验代码

3.运行结果


1.实验原理

函数imwrite用来输出图像到文件

cpp 复制代码
bool imwrite(const string& filename, InputArray image, const vector<int>& params=vector<int>());


参数说明如下:
filename:要保存的文件的名称。
image:要保存的图像。
params:格式特定的保存参数,以参数ID和参数值的配对形式传递,即 paramId_1, paramValue_1, paramId_2, paramValue_2, ...。目前支持的参数如下:
格式特定的保存参数
JPEG
CV_IMWRITE_JPEG_QUALITY:JPEG 图像的质量等级,范围为 0 到 100(数值越高,质量越好)。默认值为 95。
PNG
CV_IMWRITE_PNG_COMPRESSION:PNG 图像的压缩级别,范围为 0 到 9。数值越高意味着更小的文件大小和更长的压缩时间。默认值为 3。
PPM, PGM, 或 PBM
CV_IMWRITE_PXM_BINARY:是否使用二进制格式保存 PPM, PGM, 或 PBM 文件。可以是 0 或 1,其中 1 表示使用二进制格式。默认值为 1。

2.实验代码

//此项目未跑通!!!

cpp 复制代码
#include <iostream>
#include <opencv2\opencv.hpp>
// comment(lib, "opencv_world450d.lib")  //引用引入库 
using namespace cv;
using namespace std;
static void createAlphaMat(Mat &mat)
{
	CV_Assert(mat.channels() == 4);
	for (int i = 0; i < mat.rows; ++i)
	{
		for (int j = 0; j < mat.cols; ++j)
		{
			Vec4b& bgra = mat.at<Vec4b>(i, j);
			bgra[0] = UCHAR_MAX; // Blue
			bgra[1] = saturate_cast<uchar>((float(mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
			bgra[2] = saturate_cast<uchar>((float(mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
			bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
		}
	}
}
int main()
{
	// Create mat with alpha channel
	//Mat mat(480, 640, CV_8UC4);
	Mat mat = imread("020.png");
	createAlphaMat(mat);
	vector<int> compression_params;
	compression_params.push_back(IMWRITE_PNG_COMPRESSION);
	compression_params.push_back(9);
	bool result = false;
	try
	{
		result = imwrite("alpha.png", mat, compression_params);
	}
	catch (const cv::Exception& ex)
	{
		fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
	}
	if (result)
		printf("Saved PNG file with alpha data.\n");
	else
		printf("ERROR: Can't save PNG file.\n");
	return result ? 0 : 1;
}

下面是一个使用这些参数的例子,展示如何保存一张 JPEG 图像,并设置 JPEG 的质量为 80:

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

int main()
{
    cv::Mat image = cv::imread("input.jpg");//牛雪丽

    if (image.empty()) 
    {
        std::cerr << "Could not read the image" << std::endl;
        return -1;
    }

    std::vector<int> params;
    params.push_back(cv::IMWRITE_JPEG_QUALITY);
    params.push_back(80); // 设置 JPEG 质量为 80

    cv::imwrite("output.jpg", image, params);

    return 0;
}

3.运行结果

相关推荐
阿蒙Amon20 小时前
【Python小工具】使用 OpenCV 获取视频时长的详细指南
python·opencv·音视频
慕婉030721 小时前
OpenCV图像边缘检测
人工智能·opencv·计算机视觉
jndingxin1 天前
OpenCV中超分辨率(Super Resolution)模块类cv::dnn_superres::DnnSuperResImpl
人工智能·opencv·dnn
Edward-tan1 天前
基于 opencv+yolov8+easyocr的车牌追踪识别
python·opencv·ocr·yolov8
jndingxin2 天前
OpenCV CUDA模块设备层-----反向二值化阈值处理函数thresh_binary_inv_func()
人工智能·opencv·计算机视觉
jndingxin2 天前
OpenCV CUDA模块设备层-----在 GPU 上执行类似于 std::copy 的操作函数warpCopy()
人工智能·opencv·计算机视觉
晓13132 天前
OpenCV篇——项目(二)OCR文档扫描
人工智能·python·opencv·pycharm·ocr
jndingxin2 天前
OpenCV CUDA模块设备层-----在GPU 上高效地执行两个 uint 类型值的最大值比较函数vmax2()
人工智能·opencv·计算机视觉
luofeiju2 天前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
Echo``12 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉