天天看点

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

imread(),imshow(),nameWindow(),waitKey()的基本用法

imread(const string& filename, int flags = 1)

imread()为读取图片函数。

第一个参数:为图片文件路径

第二个参数为加载类型。第二个参数,可控制不同加载方式。第二个参数默认是1,其含义为按照RGB格式加载图片。一般而言,默认加载即可。

以下是opencv定义第二个参数数值的含义。

IMREAD_UNCHANGED = -, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = ,  //!< If set, always convert image to the single channel grayscale image.
 IMREAD_COLOR= ,  //!< If set, always convert image to the  channel BGR color image.
IMREAD_ANYDEPTH= ,  //!< If set, return -bit/-bit image when the input has the corresponding depth, otherwise convert it to -bit.
IMREAD_ANYCOLOR= ,  //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL= ,  //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_2= , //!< If set, always convert image to the  channel BGR color image and the image size reduced /
IMREAD_REDUCED_GRAYSCALE_4  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_4= , //!< If set, always convert image to the  channel BGR color image and the image size reduced /
IMREAD_REDUCED_GRAYSCALE_8  = , //!< If set, always convert image to the single channel grayscale image and the image size reduced /
IMREAD_REDUCED_COLOR_8      =   //!< If set, always convert image to the  channel BGR color image and the image size reduced /
           

在绝大部分情况,我们使用imread()时只需填写第一个参数,让opencv默认按照RGB格式加载图片,因为opencv很多库函数都是基于RGB存储空间对图片进行处理的。

nameWindow(const string& windowName, int flags = 1)

nameWindow()是创建显示窗口函数,该函数可实现自动创建和释放句柄,使用完成无需人工释放。

第一个参数:创建窗口的名称

第二个参数:窗口的显示方式。常见的方式有全屏显示、图片大小显示和自适应大小显示等。默认情况下是CV_WINDOW_AUTOSIZE,即flags=1。

CV_WINDOW_AUTOSIZE    自适应大小显示
CV_WINDOW_FULLSCREEN  全屏显示
CV_WINDOW_NORMAL      通常显示

至于其他的,可自行在opencv函数定义里寻找
           

imshow(const string& winname, InputArray mat);

该函数为在指定的窗口内显示制定的图片。

第一个参数:为窗口名称,窗口名称须在nameWindow创建过。

第二个参数:为图片InputArray 类型的Mat。

waitKey(int delay = 0)

这个函数是在一个给定的时间内(单位ms)等待用户按键触发;如果用户没有按下 键,则接续等待(循环)

delay参数为延时时间,单位是ms。waitKey()函数必须要调用,否则图片不会显示。

waitKey();   //代表不会自动关闭窗口,直到键盘按键按下才退出。
waitKey();  //代表50ms后自动关闭窗口,或者按键按下关闭窗口
           

代码测试【默认加载方式】

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
int main(int argc, char** argv) {
    Mat src = imread("G:\\2.jpg");
    namedWindow("test", CV_WINDOW_NORMAL);
    imshow("test", src);
    waitKey();
    return ;
}
           

运行效果:

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

代码测试【imread()里灰度图加载】

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
int main(int argc, char** argv) {
    Mat src = imread("G:\\2.jpg",IMREAD_GRAYSCALE);
    namedWindow("test", CV_WINDOW_NORMAL);
    imshow("test", src);
    waitKey();
    return ; 
}
           

运行效果:

imread(),imshow(),nameWindow(),waitKey()的基本用法imread(),imshow(),nameWindow(),waitKey()的基本用法

总结

在opencv中显示图片,一般需要使用到三个函数。

1.imread()

2.nameWindow()

3.imshow()

4.waitKey()

即先加载图片,再创建显示窗口,然后制定窗口显示图片,最后调用

最后,祝大家实验成功。