#*********导入各种块********#
import sensor, image, time
from pyb import UART
#*********摄像头参数********#
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
#**********串口**********#
uart = UART(3, 9600)
#********定义变量***********#
a_1=1
a_2=2
a_3=3
a_4=0
red_threshold = (63, 43, 90, 38, -37, 61)
bule_threshold = (61, 31, -66, -25, 54, 25)
green_threshold = (61, 31, -66, -25, 54, 25)
e_1=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
d_1=d_2=0
s=s_1=0
#*********滤波************#
def lvbo(d):
global d_1
global e_1
global d_2
global s
global s_1
if d_1<25:
#print(d_1)
e_1[d_1]=d
d_1=d_1+1
else:
d_2=max(e_1, default='列表为空', key=lambda v: e_1.count(v))
print(max(e_1, default='列表为空', key=lambda v: e_1.count(v))) #这一句是精华
fasong()
e_1=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#*********寻找最大红色块********#
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
#*********寻找红色块********#
def red():
global a_1
blobs = img.find_blobs([red_threshold],x_stride=10, y_stride=10,area_threshold=50)
if blobs:
max_blob = find_max(blobs)
lvbo(a_1)
'''
显示出所有的框
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
#*********寻找红色块********#
def bule():
global a_2
blobs = img.find_blobs([bule_threshold],x_stride=10, y_stride=10,area_threshold=50)
if blobs:
max_blob = find_max(blobs)
lvbo(a_2)
'''
显示出所有的框
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
#*********寻找红色块********#
def green():
global a_3
blobs = img.find_blobs([green_threshold],x_stride=10, y_stride=10,area_threshold=50)
if blobs:
max_blob = find_max(blobs)
lvbo(a_3)
'''
显示出所有的框
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
def fasong():
global d_2
global d_1
if d_2==1:
uart.write("r")
print("r")
d_1=0
if d_2==2:
uart.write("b")
print("b")
d_1=0
if d_2==3:
uart.write("g")
print("g")
d_1=0
while(True):
img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
red()
bule()
green()
openmv颜色识别串口发送,双重滤波+子函数直接调用~~~~贼稳定
最新推荐文章于 2024-06-03 21:17:37 发布