OpenCV图像读取(imread) 显示(imshow) 保存(imwrite)的冷知识点,虽然很基础,但也有用。
一、读取图像:imread() 与imreadmulti()
1. imread()函数第二个参数flags有很多选择,如下:
//! Imread flagsenum 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_COLOR 模式读取,会将图片转为3通道BGR彩图,读入进来type变成了CV_8UC3,如果你想以原本类型读取,那就选择IMREAD_UNCHANGED,那么图像原本是什么类型,读进来还是什么类型,这个参数主要在我们读取一些16位或者32为浮点型图像时就比较有用,因为一些计算需要这样的类型,有时候也为了计算提高精度。
2. imreadmulti()函数是用来一次性读取多张图片的,看下定义:
CV_EXPORTS_W bool imreadmulti(const String& filename, CV_OUT std::vector& mats, int flags = IMREAD_ANYCOLOR);
一个string类型的文件路径,输出是Mat类型的vector,也就是多张图像,比如这里我有一张tif格式的图片,它本来是2张图片组成的,那么我就可以一次性读进来,然后对vector进行处理,避免我对图片路径进行字符串的格式化。
二、显示图像:namedWindow() 和 imshow()
1. imshow()函数默认显示窗口模式是WINDOW_AUTOSIZE,它的好处是可以根据图像的大小自动调整大小显示,缺点是不能调整窗口大小,如果你想调整窗口大小,那么就要先使用namedWindow()函数,并将第二个参数设置为WINDOW_NORMAL
//! Flags for cv::namedWindowenum WindowFlags { WINDOW_NORMAL = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size. WINDOW_AUTOSIZE = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed. WINDOW_OPENGL = 0x00001000, //!< window with opengl support. WINDOW_FULLSCREEN = 1, //!< change the window to fullscreen. WINDOW_FREERATIO = 0x00000100, //!< the image expends as much as it can (no ratio constraint). WINDOW_KEEPRATIO = 0x00000000, //!< the ratio of the image is respected. WINDOW_GUI_EXPANDED=0x00000000, //!< status bar and tool bar WINDOW_GUI_NORMAL = 0x00000010, //!< old fashious way };
2. 另外一个冷门知识点是imshow显示的时候,我们可以选中窗口,进行图片的复制(Ctrl + C)和保存(Ctrl + S)
int main(){ Mat img = imread("./lena.jpg", IMREAD_UNCHANGED); if (img.empty()) { cout << "Image is empty, please check again!" << endl; return -1; } namedWindow("img", WINDOW_NORMAL); imshow("img", img); while (1) { if (waitKey(0) == 27) break; } return 0;}
可以用上一段代码尝试,先选中窗口,然后按下Ctrl + C,打开画图工具或者PPT进行粘贴,或者Ctrl + S会弹出对话框保存本地,很方便调,避免去加imwrite().
三、保存图像:imwrite()
1. 大家比较熟悉的应该是用imwrite()来保存单张图片,我们也可以用它来一次性保存多张图片到一个文件中,看函数说明:
下面是代码演示:
vectorimgs;Mat img1 = imread("./1.jpg", IMREAD_UNCHANGED);Mat img2 = imread("./2.jpg", IMREAD_UNCHANGED);imgs.push_back(img1);imgs.push_back(img2);imwrite("result.tif", imgs);
保存之后看不出来?那就用imreadmulti()函数来读取,代码如下:
vectorimgs;imreadmulti("./result.tif", imgs, IMREAD_UNCHANGED);cout << imgs.size() << endl;//多张图片可以使用for循环来读取imshow("1", imgs.at(0));imshow("2", imgs.at(1));waitKey(0);
.tif这个格式如果你用过halcon就会经常看到它的身影, 为什么老是用它?