当前位置:   article > 正文

【OpenMV】形状识别 特征点检测 算法的组合使用_openmv形状识别

openmv形状识别

目录

形状识别

圆形检测 

矩形识别

特征点检测

算法的组合使用


形状识别

圆形 霍夫圆检测算法 通过霍夫变换查找圆,支持openmv3以上

矩形 四元检测算法 识别任意大小任意角度的矩形,四元检测算法对图像的失真,畸变没有要求,畸变的图像也可以识别,圆角矩形也可以识别

还可以线段识别 直线识别,实现查找直角,三角形

从官网copy的例程

圆形检测 

  1. # 圆形检测例程
  2. #
  3. # 这个例子展示了如何用Hough变换在图像中找到圆。
  4. # https://en.wikipedia.org/wiki/Circle_Hough_Transform
  5. #
  6. # 请注意,find_circles()方法将只能找到完全在图像内部的圆。圈子之外的
  7. # 图像/ roi被忽略… 可以写在threshold前面,比如说感兴趣区域只在右半边,就写 roi = (80,0,80,120),
  8. for c in img.find_circles(roi = (80,0,80,120), threshold = 2300, x_margin = 100, y_margin = 10, r_margin = 10,r_min = 20, r_max = 100, r_step = 2):
  9. img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
  10. print(c)
  11. print("FPS %f" % clock.fps())
  12. import sensor, image, time
  13. sensor.reset()
  14. sensor.set_pixformat(sensor.RGB565) # 灰度更快
  15. sensor.set_framesize(sensor.QQVGA) # QQVGA 160*120
  16. sensor.skip_frames(time = 2000)
  17. clock = time.clock()
  18. while(True):
  19. clock.tick()
  20. #lens_corr(1.8)畸变矫正,帧率变低
  21. #img = sensor.snapshot().lens_corr(1.8)
  22. img = sensor.snapshot()
  23. # Circle对象有四个值: x, y, r (半径), 和 magnitude。
  24. # magnitude是检测圆的强度。越高越好
  25. # roi 是一个用以复制的矩形的感兴趣区域(x, y, w, h)。如果未指定,
  26. # ROI 即图像矩形。操作范围仅限于roi区域内的像素。
  27. # x_stride 是霍夫变换时需要跳过的x像素的数量。若已知圆较大,可增加
  28. # x_stride 。
  29. # y_stride 是霍夫变换时需要跳过的y像素的数量。若已知直线较大,可增加
  30. # y_stride 。
  31. # threshold 控制从霍夫变换中监测到的圆。只返回大于或等于阈值的圆。
  32. # 应用程序的阈值正确值取决于图像。注意:一条圆的大小是组成圆所有
  33. # 索贝尔滤波像素大小的总和。 圆
  34. # x_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
  35. # r_margin的部分合并。
  36. # y_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
  37. # r_margin 的部分合并。
  38. # r_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
  39. # r_margin 的部分合并。
  40. # r_min,r_max和r_step控制测试圆的半径。
  41. # 缩小测试圆半径的数量可以大大提升性能。
  42. # threshold = 3500比较合适。如果视野中检测到的圆过多,请增大阈值;
  43. # 相反,如果视野中检测到的圆过少,请减少阈值。
  44. for c in img.find_circles(threshold = 2300, x_margin = 100, y_margin = 10, r_margin = 10,r_min = 20, r_max = 100, r_step = 2):
  45. img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
  46. print(c)
  47. print("FPS %f" % clock.fps())

