颜色识别
单颜色彩色识别
# Single Color RGB565 Blob Tracking Example单色RGB565斑点跟踪示例
#
# This example shows off single color RGB565 tracking using the OpenMV Cam.这个例子展示了使用OpenMV Cam的单色RGB565跟踪。
import sensor, image, time#导入此代码所依赖的模块
threshold_index = 1 # 0 for red, 1 for green, 2 for blue
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green/blue things. You may wish to tune them...
#设置所要识别的颜色的预置
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
(48, 83, -128, 23, 15, 59), # generic_green_thresholds
(0, 30, 0, 64, -128, 0)] # generic_blue_thresholds
sensor.reset()#重置感光元件、摄像机
sensor.set_pixformat(sensor.RGB565)#设置颜色格式
sensor.set_framesize(sensor.QVGA)#设置图像大小
sensor.skip_frames(time = 2000)#跳过前几帧,等待前两个设置生效
#图像识别时必须关闭白平衡和自动增益,因为他们会影响颜色阈值
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()#返回一个时钟对象。
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are只有像素大于“pixel_threshold”和面积大于“area_threshold”的blobs才是
# 由下面的“find_blobs”返回。修改“pixels_threshold”和“area_threshold”即可
# camera resolution. "merge=True" merges all overlapping blobs in the image.相机的分辨率。“merge=True”合并图像中所有重叠的斑点。
while(True):
clock.tick()#开始追踪运行时间。
img = sensor.snapshot()#先截感光元件的一张照片
#for...in循环,依次把list或tuple中的每个元素迭代出来
#用for循环,把所有符合条件的色块找一遍
for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True):
# These values are stable all the time.这些值一直是稳定的
img.draw_rectangle(blob.rect())#给色块画一个外框
img.draw_cross(blob.cx(), blob.cy())#在色块外框的中心画一个+
print(blob.cx(), blob.cy())
print(clock.fps())#打印帧率
相关知识点
多颜色彩色识别
# 多颜色斑点跟踪示例
#
# 这个例子展示了使用OpenMV摄像头的多色斑点跟踪。
import sensor, image, time
# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
(30, 100, -64, -8, -32, 32), # generic_green_thresholds
(0, 15, 0, 40, -80, -20)] # generic_blue_thresholds
# 您可以超过16个以上的阈值。然而,在颜色阈值开始严重重叠之前,用16个阈值分割任何场景是不可能的。
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.
#不要设置"merge=True"因为那会合并我们不想要的blobs。
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs(thresholds,merge=False, pixels_threshold=200, area_threshold=200):
# These values are stable all the time.
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(blob.code())
#print(clock.fps())