OpenCV学习之旅3——OpenCV数据结构与基本绘图

本文介绍了OpenCV中基础图像容器Mat的使用方法,并通过实例展示了如何利用Mat创建单位矩阵和随机像素矩阵,同时介绍了Point、Rect等常用数据结构。此外,还详细讲解了如何使用OpenCV进行基本的绘图操作,包括绘制椭圆、实心圆、多边形及线段。
摘要由CSDN通过智能技术生成

1. 基础图像容器Mat

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;

//--------------------------------------【main( )函数】-----------------------------------------
//          描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main(int,char**)
{
    //改变控制台的前景色和背景色
    system("color 8F"); 

    Mat I = Mat::eye(4, 4, CV_64F);
    I.at<double>(1,1) = CV_PI;
    cout << "\nI = " << I << ";\n" << endl;

    Mat r = Mat(10, 3, CV_8UC3);
    randu(r, Scalar::all(0), Scalar::all(255));

    cout << "r (OpenCV默认风格) = " << r << ";" << endl << endl;
    cout << "r (Python风格) = " << format(r,"python") << ";" << endl << endl;
    cout << "r (Numpy风格) = " << format(r,"numpy") << ";" << endl << endl;
    cout << "r (逗号分隔风格) = " << format(r,"csv") << ";" << endl<< endl;
    cout << "r (C语言风格) = " << format(r,"C") << ";" << endl << endl;

    Point2f p(6, 2);
    cout << "【2维点】p = " << p << ";\n" << endl;

    Point3f p3f(8, 2, 0);
    cout << "【3维点】p3f = " << p3f << ";\n" << endl;

    vector<float> v;
    v.push_back(3);
    v.push_back(5);
    v.push_back(7);

    cout << "【基于Mat的vector】shortvec = " << Mat(v) << ";\n"<<endl;

    vector<Point2f> points(20);
    for (size_t i = 0; i < points.size(); ++i)
        points[i] = Point2f((float)(i * 5), (float)(i % 7));

    cout << "【二维点向量】points = " << points<<";";

    getchar();//按任意键退出

    return 0;


}

1.1 Mat::eye()

static MatExpr eye(int rows, int cols, int type);

生成rows行cols列的单位矩阵,CV_64F表示数据类型,即64位Float型数据。

1.2 randu()

void randu(InputOutputArray dst, InputArray low, InputArray high);

将dst赋随机的low到high的值,CV_8UC3表示三通道八位(最大255)无符字符型数据。Scalar::all(0)即Scall(0,0,0)简写,即黑色;Scall::all(255)表示白色。

1.3 其他常用数据结构

1.3.1 Point类

Point类定义了二维坐标系下的点如:Point p; p.x=10; p.y=8;Point p=Point(10,8)。Point2f即Point,Point3f则定义了三维点。

1.3.2 Rect类

Rect成员变量有x、y、width、height分别为左上角坐标与矩形的宽和高。如果想求两个矩形的交集和并集可以用

Rect rect=rect1&rect2;
Rect rect=rect1|rect2;

2. 基本绘图工具

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

//OpenCV需加入头文件:
//#include <opencv2/imgproc/imgproc.hpp>

//-----------------------------------【宏定义部分】-------------------------------------------- 
//      描述:定义一些辅助宏 
//------------------------------------------------------------------------------------------------ 
#define WINDOW_NAME1 "【绘制图1】"        //为窗口标题定义的宏 
#define WINDOW_NAME2 "【绘制图2】"        //为窗口标题定义的宏 
#define WINDOW_WIDTH 600//定义窗口大小的宏

//--------------------------------【全局函数声明部分】-------------------------------------
//      描述:全局函数声明
//-----------------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle );//绘制椭圆
void DrawFilledCircle( Mat img, Point center );//绘制圆
void DrawPolygon( Mat img );//绘制多边形
void DrawLine( Mat img, Point start, Point end );//绘制线段

