刚刚接触OpenCV,仅仅作为复习笔记,很多是总结前辈们博客,如果有不妥,还请告知,一定及时处理。
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;int main()
{Mat src;src = imread("D:/demo01.jpg");if (src.empty()){cout << "could not load image..." << endl;return -1;}//namedWindos功能是创建一个OpenCV窗口,它是由OpenCV自动创建与释放,你无需取销毁它。//WINDOW_AUTOSIZE会自动根据图像大小,显示窗口大小,不能人为改变窗口大小namedWindow("opencv setup demo",CV_WINDOW_AUTOSIZE);//imshow根据窗口名称显示图像到指定的窗口上去,第一个参数是窗口名称,第二参数是Mat对象imshow("opencv setup demo",src);namedWindow("output windows",CV_WINDOW_AUTOSIZE);Mat output_image;//cvtColor的功能是把图像从一个彩色空间转换到另外一个色彩空间,有三个参数,//第一个参数表示源图像//第二参数表示色彩空间转换之后的图像//第三个参数表示源和目标色彩空间如:COLOR_BGR2HLS 、COLOR_BGR2GRAY 等//RGB和BGR颜色空间与HLS颜色空间之间的相互转换cvtColor(src,output_image,CV_BGR2HLS);imshow("output windows",output_image);//保存图像(cv::imwrite) 保存图像文件到指定目录路径//bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )//1、filename:需要写入的文件名,会自己创建(像imwrite("1.jpeg", src); 这样)//2、img:要保存的图像//3、params : 表示为特定格式保存的参数编码imwrite("hlstest.jpg",output_image);waitKey(0);return 0;
}

Mat类简单介绍

Mat类可以用来保存图像以及其他矩阵数据的数据结构,默认情况下其尺寸为0。更详细的将会在以后的文章中继续介绍。

读入图像:imread()函数

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

第一个参数:放入打算读入的图像的路径。

这里有三点要值得注意的地方,①要用” ”双引号(英文下)将路径括起来。②路径见要用两个斜线\\分割。③要记得图像格式别忘记写。

上图中,第一种写法,即图像在当前项目文件夹中;

第二张为:自定义图像的路径。

第二个参数:默认值是1,表示的是载入三通道彩色图像。以下给出的opencv里关于此参数另一些值及其意义。

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.
       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.
     };

显示图像:imshow()函数

void imshow(const String& winname, InputArray mat);
第一个参数:显示窗口名称。
第二个参数:将要显示的图像的名称。

保存图像:imwrite()函数

bool imwrite( const String& filename, InputArray img, const std::vector<int>& params = std::vector<int>());
第一个参数:将要另保存图像的名称,可以自己定义名称。
第二个参数:你要要保存的图像名称,是目前已经存在的图像。
第三个参数:来设置对于图像格式的参数,一般省略,不写。
生活中大多数看到的彩色图片都是RGB类型,但是在进行图像处理时,需要用到灰度图、HSV、HSI等颜色制式,opencv提供了cvtC
生活中大多数看到的彩色图片都是RGB类型,但是在进行图像处理时,需要用到灰度图、HSV、HSI等颜色制式,opencv提供了cvtColor()函数来实现这些功能。

cvtColor函数

此函数的作用是将一个图像从一个颜色空间转换到另一个颜色空间。

首先看一下cvtColor函数定义:

void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)

参数说明:

  • src: 输入图像;
  • dst: 输出图像;
  • code: 颜色空间转换标识符;

OpenCV2的CV_前缀宏命名规范被OpenCV3中的COLOR_式的宏命名前缀取代
                   注意RGB色彩空间在opencv中默认通道顺序为BGR!!!

  • dstCn: 目标图像的通道数,该参数为0时,目标图像根据源图像的通道数和具体操作自动决定;

需要说明的是在opencv2.x时颜色空间转换code用的宏定义是CV_前缀开头,而在opencv3.x版本其颜色空间转换code宏定义更改为COLOR_开头,而经验证,2.4.13版本中opencv同时支持这两种形式的写法。故下面表格会将两种code类型同时列出,以供参考:

