opencv读取和保存图像
【欢迎关注编码小哥,学习更多实用的编程方法和技巧】
OpenCv源码地址:https://github.com/opencv/opencv
imread函数原型
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes//CV_EXPORTS_W Mat imread(const String& filename, int flags = IMREAD_COLOR);
imwrite函数原型
@param filename Name of the file.
@param img(Mat or vector of Mat) Image or Images to be saved.
@param params Format - specific parameters encoded as pairs(paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlagsCV_EXPORTS_W bool imwrite(const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>());
//imread 第二个参数flags取值范围以及作用
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};
//imwrite 函数第二个参数flags的取值范围以及作用
enum ImwriteFlags {
IMWRITE_JPEG_QUALITY = 1, //!< For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95.
IMWRITE_JPEG_PROGRESSIVE = 2, //!< Enable JPEG features, 0 or 1, default is False.
IMWRITE_JPEG_OPTIMIZE = 3, //!< Enable JPEG features, 0 or 1, default is False.
IMWRITE_JPEG_RST_INTERVAL = 4, //!< JPEG restart interval, 0 - 65535, default is 0 - no restart.
IMWRITE_JPEG_LUMA_QUALITY = 5, //!< Separate luma quality level, 0 - 100, default is -1 - don't use.
IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is -1 - don't use.
IMWRITE_JPEG_SAMPLING_FACTOR = 7, //!< For JPEG, set sampling factor. See cv::ImwriteJPEGSamplingFactorParams.
IMWRITE_PNG_COMPRESSION = 16, //!< For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting).
IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.
IMWRITE_PNG_BILEVEL = 18, //!< Binary level PNG, 0 or 1, default is 0.
IMWRITE_PXM_BINARY = 32, //!< For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1.
IMWRITE_EXR_TYPE = (3 << 4) + 0, /* 48 */ //!< override EXR storage type (FLOAT (FP32) is default)
IMWRITE_EXR_COMPRESSION = (3 << 4) + 1, /* 49 */ //!< override EXR compression type (ZIP_COMPRESSION = 3 is default)
IMWRITE_EXR_DWA_COMPRESSION_LEVEL = (3 << 4) + 2, /* 50 */ //!< override EXR DWA compression level (45 is default)
IMWRITE_WEBP_QUALITY = 64, //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
IMWRITE_HDR_COMPRESSION = (5 << 4) + 0, /* 80 */ //!< specify HDR compression
IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format
IMWRITE_TIFF_RESUNIT = 256,//!< For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values
IMWRITE_TIFF_XDPI = 257,//!< For TIFF, use to specify the X direction DPI
IMWRITE_TIFF_YDPI = 258,//!< For TIFF, use to specify the Y direction DPI
IMWRITE_TIFF_COMPRESSION = 259,//!< For TIFF, use to specify the image compression scheme. See libtiff for integer constants corresponding to compression formats. Note, for images whose depth is CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified by this flag; LZW compression is the default.
IMWRITE_JPEG2000_COMPRESSION_X1000 = 272,//!< For JPEG2000, use to specify the target compression rate (multiplied by 1000). The value can be from 0 to 1000. Default is 1000.
IMWRITE_AVIF_QUALITY = 512,//!< For AVIF, it can be a quality between 0 and 100 (the higher the better). Default is 95.
IMWRITE_AVIF_DEPTH = 513,//!< For AVIF, it can be 8, 10 or 12. If >8, it is stored/read as CV_32F. Default is 8.
IMWRITE_AVIF_SPEED = 514 //!< For AVIF, it is between 0 (slowest) and (fastest). Default is 9.
};
//两种方法读取图像返回图像数据,默认情况下,通过imread读取的图像是BGR三通道彩色图像。
//方法一:
#if 0
#include<opencv2/opencv.hpp>
int main(int argc,char **argv)
{
printf("CV_VERSION:%s", CV_VERSION); //打印OpenCv版本
cv::Mat img = cv::imread("D:/opencv/images/lena.jpg");//读取图像并返回图像数据,返回的图像数据是Mat对象。
if (img.empty())
{
printf("could not load image");
return -1;
}
cv::imshow("test 1",img); //显示图像,第一个参数是窗口名称,第二个参数是图像数据。
cv::waitKey(1000); //参为0时,表示显示的图像窗口一直存在,当用户按任意键结束;参数>0时,表示图像窗口显示的时间(ms)。
cv::destroyAllWindows(); //销毁创建的窗口。
return 0;
}
#endif
//方法二:
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
string img_path = "D:/opencv/images/";
Mat img = imread(img_path+"lena.jpg");
//namedWindow("test 2", WINDOW_AUTOSIZE);
imshow("test 2",img);
waitKey(0);
destroyAllWindows();
return 0;
}
#endif
//设置属性读取图像
//设置灰度属性
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
string img_path = "D:/opencv/images/";
Mat img = imread(img_path + "lena.jpg",IMREAD_GRAYSCALE);
//namedWindow("input img", WINDOW_AUTOSIZE);
imshow("test 3", img);
waitKey(0);
destroyAllWindows();
return 0;
}
#endif
//设置不改变属性
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
string img_path = "D:/opencv/images/";
Mat img = imread(img_path + "lena.jpg", IMREAD_UNCHANGED);
//namedWindow("input img", WINDOW_AUTOSIZE);
imshow("test 4", img);
waitKey(0);
destroyAllWindows();
return 0;
}
#endif
//设置属性将图像变为原图像的1/2大小
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
string img_path = "D:/opencv/images/";
Mat img = imread(img_path + "lena.jpg", IMREAD_REDUCED_COLOR_2);
//namedWindow("input img", WINDOW_AUTOSIZE);
imshow("test 5", img);
waitKey(0);
destroyAllWindows();
return 0;
}
#endif
#if 1
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
string img_path = "D:/opencv/images/";
Mat img = imread(img_path + "lena.jpg", IMREAD_COLOR);
//vector<int> set;
//保存为png格式
imwrite("E:/result/lena.png", img);
//imwrite("E:/result/lena.png", img,set);
return 0;
}
#endif