找回密码
 立即注册
首页 业界区 安全 一种基于openmv和触摸屏的动态读取颜色的解决方案 ...

一种基于openmv和触摸屏的动态读取颜色的解决方案

泥地锚 2025-6-1 18:40:41
一种基于openmv和触摸屏的动态读取颜色的解决方案

前言:

​        作为大学生电子设计竞赛控制题选手,常常需要与视觉上位机打交道,openmv作为当前一种开源方案,能够以较低的成本制作,并且官方文档和各种教程丰富,但是苦于光照的影响,程序中预定的阈值往往会出现误差,导致完美运行的工程就此崩塌,故博主以2023年电赛E题为背景,基于B站up主:程欢欢的智能控制集 的开源屏幕方案,设计了一套能够基于触摸屏动态读取阈值的解决方案,该方案成本低廉(屏幕成本大概在25元左右),支持阈值读取,保存,修改,删除等,并可以DIY扩展多阈值
原理:


  • 通过阅读星瞳的官方手册 使用统计信息 · OpenMV中文入门教程 ,我们可以使用统计方法来读取某一RIO区域的各种阈值及其平均数,中位数,众数等,基于此,不难想到我们可以通过这个api来设计一个读取RIO阈值的函数:Get_threshold
  • 基于B站up主开源的触摸屏方案,我们可以利用其显示摄像头的实时图像,并使用触摸屏操控,设计控件。


  • 至此,我们外部控制设备和设计原理都已经掌握,可以开始设计解决方案了
文件结构:

calibration.py                             :该脚本记录了电阻屏的按压阈值
get_threshold_v3.py(main.py)               :该脚本主要实现了动态阈值的读取,保存,修改,删除等操作
screen.py                                  :该脚本实现了SPI总线与屏幕控制IC和触摸IC的初始化
my_threshold.py                            :该脚本保存了读取的阈值,包括LAB格式,和灰度格式
get_threshold_v3.py的部分函数实现

