1、单颜色识别实验
大约 3 分钟
例程代码
import sensor, image, time, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
print("Hold the object you want to track in front of the camera in the box.")
print("MAKE SURE THE COLOR OF THE OBJECT YOU WANT TO TRACK IS FULLY ENCLOSED BY THE BOX!")
# Capture the color thresholds for whatever was in the center of the image.
# 50x50 center of QVGA.
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50]
for i in range(50):
img = sensor.snapshot()
img.draw_rectangle(r)
lcd.display(img)
print("Learning thresholds...")
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(50):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r, color=(0,255,0))
lcd.display(img)
print("Thresholds learned...")
print("Start Color Recognition...")
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
lcd.display(img)
print(clock.fps())
实验准备
- 通过usb线将K210与电脑连接。
- 准备一种颜色较为鲜明的物品。
- 打开CanMV IDE,执行上面的例程代码。
实验结果
- 运行之后,等待系统初始化完成后, LCD显示摄像头画面,并且屏幕中间有一个白色的方框,请将要识别的颜色放 到白色方框内,白色方框持续时间大约3秒。
- 等到白色方框变为绿色方框时,此时系统开始学习绿色方框内的颜色LAB值,还会出现其他白色的方框作为预览效果,大约5秒后,绿色方框消失,则表示学习完成。
- 之后摄像头画面中再出现类似的颜色,会自动用白框框出。
例程代码讲解
- 导入相关库,并初始化摄像头和LCD显示屏。
import sensor, image, time, lcd
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()
- 摄像头画面中绘制一个50*50大小的白色方框,此时的功能为提示用户将要识别的颜色放入方框内。
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50]
for i in range(50):
img = sensor.snapshot()
img.draw_rectangle(r)
lcd.display(img)
- 等到方框由白色变成绿色,则开始学习颜色的LAB值,多次读取数值,并取平均值作为学习的LAB值结果。
print("Learning thresholds...")
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(50):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r, color=(0,255,0))
lcd.display(img)
- 当颜色学习完成,新建一个while循环,开始识别摄像头画面中的颜色,分析与上一步学习到的颜色的LAB值是否相符合,如果符合则框出来对应的颜色块。
print("Thresholds learned...")
print("Start Color Recognition...")
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
lcd.display(img)
print(clock.fps())