OpenCV基本繪圖函數(shù)

線段:line 函數(shù)

CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineT ype=8, int shift=0);
  • img: 要繪制線段的圖像。
  • pt1: 線段的起點(diǎn)。
  • pt2: 線段的終點(diǎn)。
  • color: 線段的顏色,通過一個Scalar對象定義。
  • thickness: 線條的寬度。
  • lineType: 線段的類型。可以取值8, 4, 和CV_AA, 分別代表8鄰接連接線,4鄰接連接線和反鋸齒連接線。默認(rèn)值為8鄰接。為了獲得更好地效果可以選用CV_AA(采用了高斯濾波)。
  • shift: 坐標(biāo)點(diǎn)小數(shù)點(diǎn)位數(shù)。

橢圓:ellipse 函數(shù)

CV_EXPORTS_W void ellipse(CV_IN_OUT Mat& img, Point center, Size axes,double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1,int lineType=8, int shift=0);  
  • img: 要繪制橢圓的圖像。
  • center: 橢圓中心點(diǎn)坐標(biāo)。
  • axes: 橢圓位于該Size決定的矩形內(nèi)。(即定義長軸和短軸)。
  • angle: 橢圓旋轉(zhuǎn)角度。
  • startAngle: 橢圓開始繪制時角度。
  • endAngle: 橢圓繪制結(jié)束時角度。(若繪制一個完整的橢圓,則startAngle=0, endAngle = 360)。
  • color: 橢圓的顏色。
  • thickness: 繪制橢圓線粗。負(fù)數(shù)表示全部填充。
  • lineType、shift:同上。

矩形:rectangle 函數(shù)

CV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);  
  • pt1: 矩形的左上角坐標(biāo)。
  • pt2: 矩陣的右下角坐標(biāo)。
  • 其余同上。

圓:circle 函數(shù)

CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0);  
  • center: 圓心坐標(biāo)。
  • radius: 半徑。
  • 其余同上。

填充多邊形:fillPoly 函數(shù)

CV_EXPORTS void fillPoly(Mat& img, const Point** pts,const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() );  
  • pts: 多邊形定點(diǎn)集。
  • npts: 多邊形的頂點(diǎn)數(shù)目。
  • ncontours: 要繪制多邊形的數(shù)量。
  • offset: 所有點(diǎn)輪廓的可選偏移。
  • 其余同上。

顯示文字:PutText 函數(shù)

void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
  • img – 顯示文字所在圖像.
  • text – 待顯示的文字.
  • org – 文字在圖像中的左下角 坐標(biāo).
  • font – 字體結(jié)構(gòu)體.
  • fontFace – 字體類型, 可選擇字體:

FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN,
FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX,
FONT_HERSHEY_TRIPLEX, FONT_HERSHEY_COMPLEX_SMALL,
FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,
以上所有類型都可以配合FONT_HERSHEY_ITALIC使用,產(chǎn)生斜體效果。

  • fontScale – 字體大小,該值和字體內(nèi)置大小相乘得到字體大小
  • color – 文本顏色
  • thickness – 寫字的線的粗細(xì)
  • lineType – 線型.
  • bottomLeftOrigin – true, 圖像數(shù)據(jù)原點(diǎn)在左下角。否則, 圖像數(shù)據(jù)原點(diǎn)在左上角.

示例

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

#define WINDOW_NAME1 "[繪制圖1]"   //為窗口標(biāo)題定義的宏
#define WINDOW_NAME2 "[繪制圖2]"   //為窗口標(biāo)題定義的宏
#define WINDOW_WIDTH 600            //定義窗口大小的宏

//繪制不同角度。相同尺寸的橢圓
void DrawEllipse(Mat img, double angle)
{
    int thickness = 2;
    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);
}

//繪制實(shí)心圓
void DrawFilledCircle(Mat img, Point center)
{
    int thickness = -1; //線粗
    int lineType = 8;
    circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255),
            thickness, lineType);
}

//實(shí)現(xiàn)凹多邊形繪制
void DrawPolygon(Mat img)
{
    int lineType = 8;

    //創(chuàng)建一些點(diǎn)
    Point rootPoints[1][20];
    rootPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
    rootPoints[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
    rootPoints[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][5] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
    rootPoints[0][7] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][8] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][10] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][11] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][12] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][13] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
    rootPoints[0][14] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
    rootPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
    rootPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][17] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
    rootPoints[0][18] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
    rootPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
    const Point* ppt[1] = { rootPoints[0] };
    int npt[] = { 20 };
    fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
}

// 繪制線
void DrawLine(Mat img, Point start, Point end)
{
    int thickness = 2;
    int lineType = 8;
    line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}


int main()
{
    //創(chuàng)建空白的Mat圖像
    Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
    Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

    //----------- 繪制化學(xué)中原子的示例圖-----------
    //1.先繪制出橢圓
    DrawEllipse(atomImage, 90);
    DrawEllipse(atomImage, 0);
    DrawEllipse(atomImage, 45);
    DrawEllipse(atomImage, -45);

    //2.再繪制出圓心
    DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));

    //----------- 繪制組合圖形----------------
    //1.線繪制出多邊形
    DrawPolygon(rookImage);

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

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容