当前位置:   article > 正文

基于openmv识别红色小球代码_openmv识别球颜色

openmv识别球颜色

主要内容分为两部分,1.颜色识别        2.噪点滤波

1.颜色识别

首先要用二值化图像来取红球的颜色阈值

 将lab的值调到我们需要的颜色为白色时,此时的lab值为我们是想要的小球阈值

将阈值写入识别代码中

  1. red_threshold = (23, 99, 21, 70, 68, -2) #红球的lab阈值
  2. for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=True, margin=10):

具体find_blobs里每个参数是什么作用看openmv官网

2.噪点滤波

当我正常只进行识别时,通过二值化图像我发现存在非常多的噪点,所以接下来我们需要滤波才能让摄像头传输的是我们需要的小球的位置、大小信息

通过将二值化图像输出到lcd屏幕上时,我发现大多数噪点都比较小,都比我需要识别的物体小。

led显示二值化图像

  1. import lcd
  2. sensor.reset() # Initialize the camera sensor.
  3. sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
  4. sensor.set_framesize(sensor.QQVGA2) # Special 128x160 framesize for LCD Shield.
  5. lcd.init() # Initialize the lcd screen.
  6. while(True):
  7. img = sensor.snapshot() #开始拍照
  8. img.binary([red_threshold]) #二极化处理
  9. img.dilate(1) #腐蚀膨胀过滤边沿噪点
  10. lcd.display(img) #led显示

所以我决定通过大小滤波,因为要滤掉噪点所以我们需要识别的色块不合成,合成了不需要的噪点与物体合成一个,将我们的实验误差大大增加,则需要改变find_blobs方法中的变量

merge=False

然后通过比较大小进行滤波

  1. for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=False, margin=10):
  2. if ((blob.h()*blob.w())>(blob_max_b*blob_max_a)):#取最大的那个色块
  3. blob_max_b = blob.w()
  4. blob_max_a = blob.h()
  5. blob_max_x = blob.cx()
  6. blob_max_y = blob.cy()
  7. if(flag == 0):#第一次识别到物体(我单片机上需要进行该操作,其他人不需要则可去掉)
  8. usart3.write("ATOK\n")
  9. flag = 1
  10. if blob_max_x != 0: #识别到物体并发送串口数据
  11. data.append((blob_max_x,blob_max_y,blob_max_a,blob_max_b))
  12. data_out = json.dumps(set(data))
  13. usart3.write(data_out +'\n')
  14. time.sleep_ms(10)

整体代码

  1. from pyb import UART
  2. import sensor, image, time
  3. import json
  4. import lcd
  5. sensor.reset()
  6. sensor.set_pixformat(sensor.RGB565)
  7. sensor.set_framesize(sensor.QQVGA)
  8. sensor.reset() # Initialize the camera sensor.
  9. sensor.set_pixformat(sensor.RGB565) # or sensor.GRAYSCALE
  10. sensor.set_framesize(sensor.QQVGA2) # Special 128x160 framesize for LCD Shield.
  11. lcd.init() # Initialize the lcd screen.
  12. #sensor.set_auto_gain(False) # must be turned off for color tracking
  13. #sensor.set_auto_whitebal(False)
  14. clock = time.clock()
  15. sensor.skip_frames(time = 2000) #相当于delay200次拍照的时间消抖
  16. usart3=UART(3,115200) #初始化串口
  17. usart3.init(115200, bits=8, parity=None, stop=1) #初始化串口
  18. Range = (0,0,160,120)
  19. flag = 0
  20. red_threshold = (23, 99, 21, 70, 68, -2) #红球的lab阈值
  21. #筛选色块函数:
  22. def blob_filter(img):
  23. global flag
  24. blob_max_b=0
  25. blob_max_a=0
  26. blob_max_x=0
  27. blob_max_y=0
  28. data = []
  29. img = sensor.snapshot()
  30. for blob in img.find_blobs([red_threshold],roi = Range,area_threshold=200, pixels_threshold=200, merge=False, margin=10):
  31. if ((blob.h()*blob.w())>(blob_max_b*blob_max_a)):#取最大的那个色块
  32. blob_max_b = blob.w()
  33. blob_max_a = blob.h()
  34. blob_max_x = blob.cx()
  35. blob_max_y = blob.cy()
  36. if(flag == 0):#第一次识别到物体(我单片机上需要进行该操作,其他人不需要则可去掉)
  37. usart3.write("ATOK\n")
  38. flag = 1
  39. if blob_max_x != 0: #识别到物体并发送串口数据
  40. data.append((blob_max_x,blob_max_y,blob_max_a,blob_max_b))
  41. data_out = json.dumps(set(data))
  42. usart3.write(data_out +'\n')
  43. time.sleep_ms(10)
  44. while(True):
  45. img = sensor.snapshot() #开始拍照
  46. img.binary([red_threshold]) #二极化处理
  47. img.dilate(1) #腐蚀膨胀过滤边沿噪点
  48. lcd.display(img) #led显示
  49. #blobs = img.find_blobs([red_threshold],roi = Range,area_threshold=1000, pixels_threshold=1000, merge=False, margin=10)
  50. blob_filter(img)

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号