>>> 星瞳科技|OpenMV中国官方网站 <<<

>>> 星瞳科技店铺地址|OpenMV中国官方代理,全球九大代理商之一 <<<

>>> OpenMV3 Cam M7 官方高配版上市啦~戳我戳我戳我~ <<<

>>> 星瞳科技-OpenMV中文教程网 <<<

本例程为03-Drawing-color_drawing.py
本例程讲述如何用openmv实现在图像里画线。

# Color Drawing Example
#
# This example shows off your OpenMV Cam's built-in drawing capabilities. This
# example was originally a test but serves as good reference code. Please put
# your IDE into non-JPEG mode to see the best drawing quality.

import sensor, image, time #引入程序依赖的模块
sensor.reset()#摄像头初始化
sensor.set_framesize(sensor.QVGA)#设置图像像素大小为QVGA 320x120

# All drawing functions use the same code to pass color.
# So we just need to test one function.

while(True):

    # Test Draw Line (GRAYSCALE)
    sensor.set_pixformat(sensor.GRAYSCALE)#设置图像颜色格式为灰度图
    for i in range(10):#循环变量i从0到9,共计10个数循环
        img = sensor.snapshot()#每次循环截取一张图像
    for i in range(img.width()):#img.width=320(即QVGA图像格式的宽度),
    #所以从0到319循环
        c = ((i * 255) + (img.width()/2)) / img.width()
        img.draw_line((i, 0, i, img.height()-1), color = int(c))
        #画线函数img.draw_line((x0, y0, x1, y1), color=White),从(x0,y0)
        #到(x1,y1)画一条直线。如果是灰度图,color是0-255的一个数,0代表黑,
        #255代表白;如果是RGB图像,color是(r,g,b)的一个元组,r g b分别代表红绿蓝。
    sensor.snapshot()
    time.sleep(1000)

    # Test Draw Line (RGB565)
    sensor.set_pixformat(sensor.RGB565)#设置图像颜色格式为rgb图
    for i in range(10):
        img = sensor.snapshot()
    for i in range(img.width()):
        c = ((i * 255) + (img.width()/2)) / img.width()
        img.draw_line((i, 0, i, img.height()-1), color = [int(c), 0, 0])
        #因为是rgb图,所以color = [int(c), 0, 0],而不是一个单纯的0-255的数字。
        #注意python的元组类型一定要用[ ]或者()括起来,否则会报错。
    sensor.snapshot()
    time.sleep(1000)

    # Test Draw Line (RGB565)
    sensor.set_pixformat(sensor.RGB565)
    for i in range(10):
        img = sensor.snapshot()
    for i in range(img.width()):
        c = ((i * 255) + (img.width()/2)) / img.width()
        img.draw_line([i, 0, i, img.height()-1], color = [0, int(c), 0])
    sensor.snapshot()
    time.sleep(1000)

    # Test Draw Line (RGB565)
    sensor.set_pixformat(sensor.RGB565)
    for i in range(10):
        img = sensor.snapshot()
    for i in range(img.width()):
        c = ((i * 255) + (img.width()/2)) / img.width()
        img.draw_line([i, 0, i, img.height()-1], color = [0, 0, int(c)])
    sensor.snapshot()
    time.sleep(1000)

运行程序,发现灰、红、绿、蓝循环变色: