Open MV形状识别

大标题下面的代码可以直接使用

圆形识别用的是霍夫圆检测算法

矩形识别用的是四元检测算法,运用在Apriltag。

特点:识别任意大小,任意角度的矩形,灵活。对于图像失真、畸变是没有要求的

一、圆形识别

find_circles调参

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度更快
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(1.8)
    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):
        img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
        print(c)

    print("FPS %f" % clock.fps())
image.find_circles([roi[, x_stride=2[, y_stride=1[, threshold=2000[, x_margin=10[, y_margin=10[, r_margin=10[, r_min=2[, r_max[, r_step=2]]]]]]]]]])

# x_stride=2,查找在x方向上像素点数大于2的圆。
# 如果x_stride=5,那么就是查找在x方向上像素点数大于5的圆。      
# 圆比较大,那就增加x_stride值


# threshold  就是进行霍夫检测时返回的所有符合索贝尔滤波的像素点。相当于一个阈值
# 如果检测的圆比较多,比较杂。可以调大threshold数值。/反之,调小。

# x_margin=10    默认为10,,,

如果两个圆相距的距离小于10个像素点的话这两个圆就会被合成一个大圆。

两个圆相距的距离大于10个像素点的话这两个圆就会被合成一个大圆。

        x,y,r同理

二、矩形识别

# (但是,这个代码也会检测小半径的圆)...

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()

    for r in img.find_rects(threshold = 10000):
        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())

1,改变图形识别的区域

如果我们只想让其识别到右边图形,我们只需要根据,,,sensor.QQVGA判断出其是160x120 分辨率的相机传感器。通来设置roi区域的值。roi=(80,60,80,60)

只需要添加roi区域,不添加默认是全屏,roi 是一个用以复制的矩形的感兴趣区域(x, y, w, h).

find_rects(roi=(80,60,80,60),threshold = 10000):

2,find_rects调参

与圆改参数相似

find_rects(threshold = 10000):

 san

 for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))

 corners() 方法应该返回矩形四个角点的坐标,每个角点坐标是一个包含两个值的元组,第一个值是x坐标,第二个值是y坐标。

  • p[0]:角点的x坐标。
  • p[1]:角点的y坐标。
  • 5:圆圈的半径,表示圆圈的大小。
  • color = (0, 255, 0):圆圈的颜色,这里指定为绿色(在RGB颜色空间中,(0, 255, 0)代表绿色)。

三、直线识别

enable_lens_corr = False

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) 
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    if enable_lens_corr: img.lens_corr(1.8) 
    for l in img.find_line_segments(merge_distance = 0, max_theta_diff = 5):
        img.draw_line(l.line(), color = (255, 0, 0))
        # print(l)

    print("FPS %f" % clock.fps())

find_line_segments调参

find_line_segments(merge_distance = 0, max_theta_diff = 5):

merge_distance = 0,意思是两条线距离为0,两条线合并

四、识别三角形

enable_lens_corr = False

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.RGB565) 
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    if enable_lens_corr: img.lens_corr(1.8) 
        
        
    sum = 0
    for l in img.find_line_segments(merge_distance = 5, max_theta_diff = 5):
        img.draw_line(l.line(), color = (255, 0, 0))
        sum += l.theta()

    sum -= 90
    print(sum)
    if sum<200 and sum>160:
        print('ok')
    else:
        print('not')

    # print("FPS %f" % clock.fps())

通过来判断图中三条线围围成的内角和,来判断是否为三角形并输出。(但算法存在小小的缺陷,因为sum -=不一定为-90)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值