Opencv——读取文件

Mat 对象OpenCV2.0之后引进的图像数据结构、自动分配内存、不存在内存泄漏的问题,是面向对象的数据结构,分了两个部分:头部与数据部分。
1、部分复制:一般情况下只会复制Mat对象的头和指针部分,不会复制数据部分
Mat A = imread(imsPath);
Mat B(A);//只复制
2、完全复制:把Mat的头部和数据部分一起复制
Mat F = A.clone();或者Mat G ;A.copyTo(G);
3、Mat对象使用-四个要点
1)输出图像的内存是自动分配的
2)使用OpenCV的C++接口,不需要考虑内存分配的问题
3)赋值操作和拷贝构造函数只会复制头部份
4)使用clone与copyTo两个函数实现数据的完全复制
4、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));
函数常用方法
Mat(int rows, int cols, int type)void copyTo(Mat mat)
Mat(Size size, int type)void convertTo(Mat dst, int type)
Mat(int rows, int cols, int type, const Scalar &s)Mat clone()
Mat(Size size, int type, const Scalar &s)int channels;
Mat(int ndims, const int *sizes, int type)int depth
Mat(int ndims, const int *sizes, int type, const Scalar &s)bool empty();
noneuchar* ptr(i = 0)
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstdio>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	Mat src = imread("‪‪‪F:\\dream.jpg");//将图像读取到MAT中去
	if (src.empty())//判断图片是否存在
	{
		printf("Could not find the image!\n");
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);//命名窗口,设置自动大小
	imshow("input", src);//显示图像

	Mat dst;//创建一个Mat图像空间dst
	dst = Mat(src.size(), src.type());//大小和类型和src一样
	dst = Scalar(127, 0, 255);//所有像素值的大小
	//2.也可以使用clone()进行完全复制元图像
	//Mat dst = src.clone();
	//3.也可以使用copyTo()进行完全复制元图像
	//src.copyTo(dst);
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("output1", dst);

	cvtColor(src, dst, CV_BGR2GRAY);//将src转化为灰度图存放在dst中
	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("row: %d cols : %d \n",rows, cols);
	const uchar* firstRow = dst.ptr<uchar>(0);
	printf("first piwl values :%d\n", *firstRow);
	Mat M(100,100, CV_8UC1, Scalar(127));
	cout << "M =" << endl << M << endl;

	Mat ml;
	ml.create(src.size(), src.type());
	ml = Scalar(0, 0, 255);
	imshow("output2", ml);

	Mat csrc
	Mat kernel = (Mat_<float>(3, 3)) << 0, -1, 0, -1, 5, -1, 0, -1, 0 );
	filter2D(src, csrc, -1, kernel);
	imshow("output3", csrc);


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

	waitKey(0);
	return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值