1、Mat类
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a = cv::Mat::Mat();
a = (cv::Mat_<int>(3, 2) << 1, 2, 3, 4, 5, 6);
Mat b = cv::Mat::Mat(3, 2, CV_8UC1);
Mat c = cv::Mat::Mat(Size(3, 2), CV_8UC1);
Mat d = cv::Mat::Mat(c);
Mat e = d.clone();
Mat f = cv::Mat::Mat(a, Range(0, 2), Range(0, 2));
cout << "a:" << endl << a << endl;
cout << "b:" << endl << b << endl;
cout << "c:" << endl << c << endl;
cout << "d:" << endl << d << endl;
cout << "e:" << endl << e << endl;
cout << "f:" << endl << f << endl;
return 0;
}
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a, b, c;
a = b = c = cv::Mat::Mat();
b = (cv::Mat_<int>(3, 2) << 1, 2, 3, 4, 5, 6);
c = cv::Mat::Mat(2, 2, CV_8UC3, cv::Scalar(0, 0, 255));
Mat d = cv::Mat_<int>(3, 3);
for (int i = 0; i < d.rows; i++)
{
for (int j = 0; j < d.cols; j++)
{
d.at<int>(i, j) = 1;
}
}
Mat e = cv::Mat::Mat::zeros(2, 2, CV_8UC1);
float m_array[8] = { 1,2,3,4,5,6,7,8 };
Mat f = cv::Mat(2, 2, CV_32FC2, m_array);
cout << "a:" << endl << a << endl;
cout << "b:" << endl << b << endl;
cout << "c:" << endl << c << endl;
cout << "d:" << endl << d << endl;
cout << "e:" << endl << e << endl;
cout << "f:" << endl << f << endl;
return 0;
}
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a, b, c;
a = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
b = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
c = (cv::Mat_<float>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
cout << "加法:" << endl;
cout << a << "+" << endl << b << "=" << endl << a + b << endl;
cout << "减法:" << endl;
cout << a << "-" << endl << b << "=" << endl << a - b << endl;
cout << "数乘:" << endl;
cout << a << "*" << endl << 2 << "=" << endl << a * 2 << endl;
cout << "除法:" << endl;
cout << a << "/" << endl << 2 << "=" << endl << a / 2 << endl;
cout << "内积:" << endl;
cout << a << "*" << endl << c << "=" << endl << a.dot(c) << endl;
cout << "点乘:" << endl;
cout << a << ".*" << endl << b << "=" << endl << a.mul(b) << endl;
return 0;
}
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat a = cv::Mat(3, 3, CV_8UC3, Scalar(1,1,1));
cv::Vec3b b = a.at<Vec3b>(0, 0);
cout << "a[0,0]=" << b << endl;
uchar* ptr;
for (int i = 0; i < a.rows; i++)
{
ptr = a.ptr<uchar>(i);
for (int j = 0; j < a.cols*a.channels(); j++)
{
cout << (int)ptr[j] << " ";
}
cout << endl;
}
cv::Mat d(3, 3, CV_8UC3, Scalar(0, 1, 2));
int c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 0));
cout << "d[0,0,1]=" << c << endl;
c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 1));
cout << "d[0,0,2]=" << c << endl;
c = (int)(*(d.data + d.step[0] * 0 + d.step[1] * 0 + 2));
cout << "d[0,0,3]=" << c << endl;
return 0;
}
对于一些定义有疑问的地方,其实通过VS2017看源码是较好的方法
|