色块识别,并通过面积剔除(黑白球效果不好)
import sensor, image, time, math
from pyb import UART
import ustruct
# 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...
black_thresholds = [(0, 27, -128, 13, -128, 9)] # generic_red_thresholds -> index is 0 so code == (1 << 0)
red_thresholds = [(0, 100, 29, 127, -128, 18)]
whrite_thresholds = [(0, 96, 127, -128, -128, 125)] # generic_green_thresholds -> index is 1 so code == (1 << 1)
# Codes are or'ed together when "merge=True" for "find_blobs".
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
def sending_data(cx,cy):
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data=ustruct.pack("b",0x0a)
datasum=data
print("sentData:",cx)
uart.write(datasum); #必须要传入一个字节数组
def recive_data():
global uart
if uart.any():
tmp_data = uart.readline();
print(tmp_data)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_vflip(0)
sensor.set_hmirror(False)
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. "merge=True" must be set to merge overlapping color blobs for color codes.
while(True):
#clock.tick()
img = sensor.snapshot()
max_size = 0
for blob in img.find_blobs(red_thresholds, pixels_threshold=100, area_threshold=100, merge=True):
if blob.code() == 1: # r/g code == (1 << 1) | (1 << 0)
if max_size < blob.pixels() and 500<blob.pixels()<1200:
max_blob = blob
max_size = blob.pixels()
print(max_size)
if max_size!=0:
img.draw_edges(max_blob.min_corners(), color=(255,0,0))
# These values are stable all the time.
# Note - the blob rotation is unique to 0-180 only.
sending_data(max_blob.cx(), max_blob.cy())
max_size = 0
for blob in img.find_blobs(black_thresholds, pixels_threshold=100, area_threshold=100, merge=True):
if blob.code() == 1: # r/g code == (1 << 1) | (1 << 0)
if max_size < blob.pixels() and 500<blob.pixels()<800:
max_blob = blob
max_size = blob.pixels()
if max_size!=0:
img.draw_edges(max_blob.min_corners(), color=(0,255,0))
# These values are stable all the time.
# Note - the blob rotation is unique to 0-180 only.
sending_data(max_blob.cx(), max_blob.cy())
max_size = 0
for blob in img.find_blobs(whrite_thresholds, pixels_threshold=100, area_threshold=100, merge=True):
if blob.code() == 1: # r/g code == (1 << 1) | (1 << 0)
if max_size < blob.pixels() and 500<blob.pixels()<800:
max_blob = blob
max_size = blob.pixels()
if max_size!=0:
img.draw_edges(max_blob.min_corners(), color=(0,0,255))
# These values are stable all the time.
sending_data(max_blob.cx(), max_blob.cy())
#recive_data()
#print(clock.fps())
先找圆再分辨颜色
红色小球找圆函数开始找不到,后来加一个直方图均衡就可以了
这个比较成功,与32通信的数据格式也用这个里面的
import sensor, image, time, math
from pyb import UART
import ustruct
# 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...
black_thresholds = [(4, 15, -128, 127, -17, 6)] # generic_red_thresholds -> index is 0 so code == (1 << 0)
red_thresholds = [(0, 100, -128, 34, -128, 127)]
whrite_thresholds = [(71, 100, -5, 23, -128, 127)] # generic_green_thresholds -> index is 1 so code == (1 << 1)
thresholds = (90, 100, -128, 127, -128, 127)
# Codes are or'ed together when "merge=True" for "find_blobs".
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
def sending_data(color,cx,cy):
global uart
data0=ustruct.pack("b",0x0a)
data=ustruct.pack("b",color)
data1=ustruct.pack("b",int(cx)>>8)
data2=ustruct.pack("b",int(cx)&0xFF)
data3=ustruct.pack("b",int(cy)>>8)
data4=ustruct.pack("b",int(cy)&0xFF)
data5=ustruct.pack("b",0x0b)
datasum=data0+data+data1+data2+data3+data4+data5
uart.write(datasum)
print(int(cx)&0xFF,int(cy)&0xFF,int(cx)>>8,int(cy)>>8)
def recive_data():
global uart
if uart.any():
tmp_data = uart.readline();
print(tmp_data)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_vflip(0)
sensor.set_hmirror(False)
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. "merge=True" must be set to merge overlapping color blobs for color codes.
while(True):
#clock.tick()
img = sensor.snapshot().histeq(adaptive=True, clip_limit=3)
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
statistics = img.get_statistics(roi=area)#像素颜色统计
print(statistics)
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 0<statistics.l_mode()<100 and 20<statistics.a_mode()<87 and -128<statistics.b_mode()<30:#if the circle is red
img.draw_rectangle(area, color = (0, 255, 255))
sending_data(1,c.x(),c.y())
elif 0<statistics.l_mode()<21 and -128<statistics.a_mode()<127 and -3<statistics.b_mode()<18:#if the circle is black#(0, 21, -128, 127, -3, 18)
img.draw_rectangle(area, color = (255, 0, 255))
sending_data(2,c.x(),c.y())
elif 60<statistics.l_mode()<100 and -21<statistics.a_mode()<-1 and -128<statistics.b_mode()<127:#whrite
img.draw_rectangle(area, color = (255, 255, 0))
sending_data(3,c.x(),c.y())
else:
img.draw_rectangle(area, color = (255, 255, 255))
#recive_data()
#print(clock.fps())
前面两个的综合
备用方案
import sensor, image, time, math
from pyb import UART
import ustruct
# 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...
black_thresholds = [(0, 27, -128, 13, -128, 9)] # generic_red_thresholds -> index is 0 so code == (1 << 0)
red_thresholds = [(0, 100, 29, 127, -128, 18)]
whrite_thresholds = [(0, 96, 127, -128, -128, 125)] # generic_green_thresholds -> index is 1 so code == (1 << 1)
# Codes are or'ed together when "merge=True" for "find_blobs".
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
def sending_data(color,cx,cy):
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data=ustruct.pack("b",0x0a)
data1=ustruct.pack("b",color)
data2=ustruct.pack("b",cx)
data3=ustruct.pack("b",cy)
data4=ustruct.pack("b",0x5b)
datasum=data+data1+data2+data3+data4
#print("sentData:",color,cx,cy)
uart.write(datasum); #必须要传入一个字节数组
def recive_data():
global uart
if uart.any():
tmp_data = uart.readline();
print(tmp_data)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_vflip(0)
sensor.set_hmirror(False)
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. "merge=True" must be set to merge overlapping color blobs for color codes.
while(True):
#clock.tick()
img = sensor.snapshot()
max_size = 0
for blob in img.find_blobs(red_thresholds, pixels_threshold=100, area_threshold=100, merge=True):
if blob.code() == 1: # r/g code == (1 << 1) | (1 << 0)
if max_size < blob.pixels() and 300<blob.pixels()<800:
max_blob = blob
max_size = blob.pixels()
print(max_size)
if max_size!=0:
img.draw_edges(max_blob.min_corners(), color=(255,0,0))
# These values are stable all the time.
# Note - the blob rotation is unique to 0-180 only.
sending_data(0,max_blob.cx(), max_blob.cy())
max_size = 0
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
print(statistics)
#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
if 4<statistics.l_mode()<15 and -128<statistics.a_mode()<127 and -17<statistics.b_mode()<6:#if the circle is red
img.draw_rectangle(area, color = (255, 0, 255))
sending_data(1,c.x(),c.y())
elif 60<statistics.l_mode()<100 and -10<statistics.a_mode()<50 and -128<statistics.b_mode()<127:
img.draw_rectangle(area, color = (255, 255, 0))
sending_data(2,c.x(),c.y())
#recive_data()
#print(clock.fps())