openmv机器视觉


openmv机器视觉

摄像头应用

”文件“—-“示例”—-“Basics”—-“helloworld”

理解OpenMV4摄像头基本编程和配置原理

OpenMV将摄像头功能封装到Sensor模块中

sensor模块:

  • 构造函数 sensor 摄像头对象,通过import直接调用
  • 使用方法:
    • sensor.reset() 初始化摄像头 清除掉之前摄像头存在的代码对于图片的设置
    • sensor.set_pixformat(pixformat)
      • 设置像素格式,pixformat有3个参数
      • sensor.GRAYSCALE:灰度图像,每像素一个字节
      • sensor.RGB565:每像素为16位,5位用于红色,6位用于绿色,5位用于蓝色
      • sensor.BAYER:占空间小,仅捕捉图像用,不可做图像处理
    • sensor.set_framesize(framesize)
      • 设置每帧大小(即图像尺寸),常用的framesize参数:
      • sensor.QQVGA:160*120
      • sensor.QVGA:320*240
      • sensor.VGA:640*480
    • sensor.skip_frames([n,time]) 跳过一些刚开始不稳定的时候再开始读取图像
      • 摄像头配置后跳过n帧或者等待时间time让其稳定,n:跳过帧数,time:等待时间,单位ms
      • 如果n和time均没指定,则默认跳过300ms的帧
    • sensor.snapshot() 截取sensor摄像头的视频流的当前帧,给img对象
      • 使用相机拍摄一张照片,并返回image对象

time模块:(区别于utime对象(计时器,延时器),Timer对象(闹钟))

  • 构造函数:
    • clock=time.clock() 创建一个闹钟
  • 使用方法:
    • clock.tick() 开始追踪运行时间 更新图像的帧率
    • clock.fps()
      • 停止追踪运行时间,并返回当前FPS(每秒帧数)
      • 在调用该函数前始终首先调用tick
#导入sensor,time模块
import sensor,time,image
#初始化和配置sensor模块
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
snesor.set_framesize(QVGA)
sensor.skip_frames(time=2000)
#创建一个计时闹钟
clock=time.clock

while True:
    clock.tick()
    img=sensor.snapstop()
    print(clock.fps())

画图

“Drawing”—-“…….”

采集到照片后,我们会进行一些处理,比如在图片某个位置标记箭头,人脸识别后用矩形框提示

实验目的:在拍摄的图片上画各种图形

image对象画图功能:

  • 构造函数:创建图像,通过拍摄或者读取文件路径获取
    • img=sensor.snapshot()
    • img=image.Image(path[copy_to_fb=False])
      • copy_to_fb=True:可以加载大图片
      • copy_to_fb=False:不可以加载大图片
    • 示例:img=image.Image(“”,copy_to_fb=True)
  • 使用方法:
    • image.draw_line(x0,y0,x1,y1[color[thickness=1]])
      • 画线段,(x0,y0)起始坐标,(x1,y1)终点坐标
      • color颜色,如(255,0,0)表示红色
      • thickness:粗细
    • image.draw_rectangle(x,y,w,h,[color,[thickness=1,[fill=False]]])
      • 画矩形 ,(x,y)起始坐标
      • w:宽度,h:长度
      • color:颜色
      • thickness边框粗细
      • fill:是否填冲
    • image.draw_circle(x,y,radius,[color],[thickness]=1,[fill=False])
      • 画圆 (x,y):圆心,radius:半径
      • color颜色
      • thickness:线条粗细
      • fill:是否填冲
    • image.draw_string(x,y,text,[color],[scale=1],[mono_space=True])
      • 写字符 (x,y):起始坐标,text:字符内容
      • color:颜色
      • scale:字体大小
      • mono_space:强制间距
import pyb,time,sensor,image

sensor.reset()
sensor.set_pixforamt(sensor.RGB565)
sensor.set_framesize(QVGA)
sensor.skip_frames(time=2000)

clock=time.clock()

while True:
    clock.tick()
    img=image.snapshot()
    #画线短
    img.draw_line(20,20,100,20,color=(255,0,0),thickness=2)
    #画矩形
    img.draw_rectangle(150,20,100,30,color=(0,255,0),thickness=2,fill=False)
    #画圆
    img.draw_circle(60,120,30,color=(0,0,255),thickness=2,fill=False)
    #写字符
    img.draw_string(150,200,"1doctorc1",color=(255,255,255),scale=2,momo_space=False)
    print(clock.fps())

特征检测

特征检测,对图像进行特定的特征检测,如:边缘检测、各种形状识别,特征点识别,线性回归

特征检测位于Feature–Detection

边缘检测

轮廓

识别摄像头采集的图像轮廓并显示

重要函数:find_edges,位于image模块下

边缘处理函数:

  • 构造函数:image.find_edges(edge_type,[threshold])
    • 边缘检测,将图像变为黑白,边缘保留白色像素
    • 【edge_type】处理方式;
      • image.EDGE_SIMPLE - 简单的阈值高通滤波算法
      • image.EDGE_CANNY - Canny边缘检测算法
    • 【threshold】包含高、低阈值的二元值,默认是(100,200),仅支持灰度图像
  • 直接调用该函数
#导入sensor等相关模块
import sensor,image,time
#初始化和配置sensor模块
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(QVGA)
sensor.skip_frames(time=2000)#延时让摄像头稳定
sensor.set_gainceiling(8)#设置增益,这是官方推荐的参数
clock=time.clock()

while True:
    clock.tick()
    img=sensor.snapshot()#拍摄并返回图像
    #使用Canny边缘检测器
    image.find_edges(image.EDGE_Canny,threshold=(50,80))
    #也可以使用简单快速边缘检测
    image.find_edges(image.EDGE_SIMPLE,threshold=(100,255))
    print(clock.fps())

图形识别

示例:圆形

识别图像中的圆形并用画圆功能指示出来

重要函数:find_circles位于image模块下

圆形识别函数:

  • 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)
  • 找圆函数,返回一个image.circle圆形对象,该圆形对象有4个值:x,y(圆心),r(半径),magnitude(量级):量级越大说明识别到的圆的可信度越高
  • 【roi】识别区域(x,y,w,h),未指定则默认整张图片
  • 【threshold】阈值。返回大于或等于threshold的圆,调整识别可信度,
  • 【x_stride】【y_stride】霍夫变换时跳过x,y像素的量
  • 【x_margin】【y_margin】【r_margin】控制所检测圆的合并调节
  • 【r_min】【r_max】控制识别圆形的半径范围
  • 【r_step】控制识别步骤
  • 直接调用该函数,大部分参数使用默认
import sensor,image,time
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)#
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
clock=time.clock()
while True:
    clock.tick()
    img=sensor.snapshot().ens_corr(1.8)#lens_corr用于去除畸变
    #threshold数值的提升会降低识别圆形的总数
    for c in img.find_circles(threshold=2000,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())

也可用该历程识别圆形图案或者球类物体

矩形识别

识别图像中的矩形并用画图功能指示出来

openmv集成了矩形识别find_rects函数,位于image模块下

矩形识别函数:

  • image.find_rects([roi=Auto],threshold=10000)
    • 矩形识别函数,返回一个image.rect矩形对象列表
    • 【roi】识别区域(x,y,w,h),未指定则默认整张图片
    • 【threshold】阈值,返回大于或等于threshold的矩形,调整识别可信度
  • 直接调用该函数
#使用quad阈值在图像中找到矩形
#四元阈值检测算法检测矩形使用鲁棒的方式
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()

    for r in img.find_rexts(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())

特征点识别


文章作者: 1doctorc1
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 1doctorc1 !
  目录