openmv颜色识别串口发送,双重滤波+子函数直接调用~~~~贼稳定

#*********导入各种块********#
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()
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值