您的位置:首页 > 运维架构

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。

图像的拷贝有很多种,先介绍本文中提到的,之后的遇见在说:

括号复制:这种方式只会拷贝图像头结点。

比如

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