日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV基本绘图

發布時間:2025/4/16 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV基本绘图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

相關函數介紹

Point

該數據結構表示了由其圖像坐標 和 指定的2D點。可定義為:

Point pt;

pt.x = 10;

pt.y = 8;

或者

Point pt =?Point(10, 8);

Scalar

表示了具有4個元素的數組。次類型在OpenCV中被大量用于傳遞像素值。

本節中,我們將進一步用它來表示RGB顏色值(三個參數)。如果用不到第四個參數,則無需定義。

我們來看個例子,如果給出以下顏色參數表達式:

Scalar( a, b, c )

那么定義的RGB顏色值為:Red = c,?Green = b?and?Blue= a

Rectangle

C++:?void rectangle(Mat&?img,Point?pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)

C++:?void rectangle(Mat&?img,Rect?rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )

Parameters:

  • img?– 畫矩形的對象
  • pt1?– 矩形的一個頂點,左上角的.
  • pt2?– 另一個頂點,右下角的.
  • rec?– 確定矩形的另一種方式,給左上角坐標和長寬
  • color?– 指定矩形的顏色或亮度(灰度圖像),scalar(255,0,255)既可指定.
  • thickness?– 矩形邊框的粗細. 負值(like CV_FILLED)表示要畫一個填充的矩形
  • lineType?– 邊框線型. (???

8 (or 0) - 8-connected line(8鄰接)連接 線。

4 - 4-connected line(4鄰接)連接線。

CV_AA - antialiased 線條。)

  • shift?–坐標點的小數點位數

Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)

Parameters:

  • img – 圖像.
  • pt1 – 線條起點.
  • pt2 – 線條終點.
  • color – 線條顏色.
  • thickness – 線條寬度.
  • lineType – 線型

Type of the line:

    • 8 (or omitted) - 8-connected line.
    • 4 - 4-connected line.
    • CV_AA - antialiased line.
  • shift – 坐標點小數點位數.

Circle

C++:?void circle(Mat&img, Point?center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)

Parameters:

  • img?– 要畫圓的那個矩形.
  • center?– 圓心坐標.
  • radius?– 半徑.
  • color?– 圓邊框顏色,scalar類型的
  • thickness?– 正值表示圓邊框寬度. 負值表示畫一個填充圓形
  • lineType?– 圓邊框線型
  • shift?– 圓心坐標和半徑的小數點位數

Ellipse

C++: void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)

C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Parameters:

  • img?– 橢圓所在圖像.
  • center?– 橢圓中心.
  • axes?– 橢圓主軸一半的長度
  • angle?– 橢圓旋轉角度
  • startAngle?– 橢圓弧起始角度
  • endAngle?–橢圓弧終止角度
  • box?– 指定橢圓中心和旋轉角度的信息,通過?RotatedRect?或 CvBox2D. 這表示橢圓畫在旋轉矩形上(矩形是不可見的,只是指定了一個框而已)
  • color?– 橢圓邊框顏色.
  • thickness?– 正值代表橢圓邊框寬度,負值代表填充的橢圓
  • lineType?– 線型
  • shift?– 橢圓中心坐標和坐標軸的小數點位數

PolyLine

C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Parameters:
  • img?– 折線所在圖像.
  • pts?– 折線中拐點坐標指針.
  • npts?– 折線拐點個數指針.
  • ncontours?– 折線線段數量.
  • isClosed?– 折線是否閉合.
  • color?– 折線顏色.
  • thickness?– 折線寬度.
  • lineType?– 線型.
  • shift?– 頂點坐標小數點位數.

PutText

C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

Parameters:
  • img?– 顯示文字所在圖像.
  • text?– 待顯示的文字.
  • org?– 文字在圖像中的左下角 坐標.
  • font?–?字體結構體.
  • fontFace?– 字體類型, 可選擇字體:FONT_HERSHEY_SIMPLEX,?FONT_HERSHEY_PLAIN,?FONT_HERSHEY_DUPLEX,FONT_HERSHEY_COMPLEX,?FONT_HERSHEY_TRIPLEX,?FONT_HERSHEY_COMPLEX_SMALL,?FONT_HERSHEY_SCRIPT_SIMPLEX, orFONT_HERSHEY_SCRIPT_COMPLEX,以上所有類型都可以配合?FONT_HERSHEY_ITALIC使用,產生斜體效果。
  • fontScale?– 字體大小,該值和字體內置大小相乘得到字體大小
  • color?– 文本顏色
  • thickness?– ?寫字的線的粗細,類似于0.38的筆尖和0.5的筆尖
  • lineType?– 線性.
  • bottomLeftOrigin?– true,?圖像數據原點在左下角. Otherwise,?圖像數據原點在左上角.


