当前位置:   article > 正文

openmv循迹&脱机调阈值代码与实现_openmv脱机调阈值

openmv脱机调阈值

实验用具:

                                                                 openmv4  h7  R2

                                                  立创自己打印的openmv  lcd扩展板 

                                                         1.8寸tft  spi  屏幕芯片st7735s 

                                                        大夏龙雀BT24蓝牙模块

                                                接线:P5接BT24的TX 用来接收蓝牙发来的数据 

   成果展示:                        

openmv阈值脱机调试

 代码:

原理:利用蓝牙,你发送a b会使阈值number1  number2加加,c  d会减减

  1. import sensor, image, time, math, pyb
  2. from machine import UART
  3. import display
  4. #用来调binary值
  5. NUMBER1=51
  6. NUMBER2=255
  7. #这两变量是用来你调阈值,是为了适应在不同场合光线下刚好把黑色与白色分开 ,借助按键调整这个阈值就可以完成现场调试
  8. turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
  9. GRAYSCALE_THRESHOLD =[(0,0)]
  10. # 每个roi为(x, y, w, h),线检测算法将尝试找到每个roi中最大的blob的质心。
  11. # 然后用不同的权重对质心的x位置求平均值,其中最大的权重分配给靠近图像底部的roi,
  12. # 较小的权重分配给下一个roi,以此类推。
  13. rois = [(0, 100, 160, 20), (0, 50, 160, 20), (0, 0, 160, 20)]
  14. # roi代表三个取样区域,(x,y,w,h,weight),代表左上顶点(x,y)宽高分别为w和h的矩形,
  15. # weight为当前矩形的权值。注意本例程采用的QQVGA图像大小为160x120,roi即把图像横分成三个矩形。
  16. # 三个矩形的阈值要根据实际情况进行调整,离机器人视野最近的矩形权值要最大,
  17. # 如上图的最下方的矩形,即(0, 100, 160, 20, 0.7)
  18. # 初始化sensor
  19. sensor.reset()
  20. # 设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种
  21. sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
  22. # 设置图像像素大小
  23. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  24. # 让新的设置生效。
  25. sensor.skip_frames(time=2000) # Let new settings take effect.
  26. # 颜色跟踪必须关闭自动增益
  27. sensor.set_auto_gain(False) # must be turned off for color tracking
  28. # 颜色跟踪必须关闭白平衡
  29. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  30. # 跟踪FPS帧率
  31. lcd = display.SPIDisplay() # Initialize the lcd screen.
  32. kk='y'
  33. sensor.set_vflip(1)
  34. sensor.set_hmirror(1)
  35. clock = time.clock() # Tracks FPS.
  36. largest2_blob=0
  37. deflection_angle = 0 # Initialize deflection_angle outside of conditional blocks
  38. uart = UART(3,9600,timeout_char=1000)
  39. while True:
  40. turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
  41. clock.tick() # Track elapsed milliseconds between snapshots.
  42. img = sensor.snapshot() # Capture an image.
  43. img.binary(turnGRAYSCALE_THRESHOLD)
  44. largest_blob = None
  45. #largest2_blob = None
  46. #largest3_blob = None
  47. # Track lines in each defined ROI.
  48. #blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[0], merge=True)
  49. #if blobs:
  50. # largest_blob = max(blobs, key=lambda b: b.pixels())
  51. blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[1], merge=True)
  52. if blobs:
  53. largest2_blob = max(blobs, key=lambda b: b.pixels())
  54. #
  55. #blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[2], merge=True)
  56. #if blobs:
  57. # largest3_blob = max(blobs, key=lambda b: b.pixels())
  58. # Calculate deflection_angle based on largest2_blob.cx() - 79
  59. #通过计算图像中心点x与块中心点x的差判断偏移
  60. pianyi = 0
  61. if largest2_blob:
  62. pianyi = largest2_blob.cx() - 79
  63. img.draw_rectangle(largest2_blob.rect(),color=(0));
  64. if -5 <= pianyi < 5:
  65. deflection_angle = 0
  66. elif -15 <= pianyi < -5:
  67. deflection_angle = -2
  68. elif 5 <= pianyi < 15:
  69. deflection_angle = 2
  70. elif -30 <= pianyi < -15:
  71. deflection_angle = -3
  72. elif 15 <= pianyi < 30:
  73. deflection_angle = 3
  74. elif -50 <= pianyi < -30:
  75. deflection_angle = -5
  76. elif 30 <= pianyi < 50:
  77. deflection_angle = 5
  78. if NUMBER1>255:
  79. NUMBER1=255
  80. if NUMBER2>255:
  81. NUMBER2=255
  82. if NUMBER1<0:
  83. NUMBER1=0
  84. if NUMBER2<0:
  85. NUMBER2=0
  86. lcd.write(img) # Take a picture and display the image.
  87. byte=uart.read(1)
  88. if byte :
  89. print(byte)
  90. if byte ==b'a':
  91. NUMBER1=NUMBER1+5
  92. if byte ==b'b':
  93. NUMBER2=NUMBER2+5
  94. if byte ==b'c':
  95. NUMBER1=NUMBER1-5
  96. if byte ==b'd':
  97. NUMBER2=NUMBER2-5

原理:利用按键,你按下按键1  按键2会使阈值number1  number2加加减减,支持长久按

P3 P4 P5 P6分别接按键模块KEY1  KEY2  KEY3  KEY4,用if判断是否按下

