opencv输入显示图像imread与imshow

imread()

imread()的原型为


1
2
3
4
5
Mat cv::imread	(	const String & 	filename,

int flags = IMREAD_COLOR

)

  • const String & filename
    输入图片的文件路径名,带扩展名的。opencv支持输入的图片格式有

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    - Windows bitmaps - *.bmp, *.dib (always supported)
    JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
    JPEG 2000 files - *.jp2 (see the Notes section)
    Portable Network Graphics - *.png (see the Notes section)
    WebP - *.webp (see the Notes section)
    Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
    Sun rasters - *.sr, *.ras (always supported)
    TIFF files - *.tiff, *.tif (see the Notes section)
    OpenEXR Image files - *.exr (see the Notes section)
    Radiance HDR - *.hdr, *.pic (always supported)
    Raster and Vector geospatial data supported by Gdal (see the Notes section)
  • int flags = IMREAD_COLOR

读取图片的方式,默认是IMREAD_COLOR,彩色输入。还有其他的输入方式。如

1
2
3
4
5
6
7
8
9
10
11
    IMREAD_UNCHANGED 

IMREAD_ANYDEPTH

IMREAD_GRAYSCALE

IMREAD_ANYDEPTH

IMREAD_ANYCOLOR

...
  • 返回值为Mat类型

imshow()

imshow()的原型为:


1
2
3
4
5
void cv::imshow	(	const String & 	winname,

InputArray mat

)

  • 第一个参数为const string& 类型的 winname,显示图像的窗口名称。传入值为窗口名称,窗口不存在则会自动创建。
  • 第二个参数为mat类型输入图片
  • 函数无返回值

示例代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"

#include<iostream>

#include<opencv2/opencv.hpp>

using namespace cv;

using namespace std;

int main()

{

//read the image

Mat image = imread("./clock.jpg");

if (image.data != NULL)

{

//show the image

imshow("clock", image);

waitKey(0);

}

else

{

cout << "can&apos;t openc the file!" << endl;

getchar();

}

return 0;

}

  • waitkey(0), waitkey(n)为等待n毫秒结束显示, n若为0则表示按下任意键结束显示。
-------------本文结束感谢您的阅读-------------