文章目录
1 包含API
读取图像 imread
显示图像 imshow
2. 读取图像 imread
imread
第一个参数是文件地址,第二个参数是显示模式,默认是IMREAD_COLOR模式
修改该参数
//! 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.
};
如果有透明通道,使用IMREAD_UNCHANGED
3. 显示图像 imshow
input
src读进来的是是8位通道的,位图和深度是3 * 8 = 24,API里用枚举显示图片的深度
src.depth(); 这个值调用出来是1,是正常的,枚举中24的值等于1
4 阻塞函数 waitKey
waitKey(0); 阻塞等待
waitKey(1);停顿1毫秒
5 关闭之前创建的所有窗口 destroyAllWIndows
6 创建一个窗口 nameWindow
namedWindow(“inputwindow”,WINDOW_FREERATIO);
imshow(“inputwindow”, src);
和imshow配合,让imshow图片显示在inputwindow中
WINDOW_FREERATIO并且图片能自动适应窗口大小
7 代码
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("G:/test/NoRecord.png",IMREAD_UNCHANGED);
if (src.empty())
{
print("could not load img");
return -1;
}
namedWindow("inputwindow",WINDOW_FREERATIO);
imshow("inputwindow", src);
waitKey(0);
destroyAllWindows();
return 0;
}