矩形识别

  1. 矩形识别
  2. # Find Rects Example
  3. #
  4. # 这个例子展示了如何使用april标签代码中的四元检测代码在图像中找到矩形。 四元检测算法以非常稳健的方式检测矩形,并且比基于Hough变换的方法好得多。 例如,即使镜头失真导致这些矩形看起来弯曲,它仍然可以检测到矩形。 圆角矩形是没有问题的!
  5. # (但是,这个代码也会检测小半径的圆)...
  6. import sensor, image, time
  7. sensor.reset()
  8. sensor.set_pixformat(sensor.RGB565) # 灰度更快(160x120 max on OpenMV-M7)
  9. sensor.set_framesize(sensor.QQVGA)
  10. sensor.skip_frames(time = 2000)
  11. clock = time.clock()
  12. while(True):
  13. clock.tick()
  14. img = sensor.snapshot()
  15. # 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有
  16. # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。
  17. for r in img.find_rects(roi =(21,1,68,54), threshold = 10000): #设置了roi,只在左上角一小部分识别
  18. img.draw_rectangle(r.rect(), color = (255, 0, 0))
  19. for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
  20. print(r)
  21. print("FPS %f" % clock.fps())

检测矩形的阈值应该设置高一些,电赛期间,我用的18000,这样检测的效果会更好一些,根据室内室外环境来调整这个threshold

for r in img.find_rects(roi =(21,1,68,54), threshold = 18000):

特征点检测

模板匹配:尽量让目标物体不要变换角度也不要变换大小,识别比较单一,不准确,固定距离的识别,角度也不行! 最多保存10张,过多图片导致内存问题

特征点检测:识别目标物体的多个大小多个角度,比较灵活,在检测前保存目标物体的特征

程序运行的前10s提取目标物体的特征,记录为目标特征,后续与这个目标特征做对比,来判断是否识别到目标

  1. # 利用特征点检测特定物体例程。
  2. # 向相机显示一个对象,然后运行该脚本。 一组关键点将被提取一次,然后
  3. # 在以下帧中进行跟踪。 如果您想要一组新的关键点,请重新运行该脚本。
  4. # 注意:请参阅文档以调整find_keypoints和match_keypoints。
  5. import sensor, time, image
  6. # 重置传感器
  7. sensor.reset()
  8. # 传感器设置
  9. sensor.set_contrast(3) # 设置对比度为3
  10. sensor.set_gainceiling(16) # 增益上限为16
  11. sensor.set_framesize(sensor.VGA)
  12. sensor.set_windowing((320, 240)) #截取窗口
  13. sensor.set_pixformat(sensor.GRAYSCALE) #灰度图模式
  14. sensor.skip_frames(time = 2000) #跳过2S
  15. sensor.set_auto_gain(False, value=100) #自动增益,关闭
  16. #画出特征点
  17. def draw_keypoints(img, kpts):
  18. if kpts:
  19. print(kpts)
  20. img.draw_keypoints(kpts)
  21. img = sensor.snapshot()
  22. time.sleep_ms(1000)
  23. kpts1 = None
  24. #kpts1保存目标物体的特征,可以从文件导入特征,但是不建议这么做。
  25. #kpts1 = image.load_descriptor("/desc.orb")
  26. #img = sensor.snapshot()
  27. #draw_keypoints(img, kpts1)
  28. clock = time.clock()
  29. while (True):
  30. clock.tick()
  31. img = sensor.snapshot()
  32. if (kpts1 == None):
  33. #如果是刚开始运行程序,提取最开始的图像作为目标物体特征,kpts1保存目标物体的特征
  34. #默认会匹配目标特征的多种比例大小,而不仅仅是保存目标特征时的大小,比模版匹配灵活。
  35. # NOTE: By default find_keypoints returns multi-scale keypoints extracted from an image pyramid.
  36. kpts1 = img.find_keypoints(max_keypoints=150, threshold=10, scale_factor=1.2)
  37. #image.find_keypoints(roi=Auto, threshold=20, normalized=False, scale_factor=1.5, max_keypoints=100, corner_detector=CORNER_AGAST)
  38. #roi表示识别的区域,是一个元组(x,y,w,h),默认与framsesize大小一致。
  39. #threshold是0~255的一个阈值,用来控制特征点检测的角点数量。用默认的AGAST特征点检测,这个阈值大概是20。用FAST特征点检测,这个阈值大概是6080。阈值越低,获得的角点越多。
  40. #normalized是一个布尔数值,默认是False,可以匹配目标特征的多种大小(比ncc模版匹配效果灵活)。如果设置为True,关闭特征点检测的多比例结果,仅匹配目标特征的一种大小(类似于模版匹配),但是运算速度会更快一些。
  41. #scale_factor是一个大于1.0的浮点数。这个数值越高,检测速度越快,但是匹配准确率会下降。一般在1.35~1.5左右最佳。
  42. #max_keypoints是一个物体可提取的特征点的最大数量。如果一个物体的特征点太多导致RAM内存爆掉,减小这个数值。
  43. #corner_detector是特征点检测采取的算法,默认是AGAST算法。FAST算法会更快但是准确率会下降。
  44. draw_keypoints(img, kpts1)
  45. #画出此时的目标特征
  46. else:
  47. # 当与最开始的目标特征进行匹配时,默认设置normalized=True,只匹配目标特征的一种大小。
  48. # NOTE: When extracting keypoints to match the first descriptor, we use normalized=True to extract
  49. # keypoints from the first scale only, which will match one of the scales in the first descriptor.
  50. kpts2 = img.find_keypoints(max_keypoints=150, threshold=10, normalized=True)
  51. #如果检测到特征物体
  52. if (kpts2):
  53. #匹配当前找到的特征和最初的目标特征的相似度
  54. match = image.match_descriptor(kpts1, kpts2, threshold=85)
  55. #image.match_descriptor(descritor0, descriptor1, threshold=70, filter_outliers=False)。本函数返回kptmatch对象。
  56. #threshold阈值设置匹配的准确度,用来过滤掉有歧义的匹配。这个值越小,准确度越高。阈值范围0100,默认70
  57. #filter_outliers默认关闭。
  58. #match.count()是kpt1和kpt2的匹配的近似特征点数目。
  59. #如果大于10,证明两个特征相似,匹配成功。
  60. if (match.count()>10):
  61. # If we have at least n "good matches"
  62. # Draw bounding rectangle and cross.
  63. #在匹配到的目标特征中心画十字和矩形框。
  64. img.draw_rectangle(match.rect())
  65. img.draw_cross(match.cx(), match.cy(), size=10)
  66. #match.theta()是匹配到的特征物体相对目标物体的旋转角度。
  67. print(kpts2, "matched:%d dt:%d"%(match.count(), match.theta()))
  68. # 不建议draw_keypoints画出特征关键点。
  69. # 注意:如果你想绘制关键点,取消注释
  70. #img.draw_keypoints(kpts2, size=KEYPOINTS_SIZE, matched=True)
  71. #打印帧率。
  72. img.draw_string(0, 0, "FPS:%.2f"%(clock.fps()))

