您好,欢迎来到尔游网。
搜索
您的当前位置:首页OpenCV笔记(三)Mat对象

OpenCV笔记(三)Mat对象

来源:尔游网

一、Mat对象与IpIImage对象

  • Mat对象OpenCV2.0之后引进的图像数据结构、自动分配内存、不存在内存泄漏的问题,是面向对象的数据结构。分了两个部分,头部与数据部分
  • IplImage是从2001年OpenCV发布之后就一直存在,是C语言风格的数据结构,需要开发者自己分配与管理内存,对大的程序使用它容易导致内存泄漏问题

二、Mat对象构造函数与常用方法

Mat()               
Mat(int rows,int cols,int type)              
Mat(Size size,int type)               
Mat(int rows,int cols,int type,const Scalar &s)	
Mat(Size size,int type,const Scale &s)
Mat(int ndims,const int *size,int type)	
Mat(int ndims,const int *size,int type,const Scale &s)	
void copyTo(Mat mat)
void convertTo(Mat dst, int type)
Mat clone()
int channels()
int depth()
bool empty();
uchar* ptr(i=0)

三、Mat对象使用

  • 部分复制:一般情况下只会复制Mat对象的头和指针部分,不会复制数据部分

    Mat A= imread(imgFilePath)

  • 完全复制:如果想把Mat对象的头部和数据部分一起复制,可以通过如下两个API实现

    Mat F = A.clone(); 或 Mat G; A.copyTo(G);

四、Mat对象使用-四个要点

  • 输出图像的内存是自动分配的
  • 使用OpenCV的C++接口,不需要考虑内存分配问题
  • 赋值操作和拷贝构造函数只会复制头部分
  • 使用clone与copyTo两个函数实现数据完全复制

五、Mat对象创建

  • cv::Mat::Mat构造函数 Mat M(2,2,CV_8UC3, Scalar(0,0,255))
    • 前两个参数分别表示行(row)跟列(column)
    • 第三个CV_8UC3中的8表示每个通道占8位、U表示无符号、C表示Char类型、3表示通道数目是3
    • 第四个参数是向量表示初始化每个像素值是多少,向量长度对应通道数目一致
  • 创建数组cv::Mat::create
    • int sz[3] = {2,2,2};
    • Mat L(3,sz, CV_8UC1, Scalar::all(0));

六、综合例程

cv::Mat::create

Mat M;
M.create(4, 3, CV_8UC2);
M = Scalar(127,127);
cout << "M = " << endl << " " << M << endl << endl;
uchar* firstRow = M.ptr<uchar>(0);
printf("%d", *firstRow);

定义小数组

Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);     
cout << "C = " << endl << " " << C << endl << endl;

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) 
{
	Mat src;
	src = imread("D:/vcprojects/images/test.png");
	if (src.empty()) 
    {
		cout << "could not load image..." << endl;
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);

	/*Mat dst;
	dst = Mat(src.size(), src.type());
	dst = Scalar(127, 0, 255);
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("output", dst);*/
	Mat dst;
	//src.copyTo(dst);
	namedWindow("output", CV_WINDOW_AUTOSIZE);

	cvtColor(src, dst, CV_BGR2GRAY);
	printf("input image channels : %d\n", src.channels());
	printf("output image channels : %d\n", dst.channels());

	int cols = dst.cols;
	int rows = dst.rows;

	printf("rows : %d cols : %d\n", rows, cols);
	const uchar* firstRow = dst.ptr<uchar>(0);
	printf("fist pixel value : %d\n", *firstRow);

	Mat M(100, 100, CV_8UC1, Scalar(127));
	//cout << "M =" << endl << M << endl;

	Mat m1;
	m1.create(src.size(), src.type());
	m1 = Scalar(0, 0, 255);

	Mat csrc;//定义小数组
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	filter2D(src, csrc, -1, kernel);

	Mat m2 = Mat::eye(2, 2, CV_8UC1);
	cout << "m2 =" << endl << m2 << endl;

	imshow("output", m2);
	waitKey(0);
	return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务