1. 串口收发函数:
  1. '''
  2. 串口发送函数      请根据实际情况更改发送的数据
  3. 功能: 以16进制发送一条位置帧
  4. 帧结构: 帧头(0x12,0x13),数据段(data1,data2), 帧尾(0x11)|
  5. 参数: data1 : 颜色目标X轴量化坐标 范围:0x00 ~ 0x250
  6.      data2 : 颜色目标Y轴量化坐标 范围:0x00 ~ 0x250
  7.      异常处理: 当找不到色块时, 输出X == 255, Y == 255,表示色块没有找到
  8. '''
  9. def send_data(data1,data2):
  10.     uart.write(bytes([12,13]))       # 将整数转换为字节类型(16位)并发送
  11. #    print([data1,data2])
  12.     uart.write(bytes([data1,data2])) # 将整数转换为字节类型(16位)并发送
  13.     uart.write(bytes([11]))          # 将整数转换为字节类型(16位)并发送
  14. '''
  15. 串口接收函数      请根据实际情况更改
  16. '''
  17. def rece_data():
  18.     rece_dat = 0
  19.     if uart.any():
  20.         rece_dat =  int.from_bytes(uart.read(1),'little')
  21.         print("已接收到数据{0}".format(rece_dat))#打印接收到的数字
  22.         while(uart.any() != 0):
  23.             print("out")
  24.             uart.read(1)
  25. '''
复制代码
2. 寻找目标色块函数:
  1. '''
  2. 寻找最大色块辅助函数
  3. 参数: blobs类型
  4. '''
  5. def find_max(blobs):
  6.     max_size = 0
  7.     max_blob = 0
  8.     for blob in blobs:
  9.         if blob[2]*blob[3] > max_size:
  10.             max_blob=blob
  11.             max_size = blob[2]*blob[3]
  12.     return max_blob
  13. '''
  14. 寻找最大色块函数
  15. 参数: img:       图像
  16.      threshold: 颜色阈值
  17. '''
  18. def find_maxblobs(img,threshold):
  19.     global cnt_find_fail
  20.     print(threshold)
  21.     blobs = img.find_blobs(threshold,roi = (40,0,240,240))
  22.     if blobs:
  23.     #如果找到了目标颜色
  24.         cnt_find_fail = 0
  25.         max_blob = find_max(blobs)
  26.         img.draw_rectangle(max_blob.rect(), color = (255,255,255))
  27.         #img.draw_edges(b.min_corners(), color=(0,255,0))
  28.         img.draw_cross(max_blob.cx(),max_blob.cy(), color = (255,255,255))
  29. #        img.draw_string(10,30,str(max_blob.cx()*max_blob.cy()),color=(255,0,0),mono_space=False)
  30.         send_data(max_blob.cx() - 40,max_blob.cy())
  31.     else:
  32.         cnt_find_fail = cnt_find_fail+1
  33.         if cnt_find_fail >= 10:
  34.             print("find_out")
  35.             send_data(255,255)
复制代码
3. 文件操作函数:
  1. '''
  2. 保存阈值函数
  3. 注意: !!! 保存后需要重启器件
  4. 会在当前目录下生成一个my_threshold.py文件保存get获取的阈值
  5. '''
  6. def save_threshold():
  7.     global text
  8.     my_threshold = threshold[0]   #读取阈值默认保存到列表第一个
  9.     my_grayscale_threshold = (my_threshold[0],my_threshold[1])#灰度值等于LAB中的L即亮度值
  10.     file_path = "my_threshold.py" #文件地址
  11.     f = open(file_path,mode='w')
  12.     f.write('my_threshold=' + str(my_threshold))
  13.     f.write('\n')
  14.     f.write('my_grayscale_threshold=' + str(my_grayscale_threshold))
  15.     f.write(text+' linjiejie and bilibili up: 程欢欢')
  16.     #写入LAB和灰度阈值
  17.     #延时确保文件被保存
  18.     sleep(0.5)
  19.     f.close()
  20.     print("save sccess")
  21.     sleep(0.5)
  22.     #重置器件
  23.     hard_reset()
  24. '''
  25. 删除阈值函数
  26. 注意: !!! 删除后需要重启器件
  27. 删除当前目录下的my_threshold.py文件, 当前版本未使用该函数
  28. '''
  29. def del_threshold():
  30.     sleep(0.5)
  31.     os.remove('my_threshold.py')
  32.     hard_reset()
  33. '''
  34. 读取阈值函数
  35. 返回值: 若文件存在,返回文件的LAB阈值(默认),若没有找到则返回(0)元组
  36. 如果需要读取的是灰度阈值请自行修改
  37. '''
  38. def read_threshold():
  39.     try:
  40.         import my_threshold
  41.         return my_threshold.my_threshold
  42.     except:
  43.         print("can't open threshold")
  44.         return (0,0,0,0,0,0)
  45. '''
复制代码
4. 控件绘制函数:
  1. '''
  2. 控件绘制函数
  3. 绘制clear, get, save, size等控件, 可以自定义
  4. 参数: img: image类型
  5. '''
  6. def draw_button(img):
  7.     #绘制clear
  8.     img.draw_circle(mode_btn_x,mode_btn_y,mode_btn_radius,color=(0,255,0),thickness = 3)
  9.     img.draw_string(mode_btn_x-10,mode_btn_y-5,'mode',color=(0,255,0),mono_space=False)
  10.     #绘制get
  11.     img.draw_circle(get_btn_x,get_btn_y,get_btn_radius,color=(255,255,0),thickness = 3)
  12.     img.draw_string(get_btn_x-5,get_btn_y-6,'get',color=(255,255,0),mono_space=False)
  13.     #绘制save
  14.     img.draw_circle(save_btn_x,save_btn_y,save_btn_radius,color=(0,255,255),thickness = 3)
  15.     img.draw_string(save_btn_x-7,save_btn_y-6,'save',color=(0,255,255),mono_space=False)
  16.     #绘制size
  17.     img.draw_circle(size_btn_x,size_btn_y,size_btn_radius,color=(255,180,255),thickness = 3)
  18.     img.draw_string(size_btn_x-6,size_btn_y-6,'size',color=(255,180,255),mono_space=False)
