OpenCV中强大的Mat类型大家已经比较熟悉了。这里梳理一些在工程中其他经常用到的几种基本数据类型。包括:
Vec
Scalar
Point
Size
Rect
RotatedRect
Vec是一个模板类,主要用于存储数值向量。
Vec<double, 8> myVector; // 定义一个存放8个double型变量的向量
myVector[0]=0;
typedef Vec<uchar, 2> Vec2b; typedef Vec<uchar, 3> Vec3b; typedef Vec<uchar, 4> Vec4b; typedef Vec<short, 2> Vec2s; typedef Vec<short, 3> Vec3s; typedef Vec<short, 4> Vec4s; typedef Vec<int, 2> Vec2i; typedef Vec<int, 3> Vec3i; typedef Vec<int, 4> Vec4i; typedef Vec<float, 2> Vec2f; typedef Vec<float, 3> Vec3f; typedef Vec<float, 4> Vec4f; typedef Vec<float, 6> Vec6f; typedef Vec<double, 2> Vec2d; typedef Vec<double, 3> Vec3d; typedef Vec<double, 4> Vec4d; typedef Vec<double, 6> Vec6d;
v1 = v2 + v3 v1 = v2 - v3 v1 = v2 * scale v1 = scale * v2 v1 = -v2 v1 += v2 v1 == v2, v1 != v2 norm(v1) (euclidean norm)
// Vec cv::Vec<double, 8> myVector; for(int i=0; i<myVector.rows;i++) myVector[i] = i; cout<<"myVector= "<<myVector<<endl; cout<<"myVector[0]= "<<myVector[0]<<endl; cout<<"myVector[3]= "<<myVector[3]<<endl;运行结果:
cv::Vec<int, 6> v1,v2,v3; for(int i=0; i<v2.rows;i++){ //v2.rows返回向量v2的行数 v2[i] = i; v3[i] = i+1; } v1 = v2 + v3; cout<<"v2 = "<<v2<<endl; cout<<"v3 = "<<v3<<endl; cout<<"v1=v2+v3= "<<v1<<endl; cout<<"v1=v2*2 = "<<v2*2<<endl; cout<<"v1=-v2 = "<<-v2<<endl; cout<<"v1==v2 = "<<(v1==v2)<<endl; cout<<"v1!=v2 = "<<(v1!=v2)<<endl; cout<<"norm(v2)= "<<norm(v2)<<endl;
运行结果:
Scalar是一个从Vec类引出的模板类,是一个可存放4个元素的向量,广泛用于传递和读取图像中的像素值。
可使用[]访问Scalar值。或使用如下方式定义BGR三个通道的值。
cv:: Scalar( B, G, R )
cv::Scalar myScalar; myScalar = cv::Scalar(0,255,0); cout<<"myScalar = "<<myScalar<<endl; system("pause");
运行结果:
彩色图像的每个像素对应三个部分:RGB三个通道。因此包含彩色图像的cv::Mat类会返回一个向量,向量中包含三个8位的数值。OpenCV为这样的短向量定义了一种类型,即我们上述的cv::Vec3b。这个向量包含三个无符号字符(unsigned character)类型的数据。
OpenCV存储通道次序为:蓝色、绿色、红色即BGR。
因此,访问彩色像素中元素的方法如下:
cv::Mat pImg = cv::imread("Lena.jpg",1); if(!pImg.data) return 0; int x = 100, y = 100; cv::Scalar pixel=pImg.at<Vec3b>(x,y); cout<<"B chanel of pixel is = "<<pixel.val[0]<<endl; cout<<"G chanel of pixel is = "<<pixel.val[1]<<endl; cout<<"R chanel of pixel is = "<<pixel.val[2]<<endl; system("pause");
运行结果:
常用于表示2维坐标(x,y)。
对图像而言,我们可以这样定义:
cv::Point pt; pt.x = 10; pt.y = 8;或者
cv::Point pt = Point(10, 8);
或者
cv::Point pt(10,8);
typedef Point_<int> Point2i; typedef Point2i Point; typedef Point_<float> Point2f; typedef Point_<double> Point2d;
pt1 = pt2 + pt3; pt1 = pt2 - pt3; pt1 = pt2 * a; pt1 = a * pt2; pt1 += pt2; pt1 -= pt2; pt1 *= a; double value = norm(pt); // L2 norm pt1 == pt2; pt1 != pt2;
// Point cv::Point pt; pt.x = 278; pt.y = 269; //或者 //cv::Point pt (278,269); cv::Scalar pix = pImg.at<Vec3b>(pt); cout<<"pix("<<pt.x<<","<<pt.y<<") = "<<pix<<endl;
运行结果:
cv::Point pt1(10,20); cv::Point pt2(2,3); cout<<"pt1 = "<<pt1<<endl; cout<<"pt2 = "<<pt2<<endl; cout<<"pt1+pt2 = "<<pt1+pt2<<endl; cout<<"pt1+=pt2= "<<(pt1+=pt2)<<endl; cout<<"pt1-pt2 = "<<pt1-pt2<<endl; cout<<"pt2*2 = "<<pt2*2<<endl;
运行结果:
模板类Size可表示一幅图像或一个矩形的大小。它包含宽、高2个成员:width , height还有一个有用的面积函数area()。
cv::Size size(int w, int h); //或者 cv::Size size; size.width = w; size.height = h;
// Size cv::Size size1(6,3); cv::Size size2; size2.width = 4; size2.height = 2; cv::Mat mat1(size1,CV_8UC1,cv::Scalar(0)); cv::Mat mat2(size2,CV_8UC3,cv::Scalar(1,2,3)); cout<<"mat1 = "<<endl<<mat1<<endl; cout<<endl<<"mat2 = "<<endl<<mat2<<endl; system("pause");
运行结果:
Rect是另一个用于定义2维矩形的模板类。它由两个参数定义:
Rect可以用来定义图像的ROI区域。
cv::Rect rect(x, y, width, height);
// Rect cv::Mat pImg = imread("Lena.jpg",1); cv::Rect rect(180,200,200,200);//(x,y)=(180,200),w=200,height=200 cv::Mat roi = cv::Mat(pImg, rect); cv::Mat pImgRect = pImg.clone(); cv::rectangle(pImgRect,rect,cv::Scalar(0,255,0),2); cv::imshow("original image with rectangle",pImgRect); cv::imshow("roi",roi); cv::waitKey();运行结果:
最后一个基本数据类是一种特殊的矩形称为RotatedRect。这个类通过中心点,宽度和高度和旋转角度来表示一个旋转的矩形。
旋转矩形类的构造函数:
RotatedRect(const Point2f& center, const Size2f& size, float angle);
参数:
//RotatedRect cv::Point2f center(100,100); cv::Size2f size(100,50); float angle = 45;// try 10, 30, 45 RotatedRect rRect(center, size, angle); cv::Mat image(200,200,CV_8UC3,cv::Scalar(0)); Point2f vertices[4]; rRect.points(vertices); for (int i = 0; i < 4; i++) line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0)); Rect brect = rRect.boundingRect(); rectangle(image, brect, Scalar(255,0,0)); imshow("rectangles", image); waitKey(0);运行结果:
angle = 10, 30, 45