OpenCV 提供许多绘图功能来绘制几何形状和在图像上书写文本。让我们看看一些绘图功能,并使用OpenCV在图像上绘制几何形状。
一些绘图功能包括:
cv2。行(): 用于在图像上画线。 cv2。矩形() 用于在图像上绘制矩形。 cv2。圆圈() 用于在图像上画圆圈。 cv2。putText(): 用于在图像上书写文本。
为了演示上述功能的使用,我们需要一个大小为400 X 400的图像,图像中填充纯色(本例中为黑色)。为了做到这一点,我们可以利用 努比。零 函数创建所需的图像。
Python3
# Python3 program to draw solid-colored # image using numpy.zeroes() function import numpy as np import cv2 # Creating a black image with 3 channels # RGB and unsigned int datatype img = np.zeros(( 400 , 400 , 3 ), dtype = "uint8" ) cv2.imshow( 'dark' , img) # Allows us to see image # until closed forcefully cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
现在,让我们在这张纯黑的图片上画一些几何形状。
划一条线:
cv2。线条(imageObjectName、(’start_coordinates’)、(’end_coordinates’)、(’color_in_bgr’)、’line_thickness’)
Python3
# Python3 program to draw line # shape on solid image import numpy as np import cv2 # Creating a black image with 3 channels # RGB and unsigned int datatype img = np.zeros(( 400 , 400 , 3 ), dtype = "uint8" ) # Creating line cv2.line(img, ( 20 , 160 ), ( 100 , 160 ), ( 0 , 0 , 255 ), 10 ) cv2.imshow( 'dark' , img) # Allows us to see image # until closed forcefully cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
画一个矩形:
cv2。矩形(imageObjectName,(’top_left_vertex_coordinates’),(’lower_right_vertex_coordinates’),(’stroke_color_in_bgr’),’stroke_thickness’)
Python3
# Python3 program to draw rectangle # shape on solid image import numpy as np import cv2 # Creating a black image with 3 # channels RGB and unsigned int datatype img = np.zeros(( 400 , 400 , 3 ), dtype = "uint8" ) # Creating rectangle cv2.rectangle(img, ( 30 , 30 ), ( 300 , 200 ), ( 0 , 255 , 0 ), 5 ) cv2.imshow( 'dark' , img) # Allows us to see image # until closed forcefully cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
画一个圆圈:
cv2。圆(imageObjectName、(’center_coordinates’)、(’circle_radius’)、(’color_in_bgr’)、’stroke_thickness’)
Python3
# Python3 program to draw circle # shape on solid image import numpy as np import cv2 # Creating a black image with 3 # channels RGB and unsigned int datatype img = np.zeros(( 400 , 400 , 3 ), dtype = "uint8" ) # Creating circle cv2.circle(img, ( 200 , 200 ), 80 , ( 255 , 0 , 0 ), 3 ) cv2.imshow( 'dark' , img) # Allows us to see image # until closed forcefully cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
书写文本:
cv2。putText(imageObjectName,’TextContent’,(’text_起点_坐标’),’fontToBeUsed’,’fontToBeUsed’,’font_大小’,(’text_颜色’,’text_厚度’,’line_类型’)
python
# Python3 program to write # text on solid image import numpy as np import cv2 # Creating a black image with 3 # channels RGB and unsigned int datatype img = np.zeros(( 400 , 400 , 3 ), dtype = "uint8" ) # writing text font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, 'GeeksForGeeks' , ( 50 , 50 ), font, 0.8 , ( 0 , 255 , 0 ), 2 , cv2.LINE_AA) cv2.imshow( 'dark' , img) # Allows us to see image # until closed forcefully cv2.waitKey( 0 ) cv2.destroyAllWindows() |
输出:
在图像上绘制形状的应用:
- 绘制几何形状可以帮助我们突出图像的特定部分。
- 像直线这样的几何形状可以帮助我们指出或识别图像中的特定区域。
- 在图像的某些区域写入文本可以为该区域添加描述。
参考: https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html