openMv入手

  • Post author:
  • Post category:其他




前言

之前学习了OpenCV的一些基本操作,但终究是图片处理,有些干,回学校后拿到openmv模块开始进行一些相关操作。



openmv是什么

参数

分辨率

还有一些扩展模块可选用,but

扩展

价格劝退,选用了标配版

在这里插入图片描述

在这里插入图片描述

参数来源淘宝,它有没有夸张就不知道了


openmv入手教程


上面这个是官方教程有很多例程讲的也十分详细,对新手十分友好


进阶的openmv教程



一些基本参数



感光元件(也就是你IDE窗口看到的图像)

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对象

sensor.set_pixformat() 设置像素模式。

sensor.GRAYSCALE: 灰度,每个像素8bit。

sensor.RGB565: 彩色,每个像素16bit。

从这里可以看出若转换为灰度像素是减半的

以下列出了图像的一些参数

sensor.set_framesize() 设置图像的大小
sensor.QQCIF: 88x72
sensor.QCIF: 176x144
sensor.CIF: 352x288
sensor.QQSIF: 88x60
sensor.QSIF: 176x120
sensor.SIF: 352x240
sensor.QQQQVGA: 40x30
sensor.QQQVGA: 80x60
sensor.QQVGA: 160x120
sensor.QVGA: 320x240
sensor.VGA: 640x480
sensor.HQQQVGA: 80x40
sensor.HQQVGA: 160x80
sensor.HQVGA: 240x160
sensor.B64X32: 64x32 (用于帧差异 image.find_displacement())
sensor.B64X64: 64x64 用于帧差异 image.find_displacement())
sensor.B128X64: 128x64 (用于帧差异 image.find_displacement())
sensor.B128X128: 128x128 (用于帧差异 image.find_displacement())
sensor.LCD: 128x160 (用于LCD扩展板)
sensor.QQVGA2: 128x160 (用于LCD扩展板)
sensor.WVGA: 720x480 (用于 MT9V034)
sensor.WVGA2:752x480 (用于 MT9V034)
sensor.SVGA: 800x600 (仅用于 OV5640 感光元件)
sensor.XGA: 1024x768 (仅用于 OV5640 感光元件)
sensor.SXGA: 1280x1024 (仅用于 OV5640 感光元件)
sensor.UXGA: 1600x1200 (仅用于 OV5640 感光元件)
sensor.HD: 1280x720 (仅用于 OV5640 感光元件)
sensor.FHD: 1920x1080 (仅用于 OV5640 感光元件)
sensor.QHD: 2560x1440 (仅用于 OV5640 感光元件)
sensor.QXGA: 2048x1536 (仅用于 OV5640 感光元件)
sensor.WQXGA: 2560x1600 (仅用于 OV5640 感光元件)
sensor.WQXGA2: 2592x1944 (仅用于 OV5640 感光元件)

我认为,mv比较nb的地方就是自动增益了,以下开启

sensor.set_auto_gain() 自动增益开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动增益。

sensor.set_auto_whitebal() 自动白平衡开启(True)或者关闭(False)。在使用颜色追踪时,需要关闭自动白平衡。

sensor.set_auto_exposure(enable[\, exposure_us])

enable 打开(True)或关闭(False)自动曝光。默认打开。
如果 enable 为False, 则可以用 exposure_us 设置一个固定的曝光时间(以微秒为单位)。

设置翻转

sensor.set_hmirror(True)

水平方向翻转

sensor.set_vflip(True)

垂直方向翻转 vflip的英文就是垂直镜像

image.difference(image)

从这张图片减去另一个图片。比如,对于每个通道的每个像素点,取相减绝对值操作。这个函数,经常用来做移动检测。



统计信息


使用统计信息



画图


画线

image.draw_line(line_tuple, color=White) 在图像中画一条直线。

line_tuple的格式是(x0, y0, x1, y1),意思是(x0, y0)到(x1, y1)的直线。

颜色可以是灰度值(0-255),或者是彩色值(r, g, b)的tupple。默认是白色


画框

image.draw_rectangle(rect_tuple, color=White) 在图像中画一个矩形框。

rect_tuple 的格式是 (x, y, w, h)。


画圆

image.draw_circle(x, y, radius, color=White) 在图像中画一个圆。

x,y是圆心坐标

radius是圆的半径


画十字

image.draw_cross(x, y, size=5, color=White) 在图像中画一个十字

x,y是坐标

size是两侧的尺寸


写字

image.draw_string(x, y, text, color=White) 在图像中写字 8x10的像素

x,y是坐标。使用\n, \r, and \r\n会使光标移动到下一行。

text是要写的字符串。



测距

# Measure the distance
#
# This example shows off how to measure the distance through the size in imgage
# This example in particular looks for yellow pingpong ball.

import sensor, image, time

# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...
yellow_threshold   = ( 56,   83,    5,   57,   63,   80)
# You may need to tweak the above settings for tracking green things...
# Select an area in the Framebuffer to copy the color settings.

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.

K=5000#the value should be measured

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    blobs = img.find_blobs([yellow_threshold])
    if len(blobs) == 1:
        # Draw a rect around the blob.
        b = blobs[0]
        img.draw_rectangle(b[0:4]) # rect
        img.draw_cross(b[5], b[6]) # cx, cy
        Lm = (b[2]+b[3])/2
        length = K/Lm
        print(length)

    #print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while
    # connected to your computer. The FPS should increase once disconnected.

具体原理


测距



颜色形状同时识别

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)
    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为识别到的圆的区域,即圆的外接矩形框
        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<statistics.l_mode()<100 and 0<statistics.a_mode()<127 and 0<statistics.b_mode()<127:#if the circle is red
            img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
        else:
            img.draw_rectangle(area, color = (255, 255, 255))
            #将非红色的圆用白色的矩形框出来
    print("FPS %f" % clock.fps())



忠告忠告!

手写笔记

实验室有个师兄就这样烧坏一个openmv4,心疼啊T T

电气信息

这个很重要

后续再补神经网络,如果有兴趣的小伙伴等不及可联系我



版权声明:本文为HL_HLT原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。