一、官方下载地址,根据自己需要选择合适版本opencv版本发布地址
二、官方教程——图像入门教程
三、目标
1.读取照片
cv::imread (const string & filename,int flags = IMREAD_COLOR)
第一个参数指定文件路径,第二个参数可选以下三种,如果读取失败返回NULL。
- IMREAD_COLOR 以 BGR 8 位格式加载图像。这是此处使用的默认值。
- IMREAD_UNCHANGED 按原样加载图像(包括 alpha 通道,如果存在)。
- IMREAD_GRAYSCALE 将图像加载为灰度图。
- 更多在下面——官网解释
//! Imread flags
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
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.
};
2.使用opencv显示图像
cv::namedWindow(const string & winname,int flags = WINDOW_NORMAL);
创建一个窗口,名字和下面要显示的窗口名对应,目的是对窗口其调节作用。(imshow可以直接指定窗口名,可以省去此函数(默认调用),但如果显示图像之前需要其他窗口操作时,需要调用此函数)
- WINDOW_NORMAL 使可以调整窗口大小
- WINDOW_AUTOSIZE 会自动调整窗口大小
- WINDOW_FREERATIO 或 WINDOW_KEEPRATIO: WINDOW_FREERATIO 不考虑其比例调整图像,而 WINDOW_KEEPRATIO 保持图像比例。
- WINDOW_GUI_NORMAL 或 WINDOW_GUI_EXPANDED: WINDOW_GUI_NORMAL 是在没有状态栏和工具栏的情况下绘制窗口的旧方法
cv::imshow(const string & winname,InputArray mat)
第一个为显示窗口名字,第二个为mat对象。
- 如果图像是 8 位无符号,则按原样显示。
- 如果图像是 16 位无符号或 32 位整数,则将像素除以 256。即值范围 [0,255*256] 映射到 [0,255]。
- 如果图像是 32 位或 64 位浮点,则像素值乘以 255。即值范围 [0,1] 映射到 [0,255]。
3.保存图像
cv::imwrite(const string & filename,InputArray img,const std::vector< int > & params = std::vector<int>())
函数 imwrite 将图像保存到指定的文件中。图像格式是根据文件扩展名选择的,const std::vector& params表示为特定格式保存的参数编码,它有一个默认值std::vector< int >(),所以一般情况下不用写。
四、代码
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#define SAVE_KEY 115
#define PHOTO_PATH "../lena.png" //source photo path
#define PHOTO_SAVE "../lena1.png" //save photo path
using namespace cv;
int main()
{
//Get image
Mat img = imread(PHOTO_PATH, IMREAD_COLOR);
//check
if(img.empty())
{
std::cout << "Could not read the image: " << PHOTO_PATH << std::endl;
return 1;
}
//Creates a window
namedWindow("Display window",WINDOW_NORMAL);
//Show image
imshow("Display window", img);
int k = waitKey(0); // Wait for a keystroke in the window
if(k == SAVE_KEY)
{
//Write image
bool wr = imwrite(PHOTO_SAVE, img);
if(wr == true)
std::cout << "Write the image ok" << std::endl;
}else
{
std::cout << "Key is not s: " << k << std::endl;
}
return 0;
}
运行结果,有请lena