画线

  • image.draw_line(line_tuple, color=White) 在图像中画一条直线。
    • line_tuple的格式是(x0, y0, x1, y1),意思是(x0, y0)到(x1, y1)的直线。
    • 颜色可以是灰度值(0-255),或者是彩色值(r, g, b)的tupple。默认是白色

画框

  • image.draw_rectangle(rect_tuple, color=White) 在图像中画一个矩形框。
    • rect_tuple 的格式是 (x, y, w, h)。

画圆

  • image.draw_circle(x, y, radius, color=White) 在图像中画一个圆。
    • x,y是圆心坐标
    • radius是圆的半径

画十字

  • image.draw_cross(x, y, size=5, color=White) 在图像中画一个十字
    • x,y是坐标
    • size是两侧的尺寸

写字

  • image.draw_string(x, y, text, color=White) 在图像中写字 8x10的像素
    • x,y是坐标。使用\n, \r, and \r\n会使光标移动到下一行。
    • text是要写的字符串。

圆十字瞄准镜例程(qvga类型摄像头)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


import sensor, image, time

sensor.reset() # 初始化摄像头
sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565.
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10) # 跳过10帧,使新设置生效
sensor.set_hmirror(True)
sensor.set_vflip(True)
while(True):
img = sensor.snapshot() # Take a picture and return the image.
img.draw_line((155, 115, 160, 120),color=(155,255,255))
img.draw_line((160, 120, 165, 115), color=(255,0,0))
img.draw_rectangle((145, 105, 30, 30), color=(255,0,0))
img.draw_circle(160, 120, 15)
img.draw_cross(160,120,size=15)
img.draw_string(140,135, "Target")

效果(我学习用的摄像头垃圾,见谅)

1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#引入感光元件的模块

import sensor, image, time

sensor.reset() # 初始化元器件
sensor.set_pixformat(sensor.RGB565) # 设置为彩色
#设置翻转
#水平方向翻转
sensor.set_hmirror(True)
sensor.set_vflip(True)
#垂直方向翻转

#设置图像大小
sensor.set_framesize(sensor.QVGA)


sensor.skip_frames(time = 2000) # #跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。
clock = time.clock() # Create a clock object to track the FPS.

sensor.set_auto_gain(False) #自动增益开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动增益。

sensor.set_auto_whitebal(False) #自动白平衡开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动白平衡。

while(True):
img = sensor.snapshot() # Take a picture and return the image.
img.draw_line((145, 105, 160, 120))
img.draw_line((175, 105, 160, 120), color=(255,0,0))
img.draw_rectangle((145, 105, 30, 30), color=(0,0,0))
img.draw_circle(160, 120, 15)
img.draw_cross(160,120,size=15)
img.draw_string(140,140, "Target!")


还有这种的,需要其他样式的话,根据代码和注释魔改即可。

2