OpenCV学习笔记(一)图像编程入门

//题外话:按住Enter键执行的是段落换行,同时按住Enter键和Shift键可以实现逐句换行
//三大件
#include<opencv2/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;

1、highgui.hpp头文件:

包含了所有图形接口函数;

2、core.hpp头文件:
包含OpenCV核心组件。
        例如:Mat类包含在此头文件中,定义一个表示图形的变量就是定义一个Mat类的对象,Mat是一个很大的类,拥有众多有参或者无参构造函数以及众多成员函数,还有内置变量。以下是Mat类常用相关内容:

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

using namespace cv;

void Mat::create(int rows, int cols, int type);
void Mat::create(Size size, int type);
void Mat::create(int ndims, const int* sizes, inttype);
/* 
ndims – 新数组的维数。
rows –新的行数。
cols – 新的列数。
size – 替代新矩阵大小规格:Size(cols, rows)。
sizes – 指定一个新的阵列形状的整数数组。
type – 新矩阵的类型。
*/
void Mat::resize(size_t sz);
void Mat::resize(size_t sz, const Scalar& s);
/*
sz –新的行数。
s –分配给新添加的元素的值。
*/
int Mat::type() const;
/*
该方法返回一个矩阵的元素类型。这是兼容CvMat 类型系统,像 CV_16SC3标识符
或 16 位有符号的3 通道阵列,等等。
*/
int Mat::depth() const;
int Mat::channels() const;
Size Mat::size() const;
bool Mat::empty() const
template<typename T> T& Mat::at(int i)const
template<typename T> const T&Mat::at(int i) const
template<typename T> T& Mat::at(int i,int j)
template<typename T> const T&Mat::at(int i, int j) const
template<typename T> T& Mat::at(Pointpt)
template<typename T> const T&Mat::at(Point pt) const
template<typename T> T& Mat::at(int i,int j, int k)
template<typename T> const T&Mat::at(int i, int j, int k) const
template<typename T> T& Mat::at(constint* idx)
template<typename T> const T&Mat::at(const int* idx) const
/*
返回对指定数组元素的引用。
*/

int mian(void)
{
	Mat A = imread("1.jpg");
	A.empty();                //若没有分配图像数据,该方法返回true  
        Mat B(A);                 //这两种方法均是浅复制(头部)
        B = A;
        A.copyTo(B);              //把A复制到B    //(数据块)
        B = A.clone();            //B是A的副本
        A.convertTO(B,CV_32F,1/255.0,0.0);//如果复制时两者类型不同,使用该函数实现类型转换
           
}

Mat类常用构造函数

1、Mat::Mat()
无参数构造方法;
2、Mat::Mat(int rows, int cols, int type)
创建行数为 rows,列数为 cols,类型为 type 的图像;
3、Mat::Mat(Size size, int type)
创建大小为 size,类型为 type 的图像;
4、Mat::Mat(int rows, int cols, int type, const Scalar& s)
创建行数为 rows,列数为 col,类型为 type 的图像,并将所有元素初始化为值 s;
5、Mat::Mat(Size size, int type, const Scalar& s)
创建大小为 size,类型为 type 的图像,并将所有元素初始化为值 s;
6、Mat::Mat(const Mat& m)
将m赋值给新创建的对象,此处不会对图像数据进行复制,m和新对象共用图像数据,属于浅拷贝;
7、Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)
创建行数为rows,列数为col,类型为type的图像,此构造函数不创建图像数据所需内存,而是直接使用data所指内存,图像的行步长由 step指定。
8、Mat::Mat(Size size, int type, void* data, size_t step=AUTO_STEP)
创建大小为size,类型为type的图像,此构造函数不创建图像数据所需内存,而是直接使用data所指内存,图像的行步长由step指定。
9、Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)
创建的新图像为m的一部分,具体的范围由rowRange和colRange指定,此构造函数也不进行图像数据的复制操作,新图像与m共用图像数据;
10、Mat::Mat(const Mat& m, const Rect& roi)
创建的新图像为m的一部分,具体的范围roi指定,此构造函数也不进行图像数据的复制操作,新图像与m共用图像数据。

这些构造函数中,很多都涉及到类型type。type可以是CV_8UC1,CV_16SC1,…,CV_64FC4 等。里面的 8U 表示 8 位无符号整数,16S 表示 16 位有符号整数,64F表示 64 位浮点数(即 double 类型);C 后面的数表示通道数,例如 C1 表示一个
通道的图像,C4 表示 4 个通道的图像,以此类推。
如果你需要更多的通道数,需要用宏 CV_8UC(n),例如:
Mat M(3,2, CV_8UC(5));//创建行数为 3,列数为 2,通道数为 5 的图像。
 


