将客 2024-06-09 14:37 采纳率: 96.6%
浏览 14
已结题

MaixPyIDE摄像头识别小球


import sensor, image, time,lcd,utime
from machine import UART
from fpioa_manager import fm
fm.register(10, fm.fpioa.UART1_TX, force=True)#映射串口引脚
fm.register(9, fm.fpioa.UART1_RX, force=True)#映射串口引脚
uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=4096)
clock = time.clock()
lcd.init() #显示屏初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) #QQVGA: 160x120、QVGA: 320x240、
sensor.run(1)
sensor.skip_frames(20)#跳过刚启动开始的30帧照片
lcd.rotation(2)#LCD屏幕翻转,范围是0~3,上和下反转,左和右反转
sensor.set_hmirror(True)#摄像头镜像
sensor.set_auto_gain(False) # must be turned off for color tracking
#sensor.set_auto_whitebal(False) # must be turned off for color tracking,关闭白平衡
#sensor.set_brightness(3) #-3至+3,亮度越大,图片越亮
#sensor.set_saturation(2)#-3至+3,饱和度越大,色彩越鲜艳
#sensor.set_contrast(2)#-3至+3,对比度越大,颜色之间的反差越大
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   
#存放不同的颜色阈值,依次为白色、黄色、绿色(白天光的上流:(82, 100, -3, 8, -3, 2))(晚上:(62, 100, -30, 4, -9, 31))
color = ((82, 100, -3, 8, -3, 2),(50, 96, -28, 18, 48, 90))
size_threshold = 4500 #若检测到的小球框的面积小于size_threshold表明小球距离小车太远,小车需要前进。这个参数的大小需要根据实际情况修改
target=()
flag = 0 #用于标记一个while循环中是否检测到小球,falg==1表示本轮发现了小球
count = 0
bug_open = False # True or False
previous_ball_location = 'r'
location_count=0 #用来防止误识别,保持识别结果的稳定性
def stable_output(location):
    global location_count,previous_ball_location
    if previous_ball_location != location:
        location_count = 1
        previous_ball_location = location
    else:
        location_count = location_count + 1
 if location_count >=10:
        uart.write(location) #通过串口向外发送数据,控制小车向location方向运动
