使用opencv将jpg转png
opencv保存文件的时候是自动按照文件的后缀名来转换格式,所以使用opencv来做图片的格式转换是非常方便的。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/mman.h>
#include <assert.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat img = imread("in.jpg", IMREAD_UNCHANGED);
if (img.empty()) {
cout << "imread failed" << endl;
return -1;
}
if (imwrite("out.png", img)) {
cout << "imwrite success" << endl;
return 0;
} else {
cout << "imwrite failed" << endl;
return -1;
}
}
以上 保存的文件名字是 out.png 。
运行之后我们用 以下命令来确认是否转换成功:
ffprobe out.png
输入如下:
ffprobe version N-109162-gc0c0a5f106 Copyright (c) 2007-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.5.0-1ubuntu1~22.04)
configuration: --prefix=/home/h/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/home/h/ffmpeg_build/include --extra-ldflags=-L/home/h/ffmpeg_build/lib --extra-libs='-lpthread -lm' --ld=g++ --bindir=/home/h/bin --enable-gpl --enable-gnutls --enable-libx264 --enable-nonfree
libavutil 57. 43.100 / 57. 43.100
libavcodec 59. 54.100 / 59. 54.100
libavformat 59. 34.101 / 59. 34.101
libavdevice 59. 8.101 / 59. 8.101
libavfilter 8. 50.101 / 8. 50.101
libswscale 6. 8.112 / 6. 8.112
libswresample 4. 9.100 / 4. 9.100
libpostproc 56. 7.100 / 56. 7.100
Input #0, png_pipe, from 'out.png':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: png, rgb24(pc), 800x1067, 25 fps, 25 tbr, 25 tbn
最后一行显示是png 。
同时也可以转换为bmp,webp,tif等格式。
欢迎评论。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
二维码