**非常详细的视频和文字教程,讲解常见的openmv教程包括 巡线、物体识别、圆环识别、阈值自动获取等。非常适合学习openmv、K210、K230等项目
视频合集链接在
openmv教程合集 openmv入门到项目开发 openmv和STM32通信 openmv和opencv区别 openmv巡线 openmv数字识别教程LCD
专刊openmv视觉文章链接:
https://blog.csdn.net/qq_46187594/category_12900902.html
5.4.2-借助LCD屏幕与按键-手动调节阈值(推荐这个方法)
代码的处理流程
把之前自动计算阈值的函数修改一下,
变成通过函数获得某个区域LAB的众数,
然后通过按键设置众数多少范围内设置为阈值,
并且LCD显示根据阈值二值化的图像方便 调试合适的阈值范围。
按下KEY1 进入阈值调节模式,会1.5秒时间读取绿色框的众数,1.5到后会变成根据众数和默认宽度做的阈值然后进行二值化后的图像,然后按下KEY2会增加宽度,KEY3会减少宽度,可以实时通过LCD屏幕观察宽度变化。
按下KEY1 计算阈值,很快计算完就会退出
然后可以按key2 kye3 修改阈值宽度
import sensor, image, lcd
from pyb import Pin
import time
#教程作者:好家伙VCC
#欢迎交流群QQ: 771027961 作者邮箱: 1930299709@qq.com
#更多教程B站主页:[好家伙VCC的个人空间-好家伙VCC个人主页-哔哩哔哩视频](https://space.bilibili.com/434192043)
#淘宝主页链接:[首页-好家伙VCC-淘宝网](https://shop415231378.taobao.com)
#更多嵌入式手把手教程-尽在好家伙VCC
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_vflip(True) # 根据需要设置镜像翻转
sensor.set_hmirror(True) # 根据需要设置镜像翻转
# 初始化 LCD 显示
lcd.init()
# 定义按键引脚
key1 = Pin('P1', Pin.IN, Pin.PULL_UP) # 按键1(P1引脚)
key2 = Pin('P6', Pin.IN, Pin.PULL_UP) # 按键2(P6引脚)
key3 = Pin('P9', Pin.IN, Pin.PULL_UP) # 按键3(P9引脚)
threshold_roi = (80, 60, 30, 30) # 设定ROI,(x, y, w, h)格式# 设定要分析的区域
target_roi = (80, 80, 20, 20) # 设定目标区域,(x, y, w, h)格式,用于后续判断是否满足阈值
#threshold = [27, 47, 46, 66, 33, 53]
threshold_calculated_mode = False #控制获取众数只执行一次的变量
threshold_width = 10 # 可以根据需要调整这个值我们默认的阈值宽度是10
# 设置阈值宽度
# 获取每个颜色通道的众数值
color_l = 0 # L通道众数
color_a = 0 # A通道众数
color_b = 0 # B通道众数
# 阈值初始值
Lmin, Lmax, Amin, Amax, Bmin, Bmax = 0, 100, -128, 128, -128, 128 # 初始阈值 阈值的单个变量格式
threshold = [0, 0, 0, 0, 0, 0] # 阈值的列表格式
# 处理按键事件
def handle_keys():
global Lmin, Lmax, Amin, Amax, Bmin, Bmax # 声明全局变量,意味着这些变量会在函数外部被修改
global color_l, color_a, color_b
global threshold_width
global threshold
adjust_mode = False # 默认不进入调节模式
if not key1.value(): # KEY1 按下,进入阈值调节模式
time.sleep(0.8) # 按键1按下后的延时放置误触发
adjust_mode = True #赋值adjust_mode 表示要进入调试模式
print("KEY1 按下,进入阈值调节模式 Entering Threshold Adjustment Mode")
img = sensor.snapshot()#获取摄像头图像
img.draw_string(10, 10, "KEY1_waiting", color=(0, 255, 0), scale=2) # 绿色显示
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像
if not adjust_mode: # 如果没有按下 KEY1 进入调节模式,则执行阈值计算
print("并没有按下KEY1")
else:
# 如果进入调节模式,等待按键KEY2 KEY3 调整阈值
print("如果进入调节模式,等待按键调整阈值")
start_time = time.ticks_ms() # 获取当前时间
while adjust_mode:
# 使用500ms获得指定区域的众数
while time.ticks_ms() - start_time < 1500: #前1.5秒会进行获取区域LAB众数的操作
print("已经进入调试模式,获得指定区域众数中...")
#显示变化后的阈值,然后根据此阈值二值化后的页面
img = sensor.snapshot() #获取摄像头画面
statistics=img.get_statistics(roi=threshold_roi)# 获取指定区域 的图像统计数据
img.draw_rectangle(threshold_roi, color=(0, 255, 0), thickness=2) # 使用绿色(0, 255, 0),厚度为2# 在图像上绘制绿色矩形框标识采集区域
color_l=statistics.l_mode()# 获取该区域 L(亮度)通道的众数
color_a=statistics.a_mode()# 获取该区域 A(红绿)通道的众数
color_b=statistics.b_mode()# 获取该区域 B(黄蓝)通道的众数
print(color_l,color_a,color_b) #输出一下
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像
# 根据众数和阈值宽度来更新阈值
Lmin = color_l - threshold_width
Lmax = color_l + threshold_width
Amin = color_a - threshold_width
Amax = color_a + threshold_width
Bmin = color_b - threshold_width
Bmax = color_b + threshold_width
threshold = [Lmin, Lmax, Amin, Amax, Bmin, Bmax]# 将阈值合并为一个列表
#显示变化后的阈值,然后根据此阈值二值化后的页面
img = sensor.snapshot()
# 应用阈值进行二值化
img.binary([threshold])#根据阈值使用binary将图像二值化
print("最后阈值为:threshold:", threshold)
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像
# 处理 KEY2 按键,调整 A 值(增加)
if not key2.value(): # KEY2按下
threshold_width += 2
print(f"KEY2 按下,增加阈值宽度: {threshold_width}")
time.sleep(0.8) # 按键2按下后的延时
# 处理 KEY3 按键,调整 A 值(减少)
if not key3.value(): # KEY3按下
threshold_width -= 2
print(f"KEY3 按下,增加阈值宽度: {threshold_width}")
time.sleep(0.8) # 按键3按下后的延时
# 如果按下 KEY1,退出阈值调整模式
if not key1.value(): # KEY1 按下
adjust_mode = False
print("如果按下 KEY1,退出阈值调整模式 Exiting Threshold Adjustment Mode")
time.sleep(0.8) # 按键1按下后的延时
# 主循环
while True:
img = sensor.snapshot() # 获取图像
handle_keys()# 处理按键事件 里面会计算适合的阈值 这个要经常能供得到调度,才能检测到那个按键被按下
#查找某一个区域是否满足阈值
blob1 = img.find_blobs([threshold], roi=target_roi)
# 绘制红色矩形框,标出 target_roi 区域
img.draw_rectangle((target_roi[0], target_roi[1], target_roi[2], target_roi[3]), color=(255, 0, 0), thickness=2)
# 检查是否找到了 blobs
if blob1:
# 显示数字 1
img.draw_string(100, 60, "1", color=(255, 0, 0), scale=2)
else:
# 如果没有找到 blobs,显示 0
img.draw_string(100, 60, "0", color=(255, 0, 0), scale=2)
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像