b站:https://www.bilibili.com/video/BV1uW411d7Wf?p=5
我在b站的学习笔记和实验代码的整理如下:
示例代码:
Matdemo
//----------------------------------------------
//Mat对象的创建和相关函数的使用演示
//作者:a1000world
//时间:2020.5.17
//---------------------------------------------
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
//创建Mat对象,src原图
Mat src;
src = imread("E:/picture/pic_cv/pic.jpg");
if (src.empty()) {
cout << "Could not load image..." << endl;
return -1;
}
namedWindow("input picture", WINDOW_AUTOSIZE);
imshow("input picture", src);
//创建Mat对象,dst目标图
Mat dst;
//dst = Mat(src.size(), src.type());
//dst = Scalar(127,0,255);
//调用clone函数
dst = src.clone();
namedWindow("output_background1", WINDOW_AUTOSIZE);
imshow("output_background1", dst);
//调用copyTo函数
src.copyTo(dst);
namedWindow("output_background2", WINDOW_AUTOSIZE);
imshow("output_background2", dst);
//调用channel函数
cvtColor(src,dst,COLOR_BGR2GRAY);//将原图转化成灰度图
printf("src.channels:%d\n", src.channels());
printf("dst.channels:%d\n", dst.channels());
namedWindow("output_background3", WINDOW_AUTOSIZE);
imshow("output_background3", dst);
//得到行的灰度值,多少行,多少列
int rows = dst.rows;
int cols = dst.cols;
printf("rows:%d col:%d\n", rows, cols);
const uchar* firstRow = dst.ptr<uchar>(0);
printf("first pixel value:%d\n", *firstRow);
//其他的构造方法
Mat M = Mat(100, 100, CV_8UC3, Scalar(0, 0, 255));
cout << "M = " << endl << M << endl;
namedWindow("M", WINDOW_AUTOSIZE);
imshow("M", M);
//其他构造方法 create
Mat N;
//N.create(40, 20, CV_8UC3);
N.create(src.size(), src.type());
N = Scalar(231, 144, 23);
/*cout << "N=" << endl << " " << N << endl << endl;*/
uchar* firstRowN = N.ptr<uchar>(0);
printf("N.first_pixel:%d\n", *firstRowN);
namedWindow("N", WINDOW_AUTOSIZE);
imshow("N", N);
//定义小数组
Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst, -1, kernel);
namedWindow("filter2D", WINDOW_AUTOSIZE);
imshow("filter2D", dst);
//两种初始化函数
//初始化函数zero,0
//Mat m2 = Mat::zeros(src.size(), src.type());
//初始化函数eye,左上到右下是1,别的是0(对角线)
//Mat m2 = Mat::eye(src.size(), src.type());
waitKey();
return 0;
}