opencv03-Mat矩阵API的使用
构造方法(具体介绍看API文档)
int main() {Mat m1 = Mat(200, 100, CV_8UC1);imshow("o1", m1);Mat m2 = Mat(Size(100, 200), CV_8UC1);imshow("o2", m2);Mat m3 = Mat(200, 100, CV_8UC3, Scalar(255, 0, 0));imshow("o3", m3);const int sizes[] = {200, 3};Mat m4 = Mat(2, sizes, CV_8UC3);imshow("m4", m4);waitKey(0);return 0;
}
成员方法
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;
int main() {string filename = "D:/workspace/cpp_workspace/my-cv/data/img/lena.jpg";Mat img = imread(filename, ImreadModes::IMREAD_COLOR);Mat b = Mat(img); cout << img.channels() << endl; Mat img2;cvtColor(img, img2, COLOR_BGR2GRAY); imshow("img2", img2);cout << img2.channels() << endl; cout << img.depth() << endl; cout << img2.depth() << endl;cout << img.cols << endl; cout << img2.cols << endl; cout << img.dims << endl; cout << img.size << endl; cout << img.rows << endl; cout << img2.rows << endl; cout << img.row(0).size << endl; cout << img.col(0).size << endl; cout << img.empty() << endl;Mat copyTo;img.copyTo(copyTo);imshow("copyTo", copyTo);Mat convertTo;img.convertTo(convertTo, CV_8UC4);imshow("convertTo", convertTo);Mat clone = img.clone();imshow("clone", clone);cout << ".............................." << endl;Mat M;M.create(4, 3, CV_8UC2);M = Scalar(127, 127);cout << "M = " << endl << " " << M << endl << endl;uchar *firstRow = M.ptr<uchar>(1);printf("%d\n", *firstRow);Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);cout << "C = " << endl << " " << C << endl << endl;cout << ".............................." << endl;cv::Mat image = cv::Mat::ones(5, 10, CV_8UC1); uchar *data00 = image.ptr<uchar>(0); uchar *data10 = image.ptr<uchar>(1); uchar data01 = image.ptr<uchar>(0)[1]; printf("%d\n", *data00); printf("%d\n", data01); cout << image << endl; waitKey(0);return 0;
}
成员方法 create
int main() {Mat m;m.create(2, 3, CV_8UC1);Mat m3;const int sizes[] = {3, 4};m3.create(2, sizes, CV_8UC3);cout << m3 << endl;Mat m4;std::vector<int> sizes2 = vector<int>();sizes2.push_back(3);sizes2.push_back(4);m4.create(sizes2, CV_8UC3);cout << m4 << endl;waitKey(0);return 0;
}