openmv入门之路

如果你发现OpenCV函数繁多!!!

如果你发现OpenCV配置繁琐!!!

如果你发现自己买不起miniPC!!!

请选择OpenMV来完成你的项目吧!!!

新篇章新阶段,来自其他小白提醒

  • OpenMV简介

OpenMV,其目标是成为机器视觉界的“Arduino”,它以STM32CPU为核心,提供Python的编程接口,成本低,拓展性好,不仅能够用来进行图像处理,还可以当作一个普通的单片机使用。OpenMV的像素较小,所以适用范围是一些对精度要求不高的视觉任务,例如:扫码,色块检测,目标跟踪,以及 一些简单的机器学习任务(当然,这里仅仅介绍与视觉有关的内容,除非你想用它去做嵌入式开发)。

  • OpenMV的安装

OpenMV的安装不需要像OpenMV一样配置一系列的环境,非常简便!!!
具体安装步骤可参考官网:

OpenMV IDE安装 | 星瞳科技​singtown.com/learn/49989/

  • OpenMV快速上手

如果你想尽快的对OpenMV的功能做出了解,并能够通过代码去实现简单的功能,可以参考如下材料:

10分钟快速上手 · OpenMV中文入门教程​book.openmv.cc/quick-starter.html正在上传…重新上传取消

  • 基于例程进行代码移植

如果我们去网页搜索我们想要完成的任务,我们搜到的基本都是使用OpenCV实现的代码,关于OpenMV的博客显然屈指可数,在这种情况下IDE中的示例则显得尤为重要,而官方的函数手册则更为重要了。

例如我们想进行色块检测并打印图中的色块摆放的顺序结果编码(如图从左向右从上向下编码为123312)1——红,2——绿,3——蓝:

首先我们可以在示例中发现multi_color_blob_tracking.py的例程:

# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.

import sensor, image, time, math

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
              (30, 100, -64, -8, -32, 32), # generic_green_thresholds
              (0, 15, 0, 40, -80, -20)] # generic_blue_thresholds
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.

while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
        # These values depend on the blob not being circular - otherwise they will be shaky.
        if blob.elongation() > 0.5:
            img.draw_edges(blob.min_corners(), color=(255,0,0))
            img.draw_line(blob.major_axis_line(), color=(0,255,0))
            img.draw_line(blob.minor_axis_line(), color=(0,0,255))
        # These values are stable all the time.
        img.draw_rectangle(blob.rect())
        img.draw_cross(blob.cx(), blob.cy())
        # Note - the blob rotation is unique to 0-180 only.
        img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20)
    print(clock.fps())

 

根据例程如何得到我们想要的程序呢?代码中的img.find_blobs()很明显是一个Lists,代表着很多个色块对象,我们想要获取色块位置就需要知道包络色块的矩形框的信息,这些信息都包含在这个对象的成员中。这就需要用到函数手册

https://docs.openmv.io/​docs.openmv.io/

我们搜索Blobs这个类可以看到其成员,之后的事情就是我们自己写代码了,通过调用其成员对色块进行筛选和处理,最终打印位置编码

最终的程序和结果如下:

#color_test

# Multi Color Blob Tracking Example
#
# This example shows off multi color blob tracking using the OpenMV Cam.

import sensor, image, time, math

# Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max)
# The below thresholds track in general red/green things. You may wish to tune them...
thresholds = [(38, 65, 49, 114, -8, 113), # generic_red_thresholds
              (30, 80, -62, -33, -30, 74), # generic_green_thresholds
              (27, 56, -40, 59, -86, -41)] # generic_blue_thresholds
# You may pass up to 16 thresholds above. However, it's not really possible to segment any
# scene with 16 thresholds before color thresholds start to overlap heavily.
thresholds_red=((38, 65, 49, 114, -8, 113))
thresholds_green=((30, 80, -62, -33, -30, 74))
thresholds_blue=((27, 56, -40, 59, -86, -41))
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
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()

# Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are
# returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the
# camera resolution. Don't set "merge=True" becuase that will merge blobs which we don't want here.

