OpenMV基础笔记



以下笔记为参加2023工程实践与创新能力大赛时候学习OpenMV的总结。

参考资料

OpenMV 美国官网:https://openmv.io/

OpenMV 星瞳官网:https://openmv.cc/

OpenMV 星瞳中文教程:https://book.openmv.cc/

OpenMV IDE 软件 Github 地址:https://github.com/openmv/openmv-ide/releases/

适用于 OpenMV 的 Micro Python 官方文档:https://docs.openmv.io/

OpenMV sensor 设置教程:https://docs.singtown.com/micropython/zh/latest/openmvcam/library/omv.sensor.html

OpenMV 引脚

pinout-openmv-cam-h7-plus-ov5640

摄像头设置:

摄像头 sensor 参数具体设置如下:

sensor.set_auto_gain(False) # 关闭自动增益
sensor.set_auto_whitebal(False) # 关闭白平衡
sensor.set_windowing((180, 160)) # ROI取中间的640*80区域
sensor.set_pixformat(sensor.GRAYSCALE) # 灰度GRAYSCALE
sensor.lens_corr(strength = 1.5, zoom = 1.0)
sensor.set_hmirror(True) # 水平方向翻转
sensor.set_vflip(True) # 垂直方向翻转

可以尝试打开/关闭自动增益和白平衡,以及改变 (strength = 1.5, zoom = 1.0) 参数来让摄像头识别更准确。

摄像头像素设置:

sensor.set_pixformat() 设置像素模式
GRAYSCALE: 灰度,每个像素8bit RGB565: 彩色,每个像素16bit。
sensor.set_framesize() 设置图像的分辨率
QQQVGA: 80x60 QQVGA: 160x120 QVGA: 320x240 VGA: 640x480

图像分辨率一般设置为 QVGA 效果最佳,在这个分辨率下图像清晰并且处理图像流畅。

串口通信

OpenMV打开/关闭串口:

OpenMV运行时候某些情况下可能需要清空串口接收的缓冲区,避免造成干扰,可以关闭串口再重新打开,或者重新初始化串口。

uart.deinit() # 关闭串口
uart.init(9600,bits=8,parity=None,stop=1,timeout_char=1000) # 重新初始化串口,这会清空缓冲区

OpenMV进行串口通信:

需要导入 import pyb 模块;

初始化串口设置波特率:uart = UART(3, 9600)

随后监听串口并且将读到的信息赋给变量 A,代码如下:

A = 0
if uart.any():
A = uart.read()

如果串口接收的是数字,但是数字经过串口发送变为了字符串,此时可以强行将接收的字符串转化为整型。但是如果接收的字符串并不是数字而是字母或者其他,则强行转换为整型时候会报错。代码如下:

A = 0
if uart.any():
A = int(uart.read())

读取信息:uart.read() 发送信息:uart.write()

颜色阈值

定义一种颜色的阈值:

color_thresholds = (xxx,xxx,xxx,xxx,xxx,xxx)

可以通过以下检测程序自动测出目标物体的 LAB 阈值

import sensor, image, time, pyb
# 初始化相机和设置参数
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度GRAYSCALE
sensor.set_framesize(sensor.QVGA) # 像素为320x240
sensor.skip_frames(n = 10)
sensor.set_auto_whitebal(False) # 关闭自动白平衡以获得准确的颜色检测
clock = time.clock()
# 定义颜色阈值的变量
red_threshold = None
blue_threshold = None
green_threshold = None
# 存储颜色阈值的列表
threshold_history = []
# 初始状态为红色
current_color = "RED"
threshold_count = 0 # 计数器,记录已经收集的阈值数量
while True:
clock.tick()
img = sensor.snapshot()
img.lens_corr(1.8)
img.draw_rectangle((154, 90, 45, 42), color=(255, 255, 255))
statistics_Data = img.get_statistics(roi=(154, 90, 45, 42))
color_L_Mode = statistics_Data.l_mode()
color_A_Mode = statistics_Data.a_mode()
color_B_Mode = statistics_Data.b_mode()
yellow_threshold = (color_L_Mode - 10, color_L_Mode + 10, color_A_Mode - 10, \
color_A_Mode + 10, color_B_Mode - 10, color_B_Mode + 10)
img.binary([yellow_threshold])
# 添加颜色阈值到历史列表
threshold_history.append(yellow_threshold)
threshold_count += 1
# 如果已经收集了20个阈值,输出最新的阈值并切换到下一个颜色
if threshold_count >= 200:
final_threshold = threshold_history[-1]
print("Final Threshold for", current_color, ":", final_threshold)
# 根据当前颜色赋值阈值变量
if current_color == "RED":
red_threshold = final_threshold
current_color = "BLUE"
elif current_color == "BLUE":
blue_threshold = final_threshold
current_color = "GREEN"
elif current_color == "GREEN":
green_threshold = final_threshold
threshold_history = [] # 清空历史列表
threshold_count = 0 # 重置计数器

OpenMV包含多种颜色阈值:

不同的颜色仍然单独定义阈值,但是在找颜色的程序中用 [] 包括所有的目标颜色阈值即可。代码如下:

red_thresholds = (xxx,xxx,xxx,xxx,xxx,xxx)
blue_thresholds = (xxx,xxx,xxx,xxx,xxx,xxx)
yellow_thresholds = (xxx,xxx,xxx,xxx,xxx,xxx)
img=sensor.snapshot()
color_blobs = img.find_blobs([red_thresholds, blue_thresholds, yellow_thresholds])
# 此时即实现了 img.find_blobs 寻找 [red_thresholds, blue_thresholds, yellow_thresholds] 三个颜色的物块。
posted @   IMShuaibei  阅读(137)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示