繪圖(line、rectangle、circle、ellipse、polylines、putText)
2017-04-04 23:53
375 查看
OpenCV有函式方便我們繪圖,這邊依序介紹如何繪製線、矩形、圓、橢圓、多角形、文字在影像上,其他圖形請參考OpenCV文件。
內文索引 [隱藏]1 連通類型
2 OpenCV 畫線
3 OpenCV 畫矩形
4 OpenCV 畫圓
5 OpenCV 畫橢圓
6 OpenCV 畫多角形
7 OpenCV 畫文字字串
8 程式碼
連通類型
連通類型有兩種,不同的連通型態會影響圖案的邊界部分:4-連通(4-connectivity):座標(x,y)處的像素 P,其上下左右各有一個鄰近點,其座標分別為:(x+1, y)、(x-1,y)、(x, y+1)、(x, y-1),這樣一組像素稱為P的4近鄰,用 N4(P)表示。
8-連通(8-connectivity):如果除了上式N4(P)的點,加上四個對角鄰近點,(x+1,y+1)、(x+1,y-1)、(x-1,y+1)、(x-1,y-1))加在一起,則稱這八個點為P的8近鄰,用N8(P)表示。
OpenCV 畫線
void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)img:輸入圖,線會畫在上面。
pt1:線的起點。
pt2:線的終點。
color:線的顏色。
thickness:線的厚度。
lineType:通道型態,可輸入8、4、CV_AA: 8->8通道連結。 4->4通道連結。 CV_AA->消除鋸齒(antialiased line),消除顯示器畫面線邊緣的凹凸鋸齒。
OpenCV 畫矩形
void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)img:輸入圖,矩形會畫在上面。
pt1:矩形頂點。
pt2:矩形頂點,pt1的對角邊
color:矩形的顏色。
thickness:矩形的邊線寬度,輸入負值或CV_FILLED代表填滿矩形。
lineType:通道型態,可輸入8、4、CV_AA: 8->8通道連結。 4->4通道連結。 CV_AA->消除鋸齒(antialiased line),消除顯示器畫面線邊緣的凹凸鋸齒。
OpenCV 畫圓
void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)img:輸入圖,圓會畫在上面。
center:圓心。
radius:圓半徑。
color:圓形的顏色。
thickness:圓形的邊線寬度,輸入負值或CV_FILLED代表填滿圓形。
lineType:通道型態,可輸入8、4、CV_AA: 8->8通道連結。 4->4通道連結。 CV_AA->消除鋸齒(antialiased line),消除顯示器畫面線邊緣的凹凸鋸齒。
OpenCV 畫橢圓
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)img:輸入圖,橢圓會畫在上面。
center:圓心。
axes:橢圓軸的尺寸。
angle:旋轉角度,單位角度。
startAngle:橢圓弧度起始角度,單位角度。
endAngle:橢圓弧度結束角度,單位角度。
color:橢圓的顏色。
thickness:橢圓的邊線寬度,輸入負值或CV_FILLED代表填滿橢圓形 。
lineType:通道型態,可輸入8、4、CV_AA: 8->8通道連結。 4->4通道連結。 CV_AA->消除鋸齒(antialiased line),消除顯示器畫面線邊緣的凹凸鋸齒。
OpenCV 畫多角形
void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, intlineType=8, int shift=0)img:輸入圖,多角形會畫在上面。
pts:包含多角形各個曲線點的陣列。
npts:包含多角形各曲線頂點數目的陣列。
ncontours:曲線數。
isClosed:是否為封閉的多角形。
color:多角形的顏色。
thickness:多角形的邊線寬度,輸入負值或CV_FILLED代表填滿多角形。
lineType:通道型態,可輸入8、4、CV_AA: 8->8通道連結。 4->4通道連結。 CV_AA->消除鋸齒(antialiased line),消除顯示器畫面線邊緣的凹凸鋸齒。
OpenCV 畫文字字串
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:輸出字串,OpenCV目前沒有支援中文文字顯現。
org:文字左下角位置。
fontFace:字體樣式。
fontScale:字體大小。
color:字串顏色。
thickness:構成字串的線寬度。
lineType:通道型態,有以下三種可選: 8:8通道連結。 4:4通道連結。 CV_AA:消除鋸齒(antialiased line),消除顯示器畫面橢圓邊緣的凹凸鋸齒。
程式碼
以下程式碼實際繪製線、矩形、圓、橢圓、多角形、文字在影像上:#include <cstdio> #include <opencv2/opencv.hpp> using namespace cv; int main(){ Mat img(400, 400, CV_8UC3, Scalar(255,255,255)); line(img, Point(20,40), Point(120,140), Scalar(255,0,0), 3); rectangle(img, Point(150,40), Point(250,140), Scalar(0,0,255), -1); circle(img, Point(330,90), 50, Scalar(0,255,0), -1); ellipse(img, Point(80,280), Size(60,40), 45, 0, 360, Scalar(255,255,0), 2); Point points[1][5]; points[0][0] = Point(150, 270); points[0][1] = Point(190, 220); points[0][2] = Point(260, 255); points[0][3] = Point(224, 296); points[0][4] = Point(178, 316); const Point* ppt[1] = {points[0]}; int npt[] = {5}; polylines(img, ppt, npt, 1, 1, Scalar(0,255,255),5); putText(img, string("OpenCV"), Point(280,280), 0, 1, Scalar(0,0,0),3); imshow("window", img); waitKey(0); return 0; }
相关文章推荐
- OpenCV Tutorial: 繪圖(line、rectangle、circle、ellipse、polylines、putText)
- 繪圖(line、rectangle、circle、ellipse、polylines、putText)
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- 稳扎稳打Silverlight(7) - 2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
- SVG 形状元素(rect、circle、ellipse、line、polyline、polygon、path)
- ellipse(),circle(),fillPoly(),line()函数介绍
- xfa spec: handedness, border, fill, margin for widgets(line, arc, rectangle, etc)
- CCDrawingPrimitives(使用gl原始方法 绘制Points Line Rect Poly Circle Bezier CatmullRom CardinalSpline。。。。)
- windows phone (14) 简单了解Ellipse元素和Rectangle元素
- LightOJ 1130 Intersection between Circle and Rectangle
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- 写一个程序,定义抽象基类Shape,由它派生出3个派生类,Circle(圆形)、Rectangle(矩形)、Triangle(三角形)。用如下的main()函数,求出定义的几个几何体的面积和。
- Flex4 Spark Ellipse Rect Line Path SolidColorStroke 的简单示例
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
- 定义抽象基类Shape,由它派生出3个派生类,Circle、Rectangle、Triangle
- HTML5 Canvas自定义圆角矩形与虚线(Rounded Rectangle and Dash Line)
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。