opencv for python 之 创建图片绘制简单几何图形
#create a image and test draw it
创建一张图片,需要定义图片各个属性,包括大小,图片像素类型(每个像素点用多少bits表示),通道数3(rgb)
import cv2.cv as cv
width = 200
height = 200
no_of_bits = 8
channels = 3
image = cv.CreateImage((width,height), no_of_bits, channels)
创建显示窗口
win_name = "test"
cv.NamedWindow("test",cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage(win_name, image)
画一条直线,只需定义两个顶点坐标和线的宽度以及颜色
线的属性有颜色 , 宽度, 线类型(当线的宽度为-1时,矩形与圆形将是实心,及颜色填充)
#draw a line
x = 10
y = 10
start_point = (x,y)
x1 = 100
y1 = 100
end_point = (x1,y1)
#line attribute
color = (0,255,0)
line_width = 4
line_type = 8
cv.Line(image, start_point, end_point, color, line_width, line_type)
画一个矩形,只需定义矩形的左上顶点和右下顶点位置,以及线的颜色和宽度
x = 30
y = 30
rect_start = (x,y)
x1 = 90
y1 = 90
rect_end = (x1,y1)
cv.Rectangle(image, rect_start, rect_end, color, 1, 0)
画一个圆,只需定义圆的中心点坐标和圆的半径,以及线的颜色和宽度
x = 100
y = 100
radius = 60
circle_center = (x,y)
cv.Circle(image, circle_center, radius, color)
cv.ShowImage(win_name, image)
cv.WaitKey()
示例运行结果