【OpenCV学习笔记之二】图像的载入、显示及输出

一、opencv命名空间众所周知的是C++有一个最常用的命名空间std,常见的输入输出和回车cin、cout和endl都依赖于此。opencv中的类和函数都是定义在命名空间cv之内的,跟C++一样有两种方式可以访问。第一种是在代码的开头加上using namespace cv,另一种是在使用opencv类个函数时加上cv::,比如cv::imread()。不过第二种方式每使用一个函数都需要打出命名空间,相对麻烦。所以比较第一种方式。所以我一般在写程序时,都会加上#include <iostre

一、opencv命名空间

众所周知的是C++有一个最常用的命名空间std,常见的输入输出和回车cin、cout和endl都依赖于此。

opencv中的类和函数都是定义在命名空间cv之内的,跟C++一样有两种方式可以访问。第一种是在代码的开头加上using namespace cv,另一种是在使用opencv类个函数时加上cv::,比如cv::imread()。不过第二种方式每使用一个函数都需要打出命名空间,相对麻烦。所以比较第一种方式。

所以我一般在写程序时,都会加上

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

二、Mat数据类型

cv::Mat类是用于保存图像以及其他矩阵数据的数据结构。默认情况下,其尺寸为0,我们也可以指定初始尺寸,比如,比如定义一个Mat类对象,就要写cv::Mat pic(320,640,cv::Scalar(100));

MAT几个基本数据类型:

Mat();
Mat(int rows, int cols, int type);
Mat(Size size, int type);
Mat(Size size, int type, const Scalar& s);
Mat(int rows, int cols, int type, const Scalar& s);

rows:行
cols:列
size:参考二维数组size(cols, rows)的大小创建构造函数,和的行数
列数按照相反的顺序。
type:CV_8UC1、CV_64FC4
Scalar: Scalar是一个4通道数组,一般用来给Mat对象赋值,第四个通道的值都默认为0。


typedef struct Scalar
{
    double val[4];
}Scalar;

Scalar常用的使用场景:Mat M(7,7,CV_32FC2,Scalar(1,3));

上面的代码表示:创建一个2通道,且每个通道的值都为(1,3),深度为32,7行7列的图像矩阵。CV_32F表示每个元素的值的类型为32位浮点数,C2表示通道数为2,Scalar(1,3)表示对矩阵每个元素都赋值为(1,3),第一个通道中的值都是1,第二个通道中的值都是3.

三、图像的载入和显示

1.imread函数

	Mat imread( const String& filename, int flags = IMREAD_COLOR );

参数1 filename,读取的图片文件名,可以使用相对路径或者绝对路径,但必须带完整的文件扩展名(图片格式后缀)
参数2 flags,一个读取标记,用于选择读取图片的方式,默认值为IMREAD_COLOR,flag值的设定与用什么颜色格式读取图片有关

enum ImreadModes {
       IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
       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.
     };

【OpenCV学习笔记之二】图像的载入、显示及输出

  1. flags >0返回一个3通道的彩色图像
  2. flags =0返回灰度图像。
  3. flags <0返回包含Alpha通道的加载的图像。

imread几个示例:

	Mat image0 = imread("dota.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);//载入最真实的图像
	Mat image1 = imread("dota.jpg",0);//载入灰度图
	Mat image2 = imread("dota.jpg",199);//载入3通道的彩色图像
	Mat logo = imread("dota_logo.jpg");//载入3通道的彩色图像

2. namedWindow函数

namedWindow函数,用于创建一个窗口:

void namedWindow(const string& winname,int flags=WINDOW_AUTOSIZE );

参数1 winname,创建窗口的名称
参数2 flags, int 类型的flags ,窗口的标识,可以填一下的值:

  • WINDOW_NORMAL设置了这个值,用户便可以改变窗口的大小(没有限制)
  • WINDOW_AUTOSIZE如果设置了这个值,窗口大小会自动调整以适应所显示的图像,并且不能手动改变窗口大小。
  • WINDOW_OPENGL 如果设置了这个值的话,窗口创建的时候便会支持OpenGL。
    flags不填时默认是WINDOW_AUTOSIZE。

示例:

namedWindow("图像混合窗口");

3. imshow函数

在指定的窗口里显示一副图像。

void imshow(const string& winname, InputArray mat);

参数1 winname, 需要显示窗口的标志名称
参数2 mat, 需要显示的图像

四、输出图像到文件

imwrite函数原型:

	bool imwrite(const string& filename,InputArray img, const vector<int>& params=vector<int>() );

参数1 filename, 需要写入的文件名,比如”xxx.mp4“。
参数2 img,一般为Mat类型的图像数据
参数3 params, 特定格式的参数编码,它有默认值vector(),一般情况不需要填写。

  • 对于JPEG格式的图片,这个参数表示从0到100的图片质量(CV_IMWRITE_JPEG_QUALITY),默认值是95.
  • 对于PNG格式的图片,这个参数表示压缩级别(CV_IMWRITE_PNG_COMPRESSION)从0到9。较高的值意味着更小的尺寸和更长的压缩时间,而默认值是3。
  • 对于PPM,PGM,或PBM格式的图片,这个参数表示一个二进制格式标志(CV_IMWRITE_PXM_BINARY),取值为0或1,而默认值是1

五、代码示例:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
	/*1. 读取图片*/
	Mat image = imread("angel.jpg");
	if (!image.data) {
		cout << "open angel.jpg fail" << endl;
		return -1;
	}

	/*2. 创建显示窗口*/
	namedWindow("example");
	
	/*3. 显示图片*/
	imshow("example", image);

	/*4. 保存图片*/
	imwrite("test.jpg", image);
	
	waitKey(0);

	return 0;
}

执行效果如下:
【OpenCV学习笔记之二】图像的载入、显示及输出

最后对你有用的话,希望可以点赞、收藏、评论三连,谢谢。

借鉴:毛星云【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析

版权声明:本文为博主生命如歌,代码如诗原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/ma950924/article/details/122570358

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年1月19日 下午9:38
下一篇 2022年1月19日 下午10:13

相关推荐