OpenCV第四发:Mat类常用基本操作

这几天发现C++有些狠迷糊,恶补了几天C++。

 

#include <iostream>
#include <string>
#include <thread>
#include <math.h>
#include <Windows.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main() {

	Mat src = imread("D:/opencv/Test06.jpg");

	//if (src.data == NULL) {
	//	printf("骚年:你应该继续查找你的文件路径是什么??或者你电脑是否需要换新的。");
	//	return -1;
	//}

	if (src.empty()) {//如果图像为空则返回 false
		printf("如果图像为空则返回 false");
		return -1;
	}
	//imshow("源图像", src);
	//新的Mat对象和src一模一样。
	Mat dst = Mat(src.size(),src.type());
	//将初始的Scalar值设置为 Scalar(127,0,255),类似于设置 R G B 值
	dst = Scalar(8,255,2);
	//imshow("dst源图像", dst);


	//这个只是复制了Src图像的头指针部分, NewSrc和Src的数据部分是公用的。
	Mat NewSrc = Mat(src);
	//imshow("New新的图像", NewSrc);

	//完全复制新的对象等同于克隆
	Mat CopySrc;
	src.copyTo(CopySrc);
	//imshow("复制图像", CopySrc);

	//克隆新的图像等同于复制 - 完全复制新的对象
	Mat CloneSrc = src.clone();
	//imshow("Clone源图像", CloneSrc);
	cout << "转换为灰度空间之前,图像: " << CloneSrc.channels() << " 通道的格式" << endl;

	//转换为灰度空间,则图像的通道变为 1通道
	cvtColor(src,CloneSrc,CV_BGR2GRAY);
	cout << "转换为灰度空间之后,图像: " << CloneSrc.channels() <<" 通道的格式"<< endl;
	
	//获取第一行的指针,
	const uchar* firstRow = CloneSrc.ptr<uchar>(0);
	cout << "第一个像素值: " << int(*firstRow) << endl;

	//获取行列计数
	int CloneSrcCols = CloneSrc.cols;
	int CloneSrcRows = CloneSrc.rows;
	cout << "CloneSrcCols: " << CloneSrcCols << endl;
	cout << "CloneSrcRows: " << CloneSrcRows << endl;
	// rows  cols  CV_8UC3 为通道3
	Mat ZDY3 = Mat(20,20,CV_8UC3,Scalar(1,255,200));
	cout << "ZDY3" << endl << ZDY3 << endl;

	// rows  cols  CV_8UC3 为通道3
	Mat ZDY1 = Mat(20, 20, CV_8UC1, Scalar(200));
	cout << "ZDY"<<endl<< ZDY1 <<  endl;

	//通过creat创建
	Mat CreatMat;
	CreatMat.create(src.size(), src.type());
	CreatMat = Scalar(100,0,5);

	//创建掩模
	Mat Mkey = (Mat_ <float>(3,3) << 0,-1,0,-1,5,-1,0,-1,0);
	cout << "创建掩模Mkey" << endl << Mkey << endl;
	filter2D(src,src,-1,Mkey);
	//imshow("增强后图像", src);

	//纯黑色图像
	Mat BlackMat = Mat::zeros(src.size(),src.type());
	//imshow("zeros纯黑色图像", BlackMat);


	//对角线初始化
	//Mat eyekMat = Mat::eye(src.size(),CV_8UC1);
	Mat eyekMat = Mat::eye(3,3, CV_8UC1);
	cout << "对角线初始化eyekMat" << endl << eyekMat << endl;
	
	
	cout << endl;
	
	 //加了这个图像就无法显示。
	//system("pause");
	waitKey(0);
	return 0;


	//Mat::Mat();
	//Mat::Mat(int rows,int cols,int type);
	//Mat::Mat(Size size,int type);
	//Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)
	//Mat::Mat(Size _sz, int _type, const Scalar& _s)
	//Mat::Mat(int _dims, const int* _sz, int _type)
	//Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s)

	//void copyTo(Mat mat)
	//void convertTo(Mat mat)
	//Mat clone()
	//int channel()
	//int depth()
	//bool empty()
	//uchar *ptr(i=0)


}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值