复制代码
5. 阈值读取函数(核心):
  1. '''
  2. 采集阈值函数
  3. 采集roi方框中的统计数据, 使用均值滤波
  4. 参数 n:                 需要连续采集n次
  5.     img:               摄像头读取的图像image类型
  6.     img_drawing_board: 画板image类型, 用于和img做叠加操作, 在屏幕上显示
  7. 返回值: get_thresholds 采集到的阈值元组
  8. '''
  9. def Get_threshold(n,img,img_drawing_board):#采集n次
  10.     if(last_x == 0 and last_y == 0):
  11.         print('error')
  12.         return None
  13.     #阈值保存变量初始化
  14.     get_thresholds = ()
  15.     Lmin = 0
  16.     Lmax = 0
  17.     Amin = 0
  18.     Amax = 0
  19.     Bmin = 0
  20.     Bmax = 0
  21.     for i in range(n):
  22.         img = sensor.snapshot()
  23.         Statistics = img.get_statistics(roi = (last_x,last_y,get_roi_size,get_roi_size))
  24.         img.draw_rectangle(last_x,last_y,get_roi_size,get_roi_size,color=(255,255,255))
  25.         img.draw_string(10,10,'getting threshold:',color=(255,0,0),mono_space=False)#打印正在获取阈值
  26.         print("{0}...".format(i+1))
  27.         Lmin += Statistics.l_min()
  28.         Lmax += Statistics.l_max()
  29.         Amin += Statistics.a_min()
  30.         Amax += Statistics.a_max()
  31.         Bmin += Statistics.b_min()
  32.         Bmax += Statistics.b_max()
  33.         img.b_nor(img_drawing_board)    #将画板画布与感光器图像叠加仅支持二值图像(黑白图像)
  34.         screen.display(img)
  35.     #均值滤波
  36.     Lmin //= n
  37.     Lmax //= n
  38.     Amin //= n
  39.     Amax //= n
  40.     Bmin //= n
  41.     Bmax //= n
  42.     get_thresholds = (Lmin,Lmax,Amin,Amax,Bmin,Bmax)
  43.     print(get_thresholds)
  44.         return get_thresholds
复制代码

  • !!!注意,博主这里仅使用最简单的均值滤波,请根据自己需要修改滤波函数
6. 主函数:

[code]def get_main():    global last_x    global last_y    global rectangle_flag    global threshold    global state    global get_roi_size    highlight_threshold = [(74, 100, -4, 6, -17, 2)] #高光阈值    screen.init()    threshold[0] =  read_threshold()#读取文件中的阈值    print(threshold)    img_drawing_board=sensor.alloc_extra_fb(320,240,sensor.RGB565)    img_drawing_board.draw_rectangle(0,0,320,240,fill=True,color=(255,255,255))    #初始化画布    while(True):        #这里state 为0和1都覆盖高光是因为电阻屏四周灵敏度差,容易误触        #这里的状态可以根据自己的需要选择删除, 设置这里的黑色覆盖是为了更加直观的在屏幕中显示目标色块与实际检测结果在图像中的分布情况, 以帮助我们来调试和选择设置方框大小        if   state == 0:            img = sensor.snapshot().binary([highlight_threshold[0]], invert=False, zero=True)        elif state == 1:            img = sensor.snapshot().binary([highlight_threshold[0]], invert=False, zero=True)        elif state == 2:            img = sensor.snapshot().binary([threshold[0]], invert=False, zero=True)#        img = sensor.snapshot()#.binary([(86, 254)])二值化操作#        img = img.gaussian(3)        if screen.press:            #判断mode按键            if (mode_btn_x-mode_btn_radius
您需要登录后才可以回帖 登录 | 立即注册