第三节、Mat对象
Mat对象与IplImage对象
Mat对象构造函数与常用方法
常用方法: void copyTo(Mat mat) void convertTo(Mat dst, int type) Mat clone() int channels() int depth() bool empty(); uchar* ptr(i=0)
|
Mat对象使用
Mat对象使用-四个要点
Mat对象创建
1. cv::Mat::Mat构造函数:Mat M(2,2,CV_8UC3, Scalar(0,0,255)),其中前两个参数分别表示行(row)跟列(column)、第三个CV_8UC3中的8表示每个通道占8位、U表示无符号、C表示Char类型、3表示通道数目是3,第四个参数是向量表示初始化每个像素值是多少,向量长度对应通道数目一致
2. 创建多维数组:
cv::Mat::create
int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC1, Scalar::all(0));
3. cv::Mat::create实现:
Mat M;
M.create(4, 3, CV_8UC2);
M = Scalar(127,127);
cout << "M = " << endl << " " << M << endl << endl;
uchar* firstRow = M.ptr
printf("%d", *firstRow);
4. 定义小数组:
Mat C = (Mat_
cout << "C = " << endl << " " << C << endl << endl;
代码演示:
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat src;
src = imread("1.jpg");
if (src.empty()) {
cout << "could not load image..." << endl;
return -1;
}
namedWindow("input", CV_WINDOW_AUTOSIZE);
imshow("input", src);
/*Mat dst;
dst = Mat(src.size(), src.type());
dst = Scalar(127, 0, 255);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", dst);*/
Mat dst;
//src.copyTo(dst);
namedWindow("output", CV_WINDOW_AUTOSIZE);
cvtColor(src, dst, CV_BGR2GRAY);
printf("input image channels : %d\n", src.channels());
printf("output image channels : %d\n", dst.channels());
int cols = dst.cols;
int rows = dst.rows;
printf("rows : %d cols : %d\n", rows, cols);
const uchar* firstRow = dst.ptr(0);
printf("fist pixel value : %d\n", *firstRow);
Mat M(100, 100, CV_8UC1, Scalar(127));
//cout << "M =" << endl << M << endl;
Mat m1;
m1.create(src.size(), src.type());
m1 = Scalar(0, 0, 255);
Mat csrc;
Mat kernel = (Mat_(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, csrc, -1, kernel);
Mat m2 = Mat::eye(2, 2, CV_8UC1);
cout << "m2 =" << endl << m2 << endl;
imshow("output", m2);
waitKey(0);
return 0;
}