这里列出的类型并不齐全,但是对于一般的图像处理已经够用。

1. 图像处理的一般过程

2. 图像的基本操作

3.图像的基本属性

1).empty() 判断文件读取是否正确
2).rows 获取图像行数(高度)
3).cols 获取图像列数(长度)
4).channels() 获取图像通道数
5).depth() 获取图像位深度

三 知识点详解

opencv的标配头文件为

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

可以使用

#include "opencv2/opencv.hpp"

这一行代码进行代替

原因: 
在D:\InstallOpencv\opencvBinary\include\opencv2\core.cpp中 
定义了如下代码

#include "opencv2/core/cvdef.h"
#include "opencv2/core/version.hpp"
#include "opencv2/core/base.hpp"
#include "opencv2/core/cvstd.hpp"
#include "opencv2/core/traits.hpp"
#include "opencv2/core/matx.hpp"
#include "opencv2/core/types.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv2/core/persistence.hpp"

2.命名空间 
opencv中的C++类和函数都是定义在命名空间cv之内的,所以有如下opencv函数的两种调用方法:

ex1:
Mat srcImg = cv::imread("1.jpg");

或者

ex2:
using namespace cv;
Mat srcImg = imread("1.jpg");

注:一般推荐使用

using namespace cv;

因而简单的opencv程序头文件为下列代码

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;

3.imread函数

Mat srcImg = imread("1.png");

imread函数原型

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

1)第一个参数,需要填入图片路径名,支持格式: 
Windows位图: .bmp, .dib 
JPEG文件: .jpeg, .jpg, *.jpe 
JPEG2000文件: *.jp2 
PNG图片: *.png 
便携文件格式: .pbm, .pgm, *.ppm 
Sun rasters光栅文件: .sr, .ras 
TIFF文件: .tiff, .tif 
2)第二个参数:指定加载图像的颜色类型,默认为1 
图像类型如下

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.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.};

4. namedwindow 
namedwindow函数原型

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

1)第一个参数 
设置窗口名称 
2)第二个参数 
设置窗口的显示类型

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.};

5. imshow函数 
函数原型

void imshow(const String& winname, const ogl::Texture2D& tex);

1)第一个参数,设置需要显示的窗口名称 
2)第二个参数,填写需要显示的图像

6. imwrite函数

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

1)第一个参数,设置保存的文件名,需填写后缀,如”1.bmp” 
2)第二个参数,要保存的Mat类型图像数据 
3)第三个参数,表示特定格式保存的参数编码,一般采用默认值不填写 
7. waitKey()函数

/** @brief Waits for a pressed key.The function waitKey waits for a key event infinitely (when \f$\texttt{delay}\leq 0\f$ ) or for delay
milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the
function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is
running on your computer at that time. It returns the code of the pressed key or -1 if no key was
pressed before the specified time had elapsed.@noteThis function is the only method in HighGUI that can fetch and handle events, so it needs to be
called periodically for normal event processing unless HighGUI is used within an environment that
takes care of event processing.@noteThe function only works if there is at least one HighGUI window created and the window is active.
If there are several HighGUI windows, any of them can be active.@param delay Delay in milliseconds. 0 is the special value that means "forever".*/
CV_EXPORTS_W int waitKey(int delay = 0);

1)参数,如果delay>0, 表示等待delay毫秒之后结束 
如果delay=0, 表示无限等待,直到有按键按下结束

2)返回值为对应按下按键的ASCII码值,如Esc的ASCII码为27

