- 目标
学习用OpenCV画出不同的几何图形
将学习这些功能:cv.line(), cv.circle() , cv.rectangle(), cv.ellipse(), cv.putText() etc.等。
- 代码
在上述所有功能中,您将看到如下所示的一些常见参数:
img:你想要绘制图形的图像
颜色:形状的颜色。对于BGR,将它作为一个元组传递,例如:(255,0,0)为蓝色。对于灰度级,只需传递标量值。
厚度:线或圆的厚度等。如果-1被传递给像圆这样的封闭图形,它将填满形状。默认的厚度= 1
线型:线的类型,无论是8-联通的,反锯齿的线等。默认情况下,它是8-联通的。
cv.LINE_AA提供了反锯齿的线条,这条线看起来很适合曲线。
- 画线
要画一条线,你需要传递直线的起始和结束坐标。我们将创造一个黑色的图像,并在上面画一条蓝色的线,从左上角到右下角。
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(0,255,0),1)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
- 画矩形
要画一个矩形,你需要一个矩形的左上角和右下角。这次我们将在图像的右上角画一个绿色的矩形。
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
- 画圆
要画一个圆,你需要它的中心坐标和半径。我们将在上面画的矩形中画一个圆。
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.circle(img,(250,250), 50, (0,0,255), 1)#(对象,圆心,半径,颜色,填充方式1.-1)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
- 画椭圆
为了绘制椭圆,我们需要传递几个参数。一个参数是中心位置(x,y)。下一个参数是轴长度(主轴长度,小轴长度)。角是反时针方向椭圆旋转的角度。星结和尾角表示从主轴向顺时针方向测量的椭圆弧的开始和结束。也就是说,给出0和360的值给出了完整的椭圆。要了解更多细节,请检查cv.ellipse().的文档。下面的例子在图像的中心画了一个半椭圆
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.ellipse(img,(256,256),(100,50),90,10,360,255,1)#(对象,中心,大小,旋转角度,顺时针,逆时针,填充)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
- 绘制多边形
要画一个多边形,首先你需要顶点的坐标。把这些点变成一个形状ROWSx1x2的数组,其中行是顶点数,它应该是类型int32。这里我们画一个小的多边形,有四个顶点的黄色。
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
pts = np.array([[255,255],[220,230],[270,222],[250,210]], np.int32)
pts = pts.reshape((-1,1,2))
cv.polylines(img,[pts],True,(0,255,255))
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
- 将文本添加到图片:
要将文本放入图像中,您需要指定以下内容。
你想要写的文本数据
位置坐标在你想要的位置(也就是数据开始的左下角)。
字体类型(检查cv.putText()文档以获得支持的字体)
字体大小(指定字体的大小)
普通的东西,如颜色,厚度,线型等等。为了更好的看,llineType = cv.LINE_AA推荐。
我们将用白色来写OpenCV。
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,250), font, 4,(255,255,255),2,cv.LINE_AA)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()
也可以把所有的都写到一起调节不同的参数
import numpy as np
import cv2 as cv
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(511,511),(255,0,0),5)
cv.circle(img,(447,63), 63, (0,0,255), -1)
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
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))
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
cv.imshow('imag',img)
cv.waitKey(0)
cv.destroyAllWindows()