OpenMv学习总结(满满干货)

2020-02-02  本文已影响0人  Sunshine_晗晗

本文主要记录了学习OpenMv过程中的点滴。它跟OpenCv其实是完全不同的两个东西,大家可不要被迷惑噢。

具体可以参考[1]: https://docs.singtown.com/micropython/zh/latest/openmvcam/openmvcam/quickref.html "Openmv"

OpenMv简介

简单来说,OpenMv就是一个可编程的摄像头,通过使用python语言,专门用作嵌入式当中的视觉感光元件。下面将介绍Openmv使用过程中的常用知识。

感光元件

感光元件即sensor模块,与摄像头有关。看如下例子:

import sensor#引入感光元件的模块
sensor.reset()#初始化感光元件
sensor.set_pixformat(sensor.RGB565)#设置为彩色,用到其它参数时再上官方文档里找,下同
sensor.set_framesize(sensor.QVGA)#设置图像的大小
sensor.skip_frames()#跳过n张照片,在更改设置后,跳过一些帧,等待感光元件变稳定。

while(True):
    img = sensor.snapshot()#拍摄一张照片,img为一个image对象

ROI区域

sensor.set_framesize(sensor.VGA) # 高分辨率
sensor.set_windowing((640, 80)) #取中间的640*80区域

获取/设置像素点

img = sensor.snapshot()
img.get_pixel(10,10)
img.set_pixcel(10,10,(255,0,0))#设置坐标(10,10)的像素点为红色(255,0,0)

图像的运算

画图

image.draw_line(line_tuple, color=White) 在图像中画一条直线,其中line_tuple的格式为(x0, y0, x1, y1),即从某点到某点的直线

image.draw_rectangle(rect_tuple, color=White) 在图像中画一个矩形框,其中rect_tuple 的格式是 (x, y, w, h)。

这里只举如上两个例子,其它的用到再去查即可。

添加自定义模块

OpenMv存在一个文件系统,根目录下有一个main.py,把你需要导入的自定义模块复制到根目录下即可。

操作LED

from pyb import LED

led = LED(1) # 红led
led.toggle()
led.on()#亮
led.off()#灭

操作IO口

from pyb import Pin

p_out = Pin('P7', Pin.OUT_PP)#设置p_out为输出引脚
p_out.high()#设置p_out引脚为高
p_out.low()#设置p_out引脚为低
p_in = Pin('P7', Pin.IN, Pin.PULL_UP)#设置p_in为输入引脚,并开启上拉电阻
value = p_in.value() # get value, 0 or 1#读入p_in引脚的值

可以看出,pyb模块里各种外设的操作和32类似,易上手。

控制舵机

from pyb import Servo

s1 = Servo(1) # servo on position 1 (P7)
s1.angle(45) # move to 45 degrees
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
s1.speed(50) # for continuous rotation servos

具体需要用到哪个舵机还是需要参照原理图的结构。

定时器

from pyb import Timer

tim = Timer(4, freq=1000)
tim.counter() # get counter value
tim.freq(0.5) # 0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle())  //类似于32的回调函数

IO中断

from pyb import Pin, ExtInt

callback = lambda e: print("intr")
ext = ExtInt(Pin('P7'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)

PWM输出

from pyb import Pin, Timer

p = Pin('P7') # P7 has TIM4, CH1
tim = Timer(4, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)

UART

from pyb import UART

uart = UART(3, 9600)
uart.write('hello')
uart.read(5) # read up to 5 bytes

Openmv应用(寻找色块)

image.find_blobs(thresholds, roi=Auto, x_stride=2, y_stride=1, invert=False, area_threshold=10, pixels_threshold=10, 
merge=False, margin=0, threshold_cb=None, merge_cb=None)

颜色阙值的结构

red = (minL, maxL, minA, maxA, minB, maxB)

但是OpenMv如此强大,在IDE工具栏里的Machine Vision中有阙值调试工具,效果图如下:

效果

BLOB色块对象

能看到这里的人真不容易啊,你们都是大学霸!!!

OpenMv学习之串口的使用

先来看如下一段代码:

# Blob Detection and uart transport
import sensor, image, time
from pyb import UART
import json

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.

uart = UART(3, 115200)

while(True):
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if blobs:
        print('sum :', len(blobs))
        output_str = json.dumps(blobs) # 在这里串口发送的数据中我们用了json格式,因为它使用起来简单方便,但缺点是占用的资源较多
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b.rect()) # rect
            img.draw_cross(b.cx(), b.cy()) # cx, cy

        print('you send:',output_str)
        uart.write(output_str+'\n')
    else:
        print('not found!')

在这里,我们用串口来不断发送blob数据,而寻找色块的过程跟opencv的思路相似,在前边也介绍过,只不过在openmv中更简便。

当用openmv的串口发送完数据后,接收端需要进行相应的解析才能得到想要的数据,前提是你需要知道发送数据的格式并撰写相应的函数。

本文结束,谢谢阅读

上一篇 下一篇

猜你喜欢

热点阅读