opencv中Mat类对象创建的方法总结

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

using namespace std;
using namespace cv;
//如何创建Mat对象!
int main(int argc,char**argv[])
{
	Mat src;
	src = imread("F:\\LX(OpenCV)\\ny1.bmp");
	if (src.empty())
	{
		//cout<<"could not load image\n"<<endl;endl=end line,即输出一行换一行
		printf("could not load image\n");
		return -1;
	}
	namedWindow("src");
	imshow("src", src);

	/*Mat dst;
	dst = Mat(src.size(), src.type());
	dst = Scalar(134, 123, 33);
	*/

	/*Mat  dst;
	src.copyTo(dst);
	*/

	Mat dst;
	//dst = src.clone();


	cvtColor(src, dst, COLOR_BGR2GRAY);

	const uchar*out = dst.ptr<uchar>(0);

	printf("第一个像素值:%d\n", *out);

	int row = src.rows;
	int col = src.cols;
	int c = src.channels();
	int d = dst.channels();
	printf("src的行:%d\n",row);
	printf("src的列:%d\n",col);
	printf("src的通道数:%d\n",c);
	printf("dst的通道数:%d\n", d);

	Mat M(3, 3, CV_8UC3, Scalar(0, 255, 0));
	cout << "M=" << endl << M << endl;

	Mat N;
	N = Mat(3,3, CV_8UC3, Scalar(0, 254, 0));
	
	cout << "N=" << endl << N << endl;

	Mat a;
	a.create(3, 3, CV_8UC3 );
	a = Scalar(23, 145, 224);

	Mat r(a);//只是复制头和指针,不复制数据
	cout << "r=" << endl << r << endl;


	cout << "a=" << endl << a << endl;

	Mat b = Mat::eye(3,3, CV_8UC3); //Mat b = Mat::zeros(3,3, CV_8UC3);
	cout << "b=" << endl << b << endl;

	//定义小数组
	Mat kernel=(Mat_<int>(3,3)<<0,-1,0,-1,5,-1,0,-1,0);
	cout << "kernel=" << endl << kernel << endl;


	namedWindow("dst");
	imshow("dst", dst);

	waitKey(0);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值