算法的组合使用

同时识别颜色和形状

先…后…

 可以进行多次设置

  1. 只识别红色的圆,用红色圆圈框选,其他用白色矩形框出来
  2. import sensor, image, time
  3. sensor.reset()
  4. sensor.set_pixformat(sensor.RGB565)
  5. sensor.set_framesize(sensor.QQVGA)
  6. sensor.skip_frames(time = 2000)
  7. sensor.set_auto_gain(False) # must be turned off for color tracking
  8. sensor.set_auto_whitebal(False) # must be turned off for color tracking
  9. clock = time.clock()
  10. while(True):
  11. clock.tick()
  12. #img = sensor.snapshot().lens_corr(1.8) #畸变矫正
  13. img = sensor.snapshot()
  14. for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
  15. r_min = 2, r_max = 100, r_step = 2):
  16. area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
  17. #area为识别到的圆的区域,即圆的外接矩形框 roi区域
  18. statistics = img.get_statistics(roi=area)#像素颜色统计, 统计像素
  19. print(statistics)
  20. #(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
  21. #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
  22. if 0<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
  23. img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
  24. else:
  25. img.draw_rectangle(area, color = (255, 255, 255))
  26. #将非红色的圆用白色的矩形框出来
  27. print("FPS %f" % clock.fps())

学习视频链接: 

OpenMV形状识别 | 星瞳科技 (singtown.com)

OpenMV特征点检测 | 星瞳科技 (singtown.com)

 OpenMV算法的组合使用 | 星瞳科技 (singtown.com)

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

闽ICP备14008679号