opencv中的数据结构

一、Vec向量模板类

vector(向量):是C++中的一个类,它相当与一个动态数组,当无法知道自己需要的数组大小是,可以使用它节约空间

Vec是opencv中的向量模板类,而opencv中常用的Vec3b,Vec3s

其实看源码可以看到它的定义,它们都是Vec类只是用于不同数据类型:

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<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;

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;

用法:

获取某像素的颜色值:

例如:

cout<<img.at<Vec3b>(row,col)<<endl;//获取点(row,col)的b,g,r值

 

二、Point类

point是opencv中常见的数据结构,支持整型,浮点型等数据类型,常用于表示矩阵中某个点

按照维度主要分为二维(Point2),三维(Point3)两个类型

Point2产生的变种:为Point2i、Point2l、Point2f、Point2d,其中i代表int,l代表int64, f代表float,d代表double,

typedef Point_<int> Point2i;
typedef Point_<int64> Point2l;
typedef Point_<float> Point2f;
typedef Point_<double> Point2d;
typedef Point2i Point;

Point3变种为:Point3i、Point3f、Point3d

typedef Point3_<int> Point3i;
typedef Point3_<float> Point3f;
typedef Point3_<double> Point3d;

用法

获取某像素点的坐标

例如:

    // Point  
        cv::Point pt;  
        pt.x 278;  
        pt.y 269;  
        //或者  
        //cv::Point  pt (278,269);  
        cv::Scalar pix = pImg.at<Vec3b>(pt);  //获取BGR三通道图片在点(278,269)处每个通道的像素值
        cout<<"pix("<<pt.x<<","<<pt.y<<") = "<<pix<<endl;  

 

 

三、Scalar类

Scalar是一个从Vec类引出的模板类,是一个可存放4个元素的向量,广泛用于传递和读取图像中的像素值

cv::Scalar(RGB)

     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");  

 

四、SIZE类

模板类SIZE可标识一幅图像或一个矩形的大小,包含宽、高、面积

    // 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为矩形的模板类,由三个参数定义:矩形左上角坐标(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);   

 

六、Range类

用来指定连续的子序列,比如矩形的一部分

定义为:

    class CV_EXPORTS Range
    {
    public:
    Range();
    Range(int _start, int _end);
    int size() const;
    bool empty() const;
    static Range all();
    operactor CvSlice() const;
           
    int start,end;

    } ; 

例如:

    Mat A=imread("b.jpg",CV_LOAD_IMAGE_COLOR); 
    Mat B=A(Range::all(),Range(1,180));

 

 

参考博客:https://blog.csdn.net/weixin_42730667/article/details/104099293

    https://www.likecs.com/show-204163147.html#sc=1296

posted @ 2022-05-24 15:46  楸壳  阅读(169)  评论(0编辑  收藏  举报