Opencv官网手册学习---1
2017-08-20 15:13
197 查看
Opencv学习系列是针对opencv官网的tutorials进行学习之后,然后再整理,记录。
方便以后学习使用。以后的文章不再声明
Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values (taking any dimensionality depending on the method chosen for storing) . The matrix header size is constant。
图像的拷贝有很多种,先介绍本文中提到的,之后的遇见在说:
括号复制:这种方式只会拷贝图像头结点。
比如
以上方式在不用的时候不会自动清空内存
那么如果需要一种不需要时自动清空的拷贝怎么办?
Opencv提供了两个函数:
cv::Mat::clone() 和 cv::Mat::copyTo() functions.
附上代码:
下面记录一下Mat的构造方法:
cv::Mat::Mat Constructor
两行很好理解,但是为什么这里有6行呢?
这是因为我们创建的是3个通道的,每一列有3个通道,所以就有6列了
M对象前两个参数是矩阵的行列数,第三个参数是数据类型:
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]
所以CV_8UC3意思是:8位无符号char型的,并且每个像素有3个通道。也就是RGB。相应的有CV_8UC1,CV_8UC2。
cv::Scalar是用来初始化的,将矩阵设置为单一灰度。
如果是(255,0,0),则图像是全蓝
如果是(0,255,0),图像是全绿
如果是(0,0,255),图像是全红
cv::Mat::create()函数也可以创建
介绍几个MATLAB常用的初始化函数,全0,全1,和单位矩阵
Mat E = Mat::eye(4, 4, CV_64F);
cout << “E = ” << endl << ” ” << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout << “O = ” << endl << ” ” << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout << “Z = ” << endl << ” ” << Z << endl << endl;
CV_64F代表64为浮点型数据
对于小矩阵,也可以进行手动初始化
方便以后学习使用。以后的文章不再声明
Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values (taking any dimensionality depending on the method chosen for storing) . The matrix header size is constant。
图像的拷贝有很多种,先介绍本文中提到的,之后的遇见在说:
括号复制:这种方式只会拷贝图像头结点。
比如
Mat A=imread("lena.jpa",1); Mat B(A);
以上方式在不用的时候不会自动清空内存
那么如果需要一种不需要时自动清空的拷贝怎么办?
Opencv提供了两个函数:
cv::Mat::clone() 和 cv::Mat::copyTo() functions.
Mat A=imread("lena.jpa",1); Mat B=A.clone(); Mat C; A.copyTo(C);
附上代码:
#include"opencv2/opencv.hpp" #include <iostream> using namespace cv; using namespace std; int main() { Mat A = imread("C:\\Users\\asus\\Desktop\\lena.jpg", 1);//1是默认读入 imshow("origin", A); Mat B(A); imshow("1", B); Mat C(A, Rect(180, 200, 200, 200));//ROI imshow("2", C); Mat D; A.copyTo(D); imshow("3", D); waitKey(0); return 0; }
下面记录一下Mat的构造方法:
cv::Mat::Mat Constructor
Mat M(2,2, CV_8UC3, Scalar(0,0,255)); cout << "M = " << endl << " " << M << endl << endl;
两行很好理解,但是为什么这里有6行呢?
这是因为我们创建的是3个通道的,每一列有3个通道,所以就有6列了
M对象前两个参数是矩阵的行列数,第三个参数是数据类型:
CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]
所以CV_8UC3意思是:8位无符号char型的,并且每个像素有3个通道。也就是RGB。相应的有CV_8UC1,CV_8UC2。
cv::Scalar是用来初始化的,将矩阵设置为单一灰度。
如果是(255,0,0),则图像是全蓝
如果是(0,255,0),图像是全绿
如果是(0,0,255),图像是全红
cv::Mat::create()函数也可以创建
M.create(4,4, CV_8UC(2)); cout << "M = "<< endl << " " << M << endl << endl;
介绍几个MATLAB常用的初始化函数,全0,全1,和单位矩阵
Mat E = Mat::eye(4, 4, CV_64F);
cout << “E = ” << endl << ” ” << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
cout << “O = ” << endl << ” ” << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
cout << “Z = ” << endl << ” ” << Z << endl << endl;
CV_64F代表64为浮点型数据
对于小矩阵,也可以进行手动初始化
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cout << "C = " << endl << " " << C << endl << endl;
相关文章推荐
- Opencv学习手册(三)---图像坐标变换
- 学习《OpenCV 2 计算机视觉编程手册》第二章---使用指针遍历图像案例
- 学习phalcon框架按照官网手册搭建第一个项目注册功能
- opencv 学习手册
- Opencv学习手册(一)--- 图像文件读入和显示
- opencv3学习笔记(二)——opencv官网预览
- 学习《OpenCV 2 计算机视觉编程手册》第二章存取像素值案例--椒盐噪声
- django官网手册学习 一
- 学习《OpenCV 2 计算机视觉编程手册》第二章遍历图像和领域操作实例——对图像进行锐化
- 我的Opencv学习手册(一)
- Opencv学习手册(二)--- 图像采集与显示
- 【opencv学习之十一】opencv: HighGUI参考手册
- Opencv学习手册(四)--- 图像灰度变换
- 《Opencv 2计算机视觉编程手册 》学习笔记 1
- Redis学习手册(String数据类型)
- 【OpenCV学习】边缘检测
- 【OpenCV归纳】3 在实例中学习简单函数以及数据读写
- Python-OpenCV学习(二):OpenCV+python在windows上的安装
- Linux命令学习手册-yes命令
- OpenCV参考手册之Mat类详解2