OpenCV基础课程笔记04Mat常用方法
程序员文章站
2023-12-23 15:57:28
...
代码
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
Mat src = imread("A:专用\\TestForTheCV\\class4酷酷.jpg");
if (src.empty()) {
cout << "No file!" << endl;
}
else {
imshow("kuku", src);
}
//统一赋值
Mat dst = Mat(src.size(), src.type());
dst = Scalar(155, 56, 255);
imshow("output", dst);
//克隆
Mat dst = Mat(src.size(), src.type());
dst = src.clone();
Mat dst2;
src.copyTo(dst2);
cvtColor(dst2, dst2, CV_BGR2GRAY);
imshow("output", dst);
imshow("output2", dst2);
cout << "output channel:" << dst.channels() << endl << "output2 channel:" << dst2.channels() << endl;
//创建一个带色的mat
Mat M = Mat(100, 100, CV_8UC3, Scalar(0, 0, 255)); //行、列、8位无符号3通道、像素向量
imshow("M", M);
//创建一个渐变图
Mat m = Mat(255, 200, CV_8UC1, Scalar(150));
for (int i = 0; i < 255; i++) {
uchar* current = m.ptr<uchar>(i);
for (int j = 0; j < 200; j++) {
current[j] = i;
}
}
imshow("m", m);
//对角线为1的矩阵
Mat duijiaoxian = Mat::eye(23, 23,CV_8UC1);
cout << duijiaoxian;
imshow("对角线", duijiaoxian);
waitKey(0);
return 0;
}
运行结果
统一赋值
克隆
创建一个带色的Mat
创建一个渐变图(这个是自己写着玩)
对角线为1的矩阵