# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.
import sensor, image, time, math ,pyb
from pyb import UART
from pyb import Pin
import json
uart = UART(3, 19200)
led1 = pyb.LED(1)
led2 = pyb.LED(2)
led3 = pyb.LED(3)
# 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 = [(42,100, 29, 65, 12, 50), # generic_red_thresholds28, 89, -52, -2, -8, 50
(42, 65, -47, -11, 10, 41), # generic_green_thresholds 31, 86, -3, 20, -49, 14
(19, 53, -1, 27, -50, 0)] # generic_blue_thresholds 30, 66, 44, 80, 26, 69
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.
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
ROI=(0,20,100,200)
clock = time.clock()
p_out = Pin('P7', Pin.OUT_PP)#设置p_out为输出引脚
p_out.high()#设置p_out引脚为高
p_out.high()#设置p_out引脚为高
# 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.
a=0
b=1
c=2
d=4
n=0
classmates = []
while(True):
clock.tick()
#led1.on()
#led2.on()
#led3.on()
img = sensor.snapshot()
for blob in img.find_blobs(thresholds,roi=ROI,pixels_threshold=200, area_threshold=200):
# These values depend on the blob not being circular - otherwise they will be shaky.
statistics = img.get_statistics(roi=ROI)
if a<1:
if 42<statistics.l_mode()<100 and 29<statistics.a_mode()<65 and 12<statistics.b_mode()<50:#if the circle is red
img.draw_rectangle(blob.rect(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
#55, 40, 29, 65, 12, 50
#a=a+1
#print(1) 45, 72, 19, 71, -14, 44
a+=1
#a=a+1
print ('a=',a)
classmates.append(a)
if b<2:
if 42<statistics.l_mode()<65 and -47<statistics.a_mode()<-11 and 10<statistics.b_mode()<41:#if the circle is red
img.draw_rectangle(blob.rect(), color = (0, 255,0))#识别到的红色圆形用红色的圆框出来
#40, 66, -22, -2, -6, 27
b+=1
print ('b=',b)
classmates.append(b)
if c<3:
if 19<statistics.l_mode()<53 and -1<statistics.a_mode()<27 and -50<statistics.b_mode()<0:#if the circle is red
img.draw_rectangle(blob.rect(), color = (0, 0, 255))#识别到的红色圆形用红色的圆框出来
#19, 36, -3, 18, -23, -4
c+=1
print ('c=',c)
classmates.append(c)
if n<a<b<c:
print (classmates)
output_str = json.dumps(classmates)
uart.write(output_str)
m=len(classmates)
if m>2:
n=1

4
46nr 发布的最佳帖子
-
为什么摄像头拍摄到的颜色跟设置的阈值能够对应,但是就是识别不了呢,或者说是不灵敏 有时能识别到,有时又不能