while(True):
    clock.tick()
    img = sensor.snapshot()
    
    blobs_red=img.find_blobs([thresholds_red], pixels_threshold=200, area_threshold=200)
    blobs_green=img.find_blobs([thresholds_green], pixels_threshold=200, area_threshold=200)
    blobs_blue=img.find_blobs([thresholds_blue], pixels_threshold=200, area_threshold=200)
    
    for blobs in blobs_red:
        img.draw_rectangle(blobs.rect())
    for blobs in blobs_green:
        img.draw_rectangle(blobs.rect())
    for blobs in blobs_blue:
        img.draw_rectangle(blobs.rect())
    
    if len(blobs_red)==2:
        if blobs_red[0].cy()<blobs_red[1].cy():
            blobs_red_on=blobs_red[0]
            blobs_red_under=blobs_red[1]            
        else:
            blobs_red_on=blobs_red[1]
            blobs_red_under=blobs_red[0]            
    else:
        continue

    if len(blobs_green)==2:
        if blobs_green[0].cy()<blobs_green[1].cy():
            blobs_green_on=blobs_green[0]
            blobs_green_under=blobs_green[1]            
        else:
            blobs_green_on=blobs_green[1]
            blobs_green_under=blobs_green[0]
    else:
        continue

    if len(blobs_blue)==2:
        if blobs_blue[0].cy()<blobs_blue[1].cy():
            blobs_blue_on=blobs_blue[0]
            blobs_blue_under=blobs_blue[1]            
        else:
            blobs_blue_on=blobs_blue[1]
            blobs_blue_under=blobs_blue[0]
    else:
        continue

    color_list="abcdef"

    list_on=[blobs_red_on.cx(),blobs_green_on.cx(),blobs_blue_on.cx()]
    list_under=[blobs_red_under.cx(),blobs_green_under.cx(),blobs_blue_under.cx()]

    if min(list_on)==blobs_red_on.cx():
        color_list=color_list.replace('a','1')
        if max(list_on)==blobs_green_on.cx():
            color_list=color_list.replace('c','2')
            color_list=color_list.replace('b','3')
        else:
            color_list=color_list.replace('c','3')
            color_list=color_list.replace('b','2')
            
    if min(list_on)==blobs_green_on.cx():
        color_list=color_list.replace('a','2')
        if max(list_on)==blobs_red_on.cx():
            color_list=color_list.replace('c','1')
            color_list=color_list.replace('b','3')
        else:
            color_list=color_list.replace('c','3')
            color_list=color_list.replace('b','1') 
            
    if min(list_on)==blobs_blue_on.cx():
        color_list=color_list.replace('a','3')
        if max(list_on)==blobs_red_on.cx():
            color_list=color_list.replace('c','1')
            color_list=color_list.replace('b','2')
        else:
            color_list=color_list.replace('c','2')
            color_list=color_list.replace('b','1')
            
    if min(list_under)==blobs_red_under.cx():
        color_list=color_list.replace('d','1')
        if max(list_under)==blobs_green_under.cx():
            color_list=color_list.replace('f','2')
            color_list=color_list.replace('e','3')
        else:
            color_list=color_list.replace('f','3')
            color_list=color_list.replace('e','2')

    if min(list_under)==blobs_green_under.cx():
        color_list=color_list.replace('d','2')
        if max(list_under)==blobs_red_under.cx():
            color_list=color_list.replace('f','1')
            color_list=color_list.replace('e','3')
        else:
            color_list=color_list.replace('f','3')
            color_list=color_list.replace('e','1')

    if min(list_under)==blobs_blue_under.cx():
        color_list=color_list.replace('d','3')
        if max(list_under)==blobs_red_under.cx():
            color_list=color_list.replace('f','1')
            color_list=color_list.replace('e','2')
        else:
            color_list=color_list.replace('f','2')
            color_list=color_list.replace('e','1')

    print(color_list)
    
    print(clock.fps())

输出的是“123312”的编码以及帧率。

这里借助这个小例程分析如何基于例程进行程序的移植,希望能够对刚入手OpenMV的小伙伴有所启发。

类似的移植方法也可以用来进行串口通信,二维码的扫描等这些常用的功能。

  • 常用小工具

OpenMV提供了许多帮助调试的小工具,可以在界面——工具一栏中找到,例如在上述例程中需要标定thresholds,则点击“工具——视觉终端——阈值编辑器”可以方便的调整阈值进行标定,只需要记录下各阈值即可。

希望可以为大家快速上手OpenMV有所帮助!!!

参考资料:

OpenMV IDE安装 | 星瞳科技​singtown.com/learn/49989/

Overview — MicroPython 1.15 documentation

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被大佬糊弄的只会点灯的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值