3、VS2017中常见图标含义解释——图源官网

4、一些常用函数

imread()函数介绍:函数原型为 Mat imread( const String& filename, int flags = IMREAD_COLOR); 第一个参数是图片名的引用,字符串类型,懂吧;第二个参数是以何种形式读入图片。该宏定义在一个枚举类型中,常用的有以下几种:

IMREAD_UNCHANGED            = -1, //!< If set, return the loaded image as is (with alpha(透明度) channel, otherwise() it gets cropped).
IMREAD_GRAYSCALE(CV_8U)   = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR (CV_8UC3 三个无符号字节)     = 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.

namedWindow()函数介绍:函数原型为 void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);第一个参数为窗口名,第二个参数是一个宏,常用的有以下几种:

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.

imshow()函数介绍:函数原型为 void imshow(const String& winname, InputArray mat);第一个参数是窗口名,第二个参数是需要显示的图像变量,InputArray类型目前可以认为与Mat类型无差别,OutputArray也是。

waitKey()函数介绍:由于所写的程序是在控制台窗口中,因此会在main 函数执行完毕后关闭,通过添加这个highgui函数可以让窗口一直显示,直到用户键入数值(至今见过最详细的解释了,心累┓(;´_`)┏)。

flip()函数介绍:函数原型为 void flip(InputArray src, OutputArray dst, int flipCode);   //翻转,支持in-place
@param src() input array.
@param dst(终端) output array of the same size and type as src.//空的也是支持的,不会报错
@param flipCode a flag to specify(具体说明) how to flip the array; 0 means
flipping around the x-axis(关于x轴翻转) and positive value(正值) (for example, 1) means
flipping around y-axis. Negative value (for example, -1) means flipping
around both axes.

imwrite()函数介绍:函数原型为 bool imwrite( const String& filename, InputArray img,
                                          const std::vector<int>& params = std::vector<int>());
关于第三个参数的介绍,不过一般用的都是默认值
第3个变量有3个定义:
CV_IMWRITE_JPEG_QUALITY   
CV_IMWRITE_PNG_COMPRESSION
CV_IMWRITE_PXM_BINARY
分别对应JPEG PNG PPM, PGM, or PBM

如果是保存JPG:
vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    compression_params.push_back(90);//这就是质量 默认值是95
     imwrite("alpha.jpeg",  image_gray, compression_params);
如果是保存PNG:
vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(2);//这就是质量  默认值是3
     imwrite("alpha.png", image_gray, compression_params);

5、鼠标事件

回调函数,是一个函数,不会被显示地调用,但会在响应特定事件时被程序调用,为了可以让程序识别需要有特定的签名(此处,签名应该包括函数名及参数列表),并且必须注册,就是用来说明事件触发后要干啥。
它的签名形式:void onMouse(int event, int x, int y, int flags, void* param);

注册回调函数,是一个过程,这个过程用一个函数来实现(高层函数),让高层函数可以调用底层函数(就是回调函数)
eg: void setMousecallback(const string& winname, MouseCallback onMouse, void* userdata=0)

6、绘图函数

circle()//绘制圆形     putText()//添加文本函数(它们位于 imgproc.hpp头文件中)

7、 定义感兴趣区域

通过定义感兴趣区域(其实是一个Mat类的对象)可以让处理函数只在图像的某个部分起作用,定义ROI有两种方法:

Mat image,logo;
/*方法一*/                                          //image是原图像,logo是要插入的图像
Mat imageROI(image,Rect(0, 0, logo.cols, logo.rows));    //左上角坐标以及区域的宽度和高度
/*方法二*/
//用Range给定行和列范围,用值域限定
imageROI = image(Range(image.rows - logo.rows, image.rows),
                 Range(image.cols - logo.cols, image.cols));

还可以仅使用部分行部分列,有另外两个函数,rowRange(start, end), colRange(start, end)

8、图像掩码

首先说一下copyTo()函数的第二种形式,srcImage.copyto(dstImage, mask),mask作为一个掩模板,如果在某个像素点(i, j)其值为1(只看第一通道,所以mask单通道即可)则把srcImage.at(i, j)()处的值直接赋给dstImage.at(i, j),如果其值为0则dstImage.at(i, j)处保留其原始像素值。这样图标边缘的颜色就和原图一致了(因为边缘具有一致的白色特性)。(其中隐含的条件是scrImage、mask、dstImage三者必须同大小同类型)。这段话同时间接阐述了图像掩码的原理,需要注意掩码算子mask类型必须是CV_8U,并应该是单通道(CV_8UC(1))
 


     

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值