图像处理篇---基本OpenMV图像处理


文章目录

  • 前言
  • [1. 灰度化(Grayscale)](#1. 灰度化(Grayscale))
  • [2. 二值化(Thresholding)](#2. 二值化(Thresholding))
  • [3. 掩膜(Mask)](#3. 掩膜(Mask))
  • [4. 腐蚀(Erosion)](#4. 腐蚀(Erosion))
  • [5. 膨胀(Dilation)](#5. 膨胀(Dilation))
  • [6. 缩放(Scaling)](#6. 缩放(Scaling))
  • [7. 旋转(Rotation)](#7. 旋转(Rotation))
  • [8. 平移(Translation)](#8. 平移(Translation))
  • [9. 边缘检测(Edge Detection)](#9. 边缘检测(Edge Detection))
  • [10. 轮廓检测(Contour Detection)](#10. 轮廓检测(Contour Detection))
  • 11.总结
  • 总结

前言

本文仅仅简单介绍了Openmv中常见的图像处理操作(灰度化、掩膜、二值化、腐蚀、膨胀、缩放、旋转、平移、边缘检测、轮廓检测)


1. 灰度化(Grayscale)

将彩色图像转换为灰度图像,减少计算量。

实现方法:

python 复制代码
import sensor

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)  # 设置为灰度模式
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()  # 捕获灰度图像

2. 二值化(Thresholding)

将灰度图像转换为黑白图像,通过设定阈值分离目标区域。

实现方法:

python 复制代码
import sensor

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

threshold = (100, 255)  # 阈值范围

while True:
    img = sensor.snapshot()
    img.binary([threshold])  # 二值化处理

3. 掩膜(Mask)

通过掩膜操作提取图像中的特定区域。

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

mask = image.Image(size=(100, 100), copy_to_fb=True)  # 创建掩膜
mask.draw_rectangle(20, 20, 60, 60, color=255, fill=True)  # 在掩膜上绘制白色矩形

while True:
    img = sensor.snapshot()
    img.mask(mask)  # 应用掩膜

4. 腐蚀(Erosion)

去除图像中的细小噪声,使目标区域缩小

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.erode(1)  # 腐蚀操作,参数为腐蚀次数

5. 膨胀(Dilation)

填充目标区域中的空洞,使目标区域扩大

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.dilate(1)  # 膨胀操作,参数为膨胀次数

6. 缩放(Scaling)

调整图像大小。

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.scale(x_scale=0.5, y_scale=0.5)  # 缩放为原来的一半

7. 旋转(Rotation)

旋转图像。

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.rotation_corr(angle=45)  # 旋转45度

8. 平移(Translation)

平移图像。

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.translation(x_offset=10, y_offset=10)  # 向右下方平移10像素

9. 边缘检测(Edge Detection)

检测图像中的边缘

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    img.find_edges(image.EDGE_CANNY, threshold=(50, 80))  # Canny边缘检测

10. 轮廓检测(Contour Detection)

检测图像中的轮廓

实现方法:

python 复制代码
import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

while True:
    img = sensor.snapshot()
    contours = img.find_contours(threshold=2000)  # 查找轮廓
    for contour in contours:
        img.draw_rectangle(contour.rect(), color=127)  # 绘制轮廓矩形框

11.总结

功能 方法 描述

灰度化 sensor.set_pixformat(sensor.GRAYSCALE) 将图像转换为灰度图

二值化 img.binary([threshold]) 将灰度图转换为黑白图

掩膜 img.mask(mask) 提取图像中的特定区域

腐蚀 img.erode(iterations) 去除噪声,缩小目标区域

膨胀 img.dilate(iterations) 填充空洞,扩大目标区域

缩放 img.scale(x_scale, y_scale) 调整图像大小

旋转 img.rotation_corr(angle) 旋转图像

平移 img.translation(x_offset, y_offset) 平移图像

边缘检测 img.find_edges() 检测图像中的边缘

轮廓检测 img.find_contours() 检测图像中的轮廓

通过以上功能,OpenMV 可以实现丰富的图像处理任务,适用于嵌入式机器视觉应用。


总结

以上就是今天要讲的内容,本文仅仅简单介绍了Openmv中常见的图像处理操作(灰度化、掩膜、二值化、腐蚀、膨胀、缩放、旋转、平移、边缘检测、轮廓检测)

相关推荐
Rorsion1 分钟前
PyTorch实现线性回归
人工智能·pytorch·线性回归
AI资源库2 分钟前
OpenClaw:159K Star的开源AI助手正在重新定义“个人AI“的边界
人工智能·语言模型
Katecat996637 分钟前
YOLO11分割算法实现甲状腺超声病灶自动检测与定位_DWR方法应用
python
凯子坚持 c8 分钟前
StreamingLLM:无需训练即可支持无限上下文的推理技术
人工智能
Tfly__8 分钟前
在PX4 gazebo仿真中加入Mid360(最新)
linux·人工智能·自动驾驶·ros·无人机·px4·mid360
LLWZAI8 分钟前
让朱雀AI检测无法判断的AI公众号文章,当创作者开始与算法「躲猫猫」
大数据·人工智能·深度学习
深圳市九鼎创展科技21 分钟前
瑞芯微 RK3399 开发板 X3399 评测:高性能 ARM 平台的多面手
linux·arm开发·人工智能·单片机·嵌入式硬件·边缘计算
Σίσυφος190022 分钟前
PCL法向量估计 之 RANSAC 平面估计法向量
算法·机器学习·平面
HELLO程序员26 分钟前
Claude Code 2.1 发布:2026 年 AI 智能体开发的范式革命
人工智能
DFCED30 分钟前
OpenClaw部署实战:5分钟搭建你的专属AI数字员工(附避坑指南)
人工智能·大模型·agent·openclaw