//---------------------------------------【main( )函数】--------------------------------------
//      描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( void )
{

    // 创建空白的Mat图像
    Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
    Mat rookImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );

    ShowHelpText();
    // ---------------------<1>绘制化学中的原子示例图------------------------

    //【1.1】先绘制出椭圆
    DrawEllipse( atomImage, 90 );
    DrawEllipse( atomImage, 0 );
    DrawEllipse( atomImage, 45 );
    DrawEllipse( atomImage, -45 );

    //【1.2】再绘制圆心
    DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );

    // ----------------------------<2>绘制组合图-----------------------------
    //【2.1】先绘制出椭圆
    DrawPolygon( rookImage );

    // 【2.2】绘制矩形
    rectangle( rookImage,
        Point( 0, 7*WINDOW_WIDTH/8 ),
        Point( WINDOW_WIDTH, WINDOW_WIDTH),
        Scalar( 0, 255, 255 ),
        -1,
        8 );

    // 【2.3】绘制一些线段
    DrawLine( rookImage, Point( 0, 15*WINDOW_WIDTH/16 ), Point( WINDOW_WIDTH, 15*WINDOW_WIDTH/16 ) );
    DrawLine( rookImage, Point( WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/4, WINDOW_WIDTH ) );
    DrawLine( rookImage, Point( WINDOW_WIDTH/2, 7*WINDOW_WIDTH/8 ), Point( WINDOW_WIDTH/2, WINDOW_WIDTH ) );
    DrawLine( rookImage, Point( 3*WINDOW_WIDTH/4, 7*WINDOW_WIDTH/8 ), Point( 3*WINDOW_WIDTH/4, WINDOW_WIDTH ) );

    // ---------------------------<3>显示绘制出的图像------------------------
    imshow( WINDOW_NAME1, atomImage );
    moveWindow( WINDOW_NAME1, 0, 200 );
    imshow( WINDOW_NAME2, rookImage );
    moveWindow( WINDOW_NAME2, WINDOW_WIDTH, 200 );

    waitKey( 0 );
    return(0);
}



//-------------------------------【DrawEllipse( )函数】--------------------------------
//      描述:自定义的绘制函数,实现了绘制不同角度、相同尺寸的椭圆
//-----------------------------------------------------------------------------------------
void DrawEllipse( Mat img, double angle )
{
    int thickness = 1;
    int lineType = 8;

    ellipse( img,
        Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ),
        Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ),
        angle,
        0,
        360,
        Scalar( 255, 129, 0 ),
        thickness,
        lineType 
        );
}


//-----------------------------------【DrawFilledCircle( )函数】---------------------------
//      描述:自定义的绘制函数,实现了实心圆的绘制
//-----------------------------------------------------------------------------------------
void DrawFilledCircle(Mat img, Point center)
{
    int thickness = -1;
    int lineType = 8;

    circle( img,
        center,
        WINDOW_WIDTH/32,
        Scalar( 0, 0, 255 ),
        thickness,
        lineType );
}


//-----------------------------------【DrawPolygon( )函数】--------------------------
//      描述:自定义的绘制函数,实现了凹多边形的绘制
//--------------------------------------------------------------------------------------
void DrawPolygon( Mat img )
{
    int lineType = 8;

    //创建一些点
    Point rookPoints[1][20];
    rookPoints[0][0]  = Point(    WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
    rookPoints[0][1]  = Point(  3*WINDOW_WIDTH/4,   7*WINDOW_WIDTH/8 );
    rookPoints[0][2]  = Point(  3*WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );
    rookPoints[0][3]  = Point( 11*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
    rookPoints[0][4]  = Point( 19*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
    rookPoints[0][5]  = Point(  3*WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
    rookPoints[0][6]  = Point(  3*WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
    rookPoints[0][7]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
    rookPoints[0][8]  = Point( 26*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
    rookPoints[0][9]  = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
    rookPoints[0][10] = Point( 22*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
    rookPoints[0][11] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
    rookPoints[0][12] = Point( 18*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
    rookPoints[0][13] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/4 );
    rookPoints[0][14] = Point( 14*WINDOW_WIDTH/40,    WINDOW_WIDTH/8 );
    rookPoints[0][15] = Point(    WINDOW_WIDTH/4,     WINDOW_WIDTH/8 );
    rookPoints[0][16] = Point(    WINDOW_WIDTH/4,   3*WINDOW_WIDTH/8 );
    rookPoints[0][17] = Point( 13*WINDOW_WIDTH/32,  3*WINDOW_WIDTH/8 );
    rookPoints[0][18] = Point(  5*WINDOW_WIDTH/16, 13*WINDOW_WIDTH/16 );
    rookPoints[0][19] = Point(    WINDOW_WIDTH/4,  13*WINDOW_WIDTH/16 );

    const Point* ppt[1] = { rookPoints[0] };
    int npt[] = { 20 };

    fillPoly( img,
        ppt,
        npt,
        1,
        Scalar( 255, 255, 255 ),
        lineType );
}


//-----------------------------------【DrawLine( )函数】--------------------------
//      描述:自定义的绘制函数,实现了线的绘制
//---------------------------------------------------------------------------------
void DrawLine( Mat img, Point start, Point end )
{
    int thickness = 2;
    int lineType = 8;
    line( img,
        start,
        end,
        Scalar( 0, 0, 0 ),
        thickness,
        lineType );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值