找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3104|回复: 1
打印 上一主题 下一主题
收起左侧

基于openmv的单个色环阈值分割python代码

[复制链接]
跳转到指定楼层
楼主
基于openmv阈值分割的代码

  1. import sensor, image, time, lcd, math,ujson, pyb ,json

  2. from pyb import UART
  3. from pyb import LED
  4. obj = [[12,0],[10,12],[22,10],[99,11]]   #json

  5. threshold_index = 0 # 0 for red, 1 for green, 2 for blue

  6. objthresholds = [(51, 76, 8, 77, 7, 23), # generic_red_thresholds
  7.            (37, 60, -32, 33, 0, 67), # generic_green_thresholds
  8.            (37, 60, -5, 33, -60, 67)] # generic_blue_thresholds

  9. graythreshold=[(100,255)]

  10. red_led = LED(1)
  11. green_led = LED(2)
  12. blue_led = LED(3)

  13. sensor.reset()
  14. sensor.set_pixformat(sensor.RGB565)
  15. sensor.set_framesize(sensor.QVGA)
  16. sensor.skip_frames(time = 2000)

  17. uart = UART(3, 115200)

  18. red_led.off()
  19. green_led.off()
  20. blue_led.off()

  21. def compareBlob(blob1, blob2):
  22.    tmp = blob1.pixels() - blob2.pixels()
  23.    if tmp == 0:
  24.        return 0;
  25.    elif tmp > 0:
  26.        return 1;
  27.    else:
  28.        return -1;

  29. A=6

  30. while(True):
  31.     img = sensor.snapshot()
  32.     #img.lens_corr(1.0)
  33.                                                                         #accepet
  34.     if(uart.any()):
  35.         r = uart.readline()

  36.         if r == b'R':
  37.             A=6
  38.         elif r == b'G' :
  39.             A=7
  40.         elif r == b'B':
  41.             A=8
  42.         else:
  43.             A=0

  44.     if(A==6):
  45.         img.binary([objthresholds[0]])
  46.         img.dilate(2)

  47.         blobs=img.find_blobs(graythreshold, pixels_threshold=2025, area_threshold=1600, merge=True)
  48.         if len(blobs) == 0:
  49.             continue
  50.             bigBlob = blobs[0]
  51.             for blob in blobs:
  52.                 if compareBlob(bigBlob, blob) == -1:
  53.                     bigBlob = blob
  54.                 img.draw_rectangle(bigBlob.rect())
  55.                 print(bigBlob.cx(), bigBlob.cy())
  56.                 #output_str=json.dumps([bigBlob.cx(),bigBlob.cy()])
  57.                 data_out = 'O'
  58.             red_led.on()
  59.             green_led.off()
  60.             blue_led.off()
  61.             uart.write(data_out)
  62.             print(data_out)

  63.     if(A==7):
  64.         img.binary([objthresholds[1]])
  65.         img.dilate(4)
  66.         blobs=img.find_blobs(graythreshold, pixels_threshold=2025, area_threshold=1600, merge=True)
  67.         if len(blobs) == 0:
  68.            continue
  69.         bigBlob = blobs[0]
  70.         for blob in blobs:
  71.             if compareBlob(bigBlob, blob) == -1:
  72.                 bigBlob = blob
  73.                 img.draw_rectangle(bigBlob.rect())
  74.                 print(bigBlob.cx(), bigBlob.cy())
  75.                 #output_str=json.dumps([bigBlob.cx(),bigBlob.cy()])
  76.                 data_out ='P'
  77.             red_led.off()
  78.             green_led.on()
  79.             blue_led.off()
  80.             uart.write(data_out)
  81.             print(data_out)

  82.     if(A==8):
  83.         img.binary([objthresholds[2]])
  84.         img.dilate(4)

  85.         blobs=img.find_blobs(graythreshold, pixels_threshold=900, area_threshold=900, merge=True)
  86.         if len(blobs) == 0:
  87.             continue
  88.             bigBlob = blobs[0]
  89.             for blob in blobs:
  90.                 if compareBlob(bigBlob, blob) == -1:
  91.                     bigBlob = blob
  92.                 img.draw_rectangle(bigBlob.rect())
  93.                 print(bigBlob.cx(), bigBlob.cy())

  94.                 data_out ='Q'
  95.             red_led.off()
  96.             green_led.off()
  97.             blue_led.on()
  98.             uart.write(data_out)
  99.             print(data_out)

  100.     if(A==0):
  101.         red_led.on()
  102.         time.sleep(50)
  103.         red_led.off()
复制代码

下载: 单个色环识别uart.zip (1.05 KB, 下载次数: 21)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:362900 发表于 2019-7-13 22:51 | 只看该作者
深度学习,谢谢楼主!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表