openmv阈值脱机调试

  1. import sensor, image, time, math, pyb
  2. from machine import UART
  3. import display
  4. from pyb import Pin
  5. #用来调binary值
  6. NUMBER1=51
  7. NUMBER2=255
  8. #这两变量是用来你调阈值,是为了适应在不同场合光线下刚好把黑色与白色分开 ,借助按键调整这个阈值就可以完成现场调试
  9. turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
  10. GRAYSCALE_THRESHOLD =[(0,0)]
  11. # 每个roi为(x, y, w, h),线检测算法将尝试找到每个roi中最大的blob的质心。
  12. # 然后用不同的权重对质心的x位置求平均值,其中最大的权重分配给靠近图像底部的roi,
  13. # 较小的权重分配给下一个roi,以此类推。
  14. rois = [(0, 100, 160, 20), (0, 50, 160, 20), (0, 0, 160, 20)]
  15. # roi代表三个取样区域,(x,y,w,h,weight),代表左上顶点(x,y)宽高分别为w和h的矩形,
  16. # weight为当前矩形的权值。注意本例程采用的QQVGA图像大小为160x120,roi即把图像横分成三个矩形。
  17. # 三个矩形的阈值要根据实际情况进行调整,离机器人视野最近的矩形权值要最大,
  18. # 如上图的最下方的矩形,即(0, 100, 160, 20, 0.7)
  19. # 初始化sensor
  20. sensor.reset()
  21. # 设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种
  22. sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
  23. # 设置图像像素大小
  24. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  25. # 让新的设置生效。
  26. sensor.skip_frames(time=2000) # Let new settings take effect.
  27. # 颜色跟踪必须关闭自动增益
  28. sensor.set_auto_gain(False) # must be turned off for color tracking
  29. # 颜色跟踪必须关闭白平衡
  30. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  31. # 跟踪FPS帧率
  32. lcd = display.SPIDisplay() # Initialize the lcd screen.
  33. kk='y'
  34. #p_in4 = Pin('P7', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  35. p_in4 = Pin('P4', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  36. p_in6 = Pin('P6', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  37. #p_in7 = Pin('P7', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  38. p_in5 = Pin('P5', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  39. p_in3 = Pin('P3', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
  40. sensor.set_vflip(1)
  41. sensor.set_hmirror(1)
  42. clock = time.clock() # Tracks FPS.
  43. largest2_blob=0
  44. deflection_angle = 0 # Initialize deflection_angle outside of conditional blocks
  45. uart = UART(3,9600,timeout_char=1000)
  46. while True:
  47. turnGRAYSCALE_THRESHOLD = [(NUMBER1, NUMBER2)]
  48. clock.tick() # Track elapsed milliseconds between snapshots.
  49. img = sensor.snapshot() # Capture an image.
  50. img.binary(turnGRAYSCALE_THRESHOLD)
  51. largest_blob = None
  52. #largest2_blob = None
  53. #largest3_blob = None
  54. # Track lines in each defined ROI.
  55. #blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[0], merge=True)
  56. #if blobs:
  57. # largest_blob = max(blobs, key=lambda b: b.pixels())
  58. blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[1], merge=True)
  59. if blobs:
  60. largest2_blob = max(blobs, key=lambda b: b.pixels())
  61. #
  62. #blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=rois[2], merge=True)
  63. #if blobs:
  64. # largest3_blob = max(blobs, key=lambda b: b.pixels())
  65. # Calculate deflection_angle based on largest2_blob.cx() - 79
  66. #通过计算图像中心点x与块中心点x的差判断偏移
  67. pianyi = 0
  68. if largest2_blob:
  69. pianyi = largest2_blob.cx() - 79
  70. img.draw_rectangle(largest2_blob.rect(),color=(0));
  71. if -5 <= pianyi < 5:
  72. deflection_angle = 0
  73. elif -15 <= pianyi < -5:
  74. deflection_angle = -2
  75. elif 5 <= pianyi < 15:
  76. deflection_angle = 2
  77. elif -30 <= pianyi < -15:
  78. deflection_angle = -3
  79. elif 15 <= pianyi < 30:
  80. deflection_angle = 3
  81. elif -50 <= pianyi < -30:
  82. deflection_angle = -5
  83. elif 30 <= pianyi < 50:
  84. deflection_angle = 5
  85. if p_in3.value()==0:
  86. NUMBER1=NUMBER1+5
  87. if p_in4.value()==0:
  88. NUMBER2=NUMBER2+5
  89. if p_in5.value()==0:
  90. NUMBER1=NUMBER1-5
  91. if p_in6.value()==0:
  92. NUMBER2=NUMBER2-5
  93. if NUMBER1>255:
  94. NUMBER1=255
  95. if NUMBER2>255:
  96. NUMBER2=255
  97. if NUMBER1<0:
  98. NUMBER1=0
  99. if NUMBER2<0:
  100. NUMBER2=0
  101. lcd.write(img) # Take a picture and display the image.
  102. byte=uart.read(1)
  103. if byte :
  104. print(byte)
  105. if byte ==b'a':
  106. NUMBER1=NUMBER1+5
  107. if byte ==b'b':
  108. NUMBER2=NUMBER2+5
  109. if byte ==b'c':
  110. NUMBER1=NUMBER1-5
  111. if byte ==b'd':
  112. NUMBER2=NUMBER2-5

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

闽ICP备14008679号