clock.tick()#开始追踪运行时间。
while(True):
    sensor.set_auto_whitebal(False) # must be turned off for color tracking,检测黄球时,关闭白平衡,检测白球时开启白平衡
    img = sensor.snapshot().histeq(adaptive=True, clip_limit=3)
    img.lens_corr(1.8) #镜头畸变矫正(lens correction),lens_corr 为了去除畸变, 1.8 是默认参数,可以根据自己实际情况调整
    #专门检测黄色小球
    blobs = img.find_blobs([color[1]])
    if blobs:
        max_blob = find_max(blobs)
        x_error = max_blob[5]-img.width()/2#若x_error<0,则说明小球在左边,小车需要左转,若x_error>0,则说明小球在右边,小车需要右转
        h_error = max_blob[2]*max_blob[3]-size_threshold#若h_error<0,则小车距离小球还有距离,需要前进。
        img.draw_circle(max_blob.x()+int(max_blob.w()/2), max_blob.y()+int(max_blob.w()/2), int(max_blob.w()/2), color = (255, 255, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
        #print("blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
        #给黄球标注框框
        #for b in blobs:
            #img.draw_circle(b.x()+int(b.w()/2), b.y()+int(b.w()/2), int(b.w()/2), color = (255, 255, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
        if(x_error<-10 and max_blob.density()>0.7 and 70>max_blob.w()>8):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
            #stable_output('l')
            uart.write('l')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 10, "Left", lcd.RED, lcd.YELLOW)
            if bug_open == True:
                print("黄球,串口发送l,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
        if(x_error>10 and max_blob.density()>0.7 and 70>max_blob.w()>8):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
            #stable_output('r')
            uart.write('r')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 20, "Right", lcd.RED, lcd.WHITE)
            if bug_open == True:
                print("黄球,串口发送:r,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
        if(-10<x_error<10 and h_error<0 and max_blob.density()>0.7 and 70>max_blob.w()>8):
            #stable_output('h')
            uart.write('h')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 30, "Head", lcd.RED, lcd.GREEN)
            if bug_open == True:
                print("黄球,串口发送:h,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
    else:
 stable_output('t')
        if bug_open == True:
            print("无黄,串口发送:t,")
        #检测白球时,开启白平衡效果会更好一点
        sensor.set_auto_whitebal(True) # must be turned off for color tracking,开启白平衡
        img = sensor.snapshot().histeq(adaptive=True, clip_limit=3)
        img.lens_corr(1.8) #镜头畸变矫正(lens correction),lens_corr 为了去除畸变, 1.8 是默认参数,可以根据自己实际情况调整
        #思想:先识别出圆形,再识别圆的颜色
        #img.find_circles:控制从霍夫变换中监测到的圆。
        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为识别到的圆的区域,即圆的外接矩形框
            area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())    
            ##保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
            #target = (c.x(), c.y(), c.r())          
            #像素颜色统计,roi 是感兴趣区域的矩形元组(x,y,w,h)。如果未指定,ROI即整个图像的图像矩形。操作范围仅限于 roi 区域内的像素。                
            statistics = img.get_statistics(roi=area)
            #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
            if color[0][0]<statistics.l_mode()<color[0][1] and color[0][2]<statistics.a_mode()<color[0][3] and color[0][4]<statistics.b_mode()<color[0][5]:#if the circle is red
                img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255),thickness=5)#识别到的白色圆形用白色的圆框出来
                #保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
                target = (c.x(), c.y(), c.r())
            #elif color[1][0]<statistics.l_mode()<color[1][1] and color[1][2]<statistics.a_mode()<color[1][3] and color[1][4]<statistics.b_mode()<color[1][5]:#if the circle is red
                #img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 0))#识别到的黄色圆形用黄色的圆框出来
                ##保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
                #target = (c.x(), c.y(), c.r())
            else:
                img.draw_circle(c.x(), c.y(), c.r(), color = (0, 0, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
                target = (c.x(), c.y(), c.r())#其实这里应该不加这一句,但为了演示效果更好,让小车也追一下圆形
        if target:
            flag = 1 #标记本轮发现过目标
            #一张图只保留一个小球的信息,只追一个小球
            x_error = target[0]-img.width()/2 #若x_error<0,则说明小球在左边,小车需要左转,若x_error>0,则说明小球在右边,小车需要右转
            h_error = 2*target[2]*2*target[2]-size_threshold #若h_error<0,则小车距离小球还有距离,需要前进。
            if(x_error<-10):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
                #stable_output('l')
                uart.write('l')
                count = 0 #发现目标,count置为0,从头开始计数
                if bug_open == True:
                    print("白球,左转,发串口:l")
                lcd.draw_string(10, 10, "Left", lcd.RED, lcd.YELLOW)
            if(x_error>10):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
                #stable_output('r')
                uart.write('r')
                count = 0 #发现目标,count置为0,从头开始计数
if bug_open == True:
                    print("白球,右转,发串口:r")
                lcd.draw_string(10, 20, "Right", lcd.RED, lcd.WHITE)
            if(10>x_error>-10 and h_error<0):
                #stable_output('h')
                uart.write('h')
                count = 0 #发现目标,count置为0,从头开始计数
                if bug_open == True:
                    print("白球,前进,发串口:h")
                lcd.draw_string(10, 30, "Head", lcd.RED, lcd.GREEN)
        else:
            stable_output('t')          
            if bug_open == True:
                print("无白色球,发串口:t")
        target=()#清空target
    #控制视野内无球时,自动慢速右转
    if flag == 0:
        count = count+1
        if count%10 == 0:
            uart.write('s')
            utime.sleep_ms(10)
            uart.write('n') #通过串口向外发送数据,没有检测到黄色或白色小球
            if bug_open == True:
                print("视野无球,发串口:n")
            lcd.draw_string(250, 10, "FPS %f" % clock.fps(), lcd.BLACK, lcd.RED)
            count = 0
    flag = 0 #重置标记
    if bug_open == True:
        print("FPS %f" % clock.fps())#停止追踪运行时间,并返回当前FPS(每秒帧数)。在调用该函数前始终首先调用 tick 
        print("flag:",flag,"count:",count)
    lcd.display(img) #LCD 显示图片
为什么这个代码别不了白色的球,如果要修改,应该怎么修改


  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-09 14:37
    关注

    引自免费微信小程序:皆我百晓生

    从你提供的代码来看,识别小球的逻辑分为两个部分:一是识别黄色小球,二是识别白色小球。根据你的描述,代码似乎无法正确识别白色小球。这可能由几个原因造成,包括但不限于颜色阈值设置不当、光照条件变化、算法选择以及图像处理参数设置。

    颜色阈值

    对于白色小球的识别,你使用了img.get_statistics(roi=area)来获取感兴趣区域的统计信息,然后基于LAB颜色空间的模式值进行判断。这种方式在理想情况下可以工作,但在实际应用中可能受到光照影响较大。白色在不同光照条件下LAB值可能会有显著变化,导致阈值匹配失败。

    光照影响

    由于白色对光照极其敏感,建议你在识别白色小球时尝试固定环境光照或采用更稳定的颜色识别方法,例如使用HSV颜色空间,它通常比LAB或RGB更稳定地反映颜色特性。

    图像处理与特征提取

    你目前的方法是先检测圆形,然后在检测到的圆形区域内进行颜色判断。这种方法在噪声较小且小球边界清晰的情况下有效,但在复杂背景或低光照条件下可能表现不佳。

    改进方案

    1. 调整颜色阈值

    尝试调整用于白色小球的颜色阈值,特别是在LAB模式下的阈值。你可能需要在不同的光照条件下测试并微调这些值。

    2. 切换到HSV颜色空间

    考虑到白色在不同光照条件下的不稳定性,考虑将颜色识别转换到HSV颜色空间,尤其是利用V(Value)通道的高值来识别白色。HSV对光照变化的鲁棒性更强。

    3. 增强图像预处理

    在检测之前,增加一些图像增强步骤,比如使用高斯模糊减少噪声,或者使用边缘检测来增强小球的边界,这样即使在光照变化时也能更准确地检测到小球。

    4. 使用机器学习模型

    如果上述方法仍然无法达到预期效果,可以考虑使用深度学习模型,如YOLOv3或Tiny YOLO等,它们在复杂场景下具有更好的目标检测性能。

    示例代码修改

    下面是一个简化的示例,展示如何在HSV颜色空间中识别白色小球:

    # ...其他代码保持不变...
    
    # 定义白色在HSV空间的阈值
    white_threshold_hsv = [(0, 0, 200), (180, 30, 255)]  # 白色的HSV范围,可能需要调整
    
    # 修改识别白色小球的部分
    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())
        hsv_img = img.to_hsv()  # 转换到HSV颜色空间
        mask = hsv_img.find_blobs([white_threshold_hsv], roi=area)
        if mask:
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255), thickness=5)
            # 接下来执行小球跟踪逻辑...
    

    请注意,以上代码仅作为概念验证,具体阈值和参数可能需要根据实际场景调整。此外,HSV颜色空间的使用可能需要额外的库支持,确保你的MaixPy环境已安装相关库。

    最后,如果在实际环境中遇到问题,建议使用固定的测试条件(如相同的光照和背景)进行调试,以更好地理解算法的表现和局限性。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月18日
  • 已采纳回答 6月10日
  • 创建了问题 6月9日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价