示例代碼

[cpp]?view plaincopyprint?
  • /**?
  • ?*?@file?Drawing_1.cpp?
  • ?*?@brief?Simple?sample?code?
  • ?*/??
  • ??
  • #include?<opencv2/core/core.hpp>??
  • #include?<opencv2/highgui/highgui.hpp>??
  • ??
  • #define?w?400??
  • ??
  • using?namespace?cv;??
  • ??
  • ///?Function?headers??
  • void?MyEllipse(?Mat?img,?double?angle?);??
  • void?MyFilledCircle(?Mat?img,?Point?center?);??
  • void?MyPolygon(?Mat?img?);??
  • void?MyLine(?Mat?img,?Point?start,?Point?end?);??
  • ??
  • /**?
  • ?*?@function?main?
  • ?*?@brief?Main?function?
  • ?*/??
  • int?main(?void?){??
  • ??
  • ??///?Windows?names??
  • ??char?atom_window[]?=?"Drawing?1:?Atom";??
  • ??char?rook_window[]?=?"Drawing?2:?Rook";??
  • ??
  • ??///?Create?black?empty?images??
  • ??Mat?atom_image?=?Mat::zeros(?w,?w,?CV_8UC3?);??
  • ??Mat?rook_image?=?Mat::zeros(?w,?w,?CV_8UC3?);??
  • ??
  • ??///?1.?Draw?a?simple?atom:??
  • ??///?-----------------------??
  • ??
  • ??///?1.a.?Creating?ellipses??
  • ??MyEllipse(?atom_image,?90?);??
  • ??MyEllipse(?atom_image,?0?);??
  • ??MyEllipse(?atom_image,?45?);??
  • ??MyEllipse(?atom_image,?-45?);??
  • ??
  • ??///?1.b.?Creating?circles??
  • ??MyFilledCircle(?atom_image,?Point(?w/2,?w/2)?);??
  • ??
  • ??///?2.?Draw?a?rook??
  • ??///?------------------??
  • ??
  • ??///?2.a.?Create?a?convex?polygon??
  • ??MyPolygon(?rook_image?);??
  • ??
  • ??///?2.b.?Creating?rectangles??
  • ??rectangle(?rook_image,??
  • ?????????Point(?0,?7*w/8?),??
  • ?????????Point(?w,?w),??
  • ?????????Scalar(?0,?255,?255?),??
  • ?????????-1,??
  • ?????????8?);??
  • ??
  • ??RotatedRect?rRect?=?RotatedRect(Point2f(100,100),?Size2f(100,50),?30);??
  • ??ellipse(rook_image,?rRect,?Scalar(255,255,0));??
  • ??
  • ??///?2.c.?Create?a?few?lines??
  • ??MyLine(?rook_image,?Point(?0,?15*w/16?),?Point(?w,?15*w/16?)?);??
  • ??MyLine(?rook_image,?Point(?w/4,?7*w/8?),?Point(?w/4,?w?)?);??
  • ??MyLine(?rook_image,?Point(?w/2,?7*w/8?),?Point(?w/2,?w?)?);??
  • ??MyLine(?rook_image,?Point(?3*w/4,?7*w/8?),?Point(?3*w/4,?w?)?);??
  • ??
  • ??///?3.?Display?your?stuff!??
  • ??imshow(?atom_window,?atom_image?);??
  • ??moveWindow(?atom_window,?0,?200?);??
  • ??imshow(?rook_window,?rook_image?);??
  • ??moveWindow(?rook_window,?w,?200?);??
  • ??
  • ??waitKey(?0?);??
  • ??return(0);??
  • }??
  • ??
  • ///?Function?Declaration??
  • ??
  • /**?
  • ?*?@function?MyEllipse?
  • ?*?@brief?Draw?a?fixed-size?ellipse?with?different?angles?
  • ?*/??
  • void?MyEllipse(?Mat?img,?double?angle?)??
  • {??
  • ??int?thickness?=?2;??
  • ??int?lineType?=?8;??
  • ??
  • ??ellipse(?img,??
  • ???????Point(?w/2,?w/2?),??
  • ???????Size(?w/4,?w/16?),??
  • ???????angle,??
  • ???????0,??
  • ???????360,??
  • ???????Scalar(?255,?0,?0?),??
  • ???????thickness,??
  • ???????lineType?);??
  • }??
  • ??
  • /**?
  • ?*?@function?MyFilledCircle?
  • ?*?@brief?Draw?a?fixed-size?filled?circle?
  • ?*/??
  • void?MyFilledCircle(?Mat?img,?Point?center?)??
  • {??
  • ??int?thickness?=?-1;??
  • ??int?lineType?=?8;??
  • ??
  • ??circle(?img,??
  • ??????center,??
  • ??????w/32,??
  • ??????Scalar(?0,?0,?255?),??
  • ??????thickness,??
  • ??????lineType?);??
  • }??
  • ??
  • /**?
  • ?*?@function?MyPolygon?
  • ?*?@function?Draw?a?simple?concave?polygon?(rook)?
  • ?*/??
  • void?MyPolygon(?Mat?img?)??
  • {??
  • ??int?lineType?=?8;??
  • ??
  • ??/**?Create?some?points?*/??
  • ??Point?rook_points[1][20];??
  • ??rook_points[0][0]??=?Point(????w/4,???7*w/8?);??
  • ??rook_points[0][1]??=?Point(??3*w/4,???7*w/8?);??
  • ??rook_points[0][2]??=?Point(??3*w/4,??13*w/16?);??
  • ??rook_points[0][3]??=?Point(?11*w/16,?13*w/16?);??
  • ??rook_points[0][4]??=?Point(?19*w/32,??3*w/8?);??
  • ??rook_points[0][5]??=?Point(??3*w/4,???3*w/8?);??
  • ??rook_points[0][6]??=?Point(??3*w/4,?????w/8?);??
  • ??rook_points[0][7]??=?Point(?26*w/40,????w/8?);??
  • ??rook_points[0][8]??=?Point(?26*w/40,????w/4?);??
  • ??rook_points[0][9]??=?Point(?22*w/40,????w/4?);??
  • ??rook_points[0][10]?=?Point(?22*w/40,????w/8?);??
  • ??rook_points[0][11]?=?Point(?18*w/40,????w/8?);??
  • ??rook_points[0][12]?=?Point(?18*w/40,????w/4?);??
  • ??rook_points[0][13]?=?Point(?14*w/40,????w/4?);??
  • ??rook_points[0][14]?=?Point(?14*w/40,????w/8?);??
  • ??rook_points[0][15]?=?Point(????w/4,?????w/8?);??
  • ??rook_points[0][16]?=?Point(????w/4,???3*w/8?);??
  • ??rook_points[0][17]?=?Point(?13*w/32,??3*w/8?);??
  • ??rook_points[0][18]?=?Point(??5*w/16,?13*w/16?);??
  • ??rook_points[0][19]?=?Point(????w/4,??13*w/16?);??
  • ??
  • ??const?Point*?ppt[1]?=?{?rook_points[0]?};??
  • ??int?npt[]?=?{?20?};??
  • ??
  • ??fillPoly(?img,??
  • ????????ppt,??
  • ????????npt,??
  • ????????????1,??
  • ????????Scalar(?255,?255,?255?),??
  • ????????lineType?);??
  • }??
  • ??
  • /**?
  • ?*?@function?MyLine?
  • ?*?@brief?Draw?a?simple?line?
  • ?*/??
  • void?MyLine(?Mat?img,?Point?start,?Point?end?)??
  • {??
  • ??int?thickness?=?2;??
  • ??int?lineType?=?8;??
  • ??line(?img,??
  • ????start,??
  • ????end,??
  • ????Scalar(?0,?0,?0?),??
  • ????thickness,??
  • ????lineType?);??
  • }??

  • 實驗結果


    總結

    以上是生活随笔為你收集整理的OpenCV基本绘图的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。