我是靠谱客的博主 甜美猎豹,这篇文章主要介绍opencv4.5.1自学(1)——图像输入输出,现在分享给大家,希望可以做个参考。

1、命名空间

opencv的命名空间为cv,using namespace cv;

2、Mat类型

opencv中的Mat类用于保存图像以及其他矩阵数据。
比如输入一张图片,就用以下代码

复制代码
1
2
Mat myMat = imread("myimage.jpg");

3、图像载入和显示

图像载入函数:

复制代码
1
2
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

第一个参数为图片路径名,第二个参数取值如下

复制代码
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
enum ImreadModes { IMREAD_UNCHANGED = -1, //Ignore EXIF orientation. IMREAD_GRAYSCALE = 0, //the single channel grayscale image IMREAD_COLOR = 1, //the 3 channel BGR color image. IMREAD_ANYDEPTH = 2, //the corresponding depth, otherwise convert it to 8-bit. IMREAD_ANYCOLOR = 4, //any possible color format. IMREAD_LOAD_GDAL = 8, //use the gdal driver for loading the image. IMREAD_REDUCED_GRAYSCALE_2 = 16, //always convert image to the single channel grayscale image and the image size reduced 1/2. IMREAD_REDUCED_COLOR_2 = 17, //always convert image to the 3 channel BGR color image and the image size reduced 1/2. IMREAD_REDUCED_GRAYSCALE_4 = 32, //always convert image to the single channel grayscale image and the image size reduced 1/4. IMREAD_REDUCED_COLOR_4 = 33, //always convert image to the 3 channel BGR color image and the image size reduced 1/4. IMREAD_REDUCED_GRAYSCALE_8 = 64, //always convert image to the single channel grayscale image and the image size reduced 1/8. IMREAD_REDUCED_COLOR_8 = 65, //always convert image to the 3 channel BGR color image and the image size reduced 1/8. IMREAD_IGNORE_ORIENTATION = 128 //do not rotate the image according to EXIF's orientation flag. };

创建窗口

复制代码
1
2
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);

第一个参数为窗口名称,第二个参数取值如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum 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 };

图像显示函数:

复制代码
1
2
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);

第一个参数为窗口名称,第二个参数为要显示的图像

4、图像输出——imwrite

复制代码
1
2
CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>());

第一个参数为文件名,第二个参数为Mat类型图像数据,第三个参数表示为特定格式保存的参数编码

最后

以上就是甜美猎豹最近收集整理的关于opencv4.5.1自学(1)——图像输入输出的全部内容,更多相关opencv4内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部