**非常详细的视频和文字教程,讲解常见的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.0-自动计算出阈值+LCD显示
计算阈值的时候也显示画面到LCD
识别过程也显示阈值到LCD
import sensor, lcd
import image
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) # 设置为RGB565颜色格式
sensor.set_framesize(sensor.QQVGA) # 设置图像分辨率
# *************************** 如果不需要镜像就注释掉以下代码 **************************
# 摄像头镜像和翻转设置,根据摄像头的安装方向调整
sensor.set_vflip(True) # 设置垂直翻转,适用于摄像头上下安装的情况
sensor.set_hmirror(True) # 设置水平翻转,适用于摄像头左右安装的情况
# *************************** 如果不需要镜像就注释掉以上代码 **************************
sensor.skip_frames(time=2000) # 跳过帧,确保传感器稳定
# 初始化 LCD 显示
lcd.init()
# 设定阈值范围变量 后面会更新到这里的
threshold = [0, 0, 0, 0, 0, 0] # LAB色彩通道的阈值 [Lmin, Lmax, Amin, Amax, Bmin, Bmax]
#****************[0]-获取指定位置阈值-控制阈值计算只执行一次的标志********************
threshold_calculated = False #控制阈值计算只执行一次的标志
threshold_roi = (80, 60, 30, 30) # 设定ROI,(x, y, w, h)格式# 设定要分析的区域
target_roi = (80, 80, 20, 20) # 设定目标区域,(x, y, w, h)格式,用于后续判断是否满足阈值
#****************[1]-获取指定位置阈值-阈值获取函数 ********************
# 封装为函数:识别指定区域的阈值
def get_threshold(roi):
# 循环多次(默认150次)更新阈值
threshold = [0, 0, 0, 0, 0, 0] # LAB色彩通道的阈值 [Lmin, Lmax, Amin, Amax, Bmin, Bmax]
for _ in range(150):
img = sensor.snapshot()
# 获取指定区域的颜色直方图
hist = img.get_histogram(roi=roi)
img.draw_rectangle(roi, color=(0, 255, 0), thickness=2) # 使用绿色(0, 255, 0),厚度为2# 在图像上绘制绿色矩形框标识采集区域
# 在绿色矩形框上方显示“采集计算阈值中...”并加上省略号
img.draw_string(roi[0], roi[1] - 10, "Collecting Threshold...", color=(0, 255, 0), scale=1)
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像
# 获取L、A、B三个通道的5%和95%分位值
lo = hist.get_percentile(0.05) # 获取5%分位值,表示颜色分布的下边界
hi = hist.get_percentile(0.95) # 获取95%分位值,表示颜色分布的上边界
print("采集计算阈值中...请等待") # 打印检查结果,1表示满足,0表示不满足
# L通道的最小值和最大值平均后作为新的阈值
threshold[0] = (threshold[0] + lo.l_value()) // 2 # L通道的最小值
threshold[1] = (threshold[1] + hi.l_value()) // 2 # L通道的最大值
# A通道的最小值和最大值平均后作为新的阈值
threshold[2] = (threshold[2] + lo.a_value()) // 2 # A通道的最小值
threshold[3] = (threshold[3] + hi.a_value()) // 2 # A通道的最大值
# B通道的最小值和最大值平均后作为新的阈值
threshold[4] = (threshold[4] + lo.b_value()) // 2 # B通道的最小值
threshold[5] = (threshold[5] + hi.b_value()) // 2 # B通道的最大值
print(f"计算阈值的位置区域是 ROI Info: x={roi[0]}, y={roi[1]}, width={roi[2]}, height={roi[3]}") # 输出roi区域的信息
# 打印每个通道的阈值信息
print("计算出的阈值 Threshold: Lmin={0} Lmax={1}, Amin={2} Amax={3}, Bmin={4} Bmax={5}".format(
threshold[0], threshold[1], threshold[2], threshold[3], threshold[4], threshold[5]
))
# 返回计算得到的阈值列表,包含L、A、B三个通道的最小值和最大值
return threshold # 返回最终的阈值数组
while(True):
# 捕获图像
img = sensor.snapshot()
#*****************[2]-获取指定位置阈值-进行阈值计算的内容********************
if not threshold_calculated:# 仅在阈值未计算时进行计算
# 调用函数获取指定区域的阈值
threshold = get_threshold(threshold_roi)
# 设置阈值计算完成的标志
threshold_calculated = True
# 检查目标区域是否满足阈值条件
blobs = img.find_blobs([threshold], roi=target_roi)
# 检查是否找到了 blobs
if blobs:
# 显示数字 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.draw_rectangle(target_roi, color=(255, 0, 0), thickness=2) # 使用红色(255, 0, 0),厚度为2
img_copy = img.copy(0.7, 0.7) # 调整图像显示比例
lcd.display(img_copy)# 在 LCD 上显示图像