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

目录

形状识别

圆形检测 

矩形识别

特征点检测

算法的组合使用


形状识别

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

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

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

从官网copy的例程

圆形检测 

# 圆形检测例程
#
# 这个例子展示了如何用Hough变换在图像中找到圆。
# https://en.wikipedia.org/wiki/Circle_Hough_Transform
#
# 请注意,find_circles()方法将只能找到完全在图像内部的圆。圈子之外的
# 图像/ roi被忽略… 可以写在threshold前面,比如说感兴趣区域只在右半边,就写   roi = (80,0,80,120),
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):
        img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
        print(c)
    print("FPS %f" % clock.fps())

import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快
sensor.set_framesize(sensor.QQVGA)  # QQVGA 160*120
sensor.skip_frames(time = 2000)
clock = time.clock()
while(True):
    clock.tick()
    #lens_corr(1.8)畸变矫正,帧率变低
    #img = sensor.snapshot().lens_corr(1.8)
    img = sensor.snapshot()
    # Circle对象有四个值: x, y, r (半径), 和 magnitude。
    # magnitude是检测圆的强度。越高越好
    # roi 是一个用以复制的矩形的感兴趣区域(x, y, w, h)。如果未指定,
    # ROI 即图像矩形。操作范围仅限于roi区域内的像素。
    # x_stride 是霍夫变换时需要跳过的x像素的数量。若已知圆较大,可增加
    # x_stride 。
    # y_stride 是霍夫变换时需要跳过的y像素的数量。若已知直线较大,可增加
    # y_stride 。
    # threshold 控制从霍夫变换中监测到的圆。只返回大于或等于阈值的圆。
    # 应用程序的阈值正确值取决于图像。注意:一条圆的大小是组成圆所有
    # 索贝尔滤波像素大小的总和。 圆
    # x_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin的部分合并。
    # y_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin 的部分合并。
    # r_margin 控制所检测的圆的合并。 圆像素为 x_margin 、 y_margin 和
    # r_margin 的部分合并。
    # r_min,r_max和r_step控制测试圆的半径。
    # 缩小测试圆半径的数量可以大大提升性能。
    # threshold = 3500比较合适。如果视野中检测到的圆过多,请增大阈值;
    # 相反,如果视野中检测到的圆过少,请减少阈值。
    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):
        img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
        print(c)
    print("FPS %f" % clock.fps())

矩形识别

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

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

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

特征点检测

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

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

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

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

算法的组合使用

同时识别颜色和形状

先…后…

 可以进行多次设置

只识别红色的圆,用红色圆圈框选,其他用白色矩形框出来
import sensor, image, time
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
while(True):
    clock.tick()
    #img = sensor.snapshot().lens_corr(1.8) #畸变矫正
    img = sensor.snapshot()
    for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
            r_min = 2, r_max = 100, r_step = 2):
        area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
        #area为识别到的圆的区域,即圆的外接矩形框 roi区域
        statistics = img.get_statistics(roi=area)#像素颜色统计,  统计像素
        print(statistics)
        #(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
        #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
        if 0 

学习视频链接: 

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

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

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