OpenCV-Python官方文档中文翻译7:Drawing Functions in OpenCV OpenCV中的绘图功能

Drawing Functions in OpenCV

Goal

Code

In all the above functions, you will see some common arguments as given below:

  • img : The image where you want to draw the shapes
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv.LINE_AA gives anti-aliased line which looks great for curves.

在所有上述功能中,你将看见下面给出的一些常见的参数:

  • img:你想要绘制形状的图片
  • color:形状的颜色。对BGR来说,作为一个元组来传递,例如:(255,0,0)代表蓝色。对于灰度,仅仅传递标量。
  • thickness:线或圆的粗细。如果对闭合图形传递-1,它将填满整个图形。默认厚度为1。
  • lineType:线的类型,是否为8连接线,抗锯齿线等等。默认为8连接线。cv.LINE_AA 给出了抗锯齿线,非常适合曲线。

Drawing Line

To draw a line, you need to pass starting and ending coordinates of line. We will create a black image and draw a blue line on it from top-left to bottom-right corners.

想要画一条线,你需要传递线的开始和结束坐标。我们将创建一个黑色的图片,并从左上角画一条蓝色的线到右下角。

import numpy as np
import cv2 as cv

#Create a black image
img = np.zeros((512,512,3),np.unit8)

#Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(255,0,0),5)

Drawing Rectangle

To draw a rectangle, you need top-left corner and bottom-right corner of rectangle. This time we will draw a green rectangle at the top-right corner of image.

想要画一个矩形,你需要矩阵的左上角和右下角。这次我们将在图片的右上角画一个绿色的矩形。

cv.rectangle(img,(384,0),(510,128),(0,255,0),3)

Drawing Circle

To draw a circle, you need its center coordinates and radius. We will draw a circle inside the rectangle drawn above.

想要画一个圆,你需要圆心坐标和半径。我们将在上面画的矩形的中间画一个圆。

cv.circle(img,(447,63),63,(0,0,255),-1)

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv.ellipse(). Below example draws a half ellipse at the center of the image.

想要画一个椭圆,我们需要传递几个参数。一个参数是中心坐标(x,y)。下一个参数是轴长(主轴长度,短轴长度)。angle是椭圆沿逆时针方向旋转的角度。startAngle和endAngle表示了椭圆弧从主轴沿顺时针方向测量的开始和结束。例如,给值0和360的话将画出完整的椭圆。欲知更多细节,检查 **cv.ellipse()**文档。下面的例子在图片中间画了一个半椭圆。

cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Drawing Polygon

To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are number of vertices and it should be of type int32. Here we draw a small polygon of with four vertices in yellow color.

想要绘制一个多边形,首先你需要顶点的坐标。把这些点放入一个形状为ROWSx1x2的数组中,ROWS是顶点的数量并且它应该是int32类型的。下面我们画一个有四个顶点的黄色小多边形。

pts = np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
pts = pts.reshape((-1,1,2))
cv.polylines(img,[pts],True,(0,255,255))
  • Note

    If third argument is False, you will get a polylines joining all the points, not a closed shape.

    cv.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is a much better and faster way to draw a group of lines than calling cv.line() for each line.

    如果第三个参数是False,你将得到一条连接所有点的折线,而不是一个闭合的形状。

    cv.polylines() 可以用来绘制多条线。只要创建一个你想要画的所有线的列表,并将它传递给函数就可以了。所有的线都将被单独画。和用cv.line() 画每一条线相比,这是一个更好更快的画一组线的方法。

Adding Text to Images:

To put texts in images, you need specify following things.

  • Text data that you want to write
  • Position coordinates of where you want put it (i.e. bottom-left corner where data starts).
  • Font type (Check cv.putText() docs for supported fonts)
  • Font Scale (specifies the size of font)
  • regular things like color, thickness, lineType etc. For better look, lineType = cv.LINE_AA is recommended.

We will write OpenCV on our image in white color.

想要在图片中添加文本,我们需要明确下列的事。

  • 你想写入的文本数据
  • 你想要放置的位置坐标(例如 数据开始于左下角)
  • Font类型(参见**cv.putText()**文档获取支持的Font )
  • 字体规模(特指字体大小)
  • 常规内容,像颜色,粗细度,线类型等等。为了更好的视觉,推荐线条类型为 cv.LINE_AA
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,"OpenCV",(10,500),font,4,(255,255,255),2,cv_LINE_AA)

Result

So it is time to see the final result of our drawing. As you studied in previous articles, display the image to see it.

是时候来看我们画的最后结果了。正如你在前面的文章中所学的,展示图片来看一看。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值