当前位置:   article > 正文

5分钟上手Open MV颜色识别_openmv中怎么通过hsv来识别颜色

openmv中怎么通过hsv来识别颜色

代码是选自星瞳OpenMV实例,下面我就讲解如何调参,修改代码

↑颜色识别代码的位置
现在的颜色识别为红色,让我们将识别颜色改为绿色,操作步骤如下:

①阈值调试器位置

②对LAB进行调试

使想要识别的物体呈现白色,背景为黑色

初始二进制图像
                                                                                 ↓
改后,6个数值均可更改,使其成像效果最好即完成配置

③获取LAB阈值并改入

将thresholds的参数改为刚复制的LAB阈值

  1. # Single Color RGB565 Blob Tracking Example
  2. #
  3. # This example shows off single color RGB565 tracking using the OpenMV Cam.
  4. import sensor
  5. import time
  6. import math
  7. threshold_index = 0 # 0 for red, 1 for green, 2 for blue
  8. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  9. # The below thresholds track in general red/green/blue things. You may wish to tune them...
  10. thresholds = [
  11. (30, 100, 15, 127, 15, 127), # generic_red_thresholds
  12. (30, 100, -64, -8, -32, 32), # generic_green_thresholds
  13. (0, 30, 0, 64, -128, 0),
  14. ] # generic_blue_thresholds
  15. sensor.reset()
  16. sensor.set_pixformat(sensor.RGB565)
  17. sensor.set_framesize(sensor.QVGA)
  18. sensor.skip_frames(time=2000)
  19. sensor.set_auto_gain(False) # must be turned off for color tracking
  20. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  21. clock = time.clock()
  22. # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
  23. # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
  24. # camera resolution. "merge=True" merges all overlapping blobs in the image.
  25. while True:
  26. clock.tick()
  27. img = sensor.snapshot()
  28. for blob in img.find_blobs(
  29. [thresholds[threshold_index]],
  30. pixels_threshold=200,
  31. area_threshold=200,
  32. merge=True,
  33. ):
  34. # These values depend on the blob not being circular - otherwise they will be shaky.
  35. if blob.elongation() > 0.5:
  36. img.draw_edges(blob.min_corners(), color=(255, 0, 0))
  37. img.draw_line(blob.major_axis_line(), color=(0, 255, 0))
  38. img.draw_line(blob.minor_axis_line(), color=(0, 0, 255))
  39. # These values are stable all the time.
  40. img.draw_rectangle(blob.rect())
  41. img.draw_cross(blob.cx(), blob.cy())
  42. # Note - the blob rotation is unique to 0-180 only.
  43. img.draw_keypoints(
  44. [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
  45. )
  46. print(clock.fps())

 

④改后效果

可以看到图象识别已经改为绿色

代码展示

  1. # Single Color RGB565 Blob Tracking Example
  2. #
  3. # This example shows off single color RGB565 tracking using the OpenMV Cam.
  4. import sensor
  5. import time
  6. import math
  7. threshold_index = 0 # 0 for red, 1 for green, 2 for blue
  8. # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
  9. # The below thresholds track in general red/green/blue things. You may wish to tune them...
  10. thresholds = [
  11. (0, 76, -128, -13, -128, 31), # LAB_thresholds
  12. ]
  13. sensor.reset()
  14. sensor.set_pixformat(sensor.RGB565)
  15. sensor.set_framesize(sensor.QVGA)
  16. sensor.skip_frames(time=2000)
  17. sensor.set_auto_gain(False) # must be turned off for color tracking
  18. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  19. clock = time.clock()
  20. # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
  21. # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
  22. # camera resolution. "merge=True" merges all overlapping blobs in the image.
  23. while True:
  24. clock.tick()
  25. img = sensor.snapshot()
  26. for blob in img.find_blobs(
  27. [thresholds[threshold_index]],
  28. pixels_threshold=200,
  29. area_threshold=200,
  30. merge=True,
  31. ):
  32. # These values depend on the blob not being circular - otherwise they will be shaky.
  33. if blob.elongation() > 0.5:
  34. img.draw_edges(blob.min_corners(), color=(255, 0, 0))
  35. img.draw_line(blob.major_axis_line(), color=(0, 255, 0))
  36. img.draw_line(blob.minor_axis_line(), color=(0, 0, 255))
  37. # These values are stable all the time.
  38. img.draw_rectangle(blob.rect())
  39. img.draw_cross(blob.cx(), blob.cy())
  40. # Note - the blob rotation is unique to 0-180 only.
  41. img.draw_keypoints(
  42. [(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
  43. )
  44. print(clock.fps())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/920277
推荐阅读
相关标签
  

闽ICP备14008679号