OpenCV学习01-加载、修改、保存图像相关推荐

  1. 使用Hibernate加载或保存图像-MySQL

    本教程将引导您逐步了解如何使用Hibernate从数据库( MySQL )保存和加载图像. 要求 对于此示例项目,我们将使用: Eclipse IDE (您可以使用自己喜欢的IDE): MySQL ( ...

  2. Euresys eVision 加载和保存图像

    Euresys eVision  EImageBW8类的Load方法加载上来的图像像素值是按照列存储的,而自己创建EImgBW8类的Save方法保存图像时要求像素值是按照行顺序存储的.正确的加载和保存 ...

  3. c++版本opencv(02-第一个OpenCV程序 03.图像加载与保存)

    c++版本opencv(02-第一个OpenCV程序) 一.02-第一个OpenCV程序 二,03.图像加载与保存 来自网易云课堂 一.02-第一个OpenCV程序 如果加载到了之后呢,我们就要对它进 ...

  4. PyTorch框架学习十九——模型加载与保存

    PyTorch框架学习十九--模型加载与保存 一.序列化与反序列化 二.PyTorch中的序列化与反序列化 1.torch.save 2.torch.load 三.模型的保存 1.方法一:保存整个Mo ...

  5. 《ArcGIS Engine开发 从入门到精通》学习笔记1 地图的加载与保存。

    照着书上敲完了两个主要函数的代码,先mark一下,以备下次偷懒. using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Carto; private void ...

  6. Objective-C学习笔记第十五章文件加载与保存

    第十五章文件加载与保存 Cocoa提供了Core Data,他能在后台处理所有文件内容 Cocoa提供了两个通用的文件处理类:属性列表和对象编码 一.属性列表类 在Cocoa中,有一类名为属性列表的对 ...

  7. imgaug数据增强神器:第一章 加载和增强图像

    官网:https://imgaug.readthedocs.io/en/latest/ 教程:https://nbviewer.jupyter.org/github/aleju/imgaug-doc/ ...

  8. 【youcans 的 OpenCV 学习课】7. 空间域图像滤波

    专栏地址:『youcans 的图像处理学习课』 文章目录:『youcans 的图像处理学习课 - 总目录』 [youcans 的 OpenCV 学习课]7. 空间域图像滤波 图像滤波是在尽可能保留图像 ...

  9. ROS之rviz文件的加载和保存

    最近将ORB-SLAM3中的pangolin可视化功能给搞掉了,换成了基于rviz的可视化显示,摆脱程度对pangolin库的依赖,为了方便每次在启动时程序自动打开rviz可视化界面,需要在第一次运行 ...

  10. LEADTOOLS如何加载,保存和拆分注释

    LEADTOOLS Recognition Imaging SDK是精选的LEADTOOLS SDK功能集,旨在在企业级文档自动化解决方案中构建端到端文档成像应用程序,这些解决方案需要OCR,MICR ...

最新文章

  1. linux中pipe
  2. mysql数据库之linux版本
  3. 图像色彩空间与应用转换
  4. 太形象了!本科、硕士、博士,有什么本质区别?
  5. IR2104s半桥驱动使用经验
  6. captcha.js一个生成验证码的插件,使用js和canvas生成
  7. react-router-dom v6.1.1 使用方式
  8. 中专计算机总结论文,中专计算机毕业论文...doc
  9. php 图片水印删除,PHP图片水印
  10. 开源的SNMP网管系统LibreNMS
  11. python爬取五百丁word模板(有图+有代码)
  12. word替换向下箭头符号
  13. 计算机中文编码《区位码\国标码\机内码》进阶史
  14. 华盛顿大学计算机专业咋样,华盛顿大学计算机专业详解
  15. Unity3D-----摄像机镜头移动并限制角度
  16. 中级Shader教程17 海洋渲染
  17. verilog学习笔记:简单的数据选择器modelsim仿真
  18. 劣质VGA线导致不支持非标准分辨率显示
  19. 【使用C++开发MCU】05-CAN实例之NXP S32K1 FlexCAN模块
  20. libreCAD使用

热门文章

  1. SQL注入-04-(最后有实战教学)关系注入逻辑注入
  2. react之通俗易懂配置less
  3. MOGRE学习笔记(2) - MOGRE基础知识总结
  4. mysql交并补_集合交并补运算顺序是什么?
  5. C语言程序计算自己活了多少天
  6. 不是忽悠?国产16nm八核处理器来了
  7. 通过phpmyadmin修改帝国CMS的管理员密码
  8. node重绘图片_使用nodejs生成图片的尝试
  9. 密度测量:1.密度测量的基础知识
  10. 本科阶段学习经验分享(未整理)