预备知识:
1. 图像准备
2.程序模板
3. Mat类
3.1 Mat类的创建
4. Mat类常用参数
1. 图像准备
(1)图像常用的类型有:二值图像(图像像素取值为0或255),灰度图像(图像像素取值为[0,255]),彩色RGB图像(每个像素值包含三个变量,分别为B、G、R,取值均为[0,255])
(2)图像常用参数为: 维度dims --> img.dims 、行数rows --> img.rows 、列数cols --> img.cols 、通道数channels --> img.channels() 、类型type --> img.type() 、
深度depth --> img.depth() 、元素大小elemSize --> img.elemSize() 、通道大小elemSize1 --> img.elemSize1();
2. 程序模板
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 4 using namespace std; 5 using namespace cv; 6 7 int main(int agrc, char **agrv){ 8 .... 9 .... 10 waitKey(0); 11 return 0; 12 }
3. Mat类
3.1Mat类的创建方法:
1 Mat( )
// 定义矩阵大小和类型 2 Mat(int rows, int cols, int type) 3 Mat(Size(int cols, int rows), int type)
// 定义矩阵大小类型和初始化值 4 Mat(int rows, int cols, int type, const Scalar &s) 5 Mat(Size(int cols, int rows), int type, const Scalar &s)
// 复制一个已经存在的矩阵,并截取一部分 6 Mat(const Mat &m) 7 Mat(const Mat &m, const Range &rowRange, const Range &colRange) 8 Mat(const Mat &m, const Rect &roi)
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 4 using namespace std; 5 using namespace cv; 6 7 int main() { 8 Mat im = imread("lena.jpg", 1); 9 if(im.empty()){ 10 cout << "no image loaded." << endl; 11 return -1; 12 } 13 Rect rect(180, 200, 200, 200); 14 Mat roi = Mat(im, rect); 15 Mat im_Rect = im.clone(); 16 rectangle(im_Rect, rect, Scalar(0,255,0),2); 17 imshow("roi image", roi); 18 imshow("original image with rectangle", im_Rect); 19 waitKey(); 20 return 0; 21 }
4. Mat常用参数
维度dims --> img.dims 、行数rows --> img.rows 、列数cols --> img.cols 、通道数channels --> img.channels() 、
类型type --> img.type() 、深度depth --> img.depth() 、元素大小elemSize --> img.elemSize() 、通道大小elemSize1 --> img.elemSize1();
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 4 using namespace std; 5 using namespace cv; 6 int main(int argc, char **argv) { 7 Mat Im = imread("lena.jpg", 3); 8 if (Im.empty()) { 9 cout << "no image load!" << endl; 10 return -1; 11 } 12 cout << "dims:" << Im.dims << endl; 13 cout << "rows:" << Im.rows << endl; 14 cout << "cols:" << Im.cols << endl; 15 cout << "channels:" << Im.channels() << endl; 16 cout << "type:" << Im.type() << endl; 17 cout << "depth:" << Im.depth() << endl; 18 cout << "elemSize:" << Im.elemSize() << endl; 19 cout << "elemSize1:" << Im.elemSize1() << endl; 20 waitKey(0); 21 return 0; 22 }