OpenCV 基本圖形繪制函數詳解

  • 用於繪制直線的line函數;
  • 用於繪制橢圓的ellipse函數;
  • 用於繪制矩形的rectangle函數;
  • 用於繪制圓的circle函數;
  • 用於繪制填充的多邊形的fillPoly函數。
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

#define WINDOW_WIDTH 600 // 定義窗口大小的宏
#define WINDOW_NAME1 "原子圖"        //為窗口標題定義的宏 
#define WINDOW_NAME2 "多邊形圖以及畫線"        //為窗口標題定義的宏 

/***繪制橢圓***/
void DrawEllipse(Mat img, double angle)
{
    int thickness = 2; //  線寬
    int lineType = 8; // 線性,8 代表聯通線性

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

/*繪制實心圓*/
void DrawFilledCircle(Mat img, Point center)
    int thickness = -1; //  線粗-1,代表實心

    circle(img,
        center,
        WINDOW_WIDTH / 32,
        Scalar(0, 0, 255),

/*凹多邊形繪制*/
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),  // 多邊形顏色:白色

/*線繪制*/
void DrawLine(Mat img, Point start, Point end)
    int thickness = 2;
    line(img,
        start,
        end,
        Scalar(0,0,0), // 黑色


int main(int argc, char ** argv)
    Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
    Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

    // 橢圓繪制
    DrawEllipse(atomImage, 0);
    DrawEllipse(atomImage, 90);
    DrawEllipse(atomImage, 45);
    DrawEllipse(atomImage, -45);
    // 繪制圓心
    DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));


    // 繪制多邊形
    DrawPolygon(rookImage);

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

    // 繪制一些線段
    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);

運行:

參考:

《OpenCV3 編程入門》 毛星雲 P104

到此這篇關於OpenCV 基本圖形繪制函數的文章就介紹到這瞭,更多相關OpenCV 基本圖形函數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: