**非常詳細(xì)的視頻和文字教程,講解常見的openmv教程包括 巡線、物體識(shí)別、圓環(huán)識(shí)別、閾值自動(dòng)獲取等。非常適合學(xué)習(xí)openmv、K210、K230等項(xiàng)目
視頻合集鏈接在
openmv教程合集 openmv入門到項(xiàng)目開發(fā) openmv和STM32通信 openmv和opencv區(qū)別 openmv巡線 openmv數(shù)字識(shí)別教程LCD
??痮penmv視覺文章鏈接:
https://blog.csdn.net/qq_46187594/category_12900902.html
5.4.0-自動(dòng)計(jì)算出閾值+LCD顯示
計(jì)算閾值的時(shí)候也顯示畫面到LCD
識(shí)別過程也顯示閾值到LCD
import sensor, lcd
import image
import time
#教程作者:好家伙VCC
#歡迎交流群QQ: 771027961 作者郵箱: 1930299709@qq.com
#更多教程B站主頁:[好家伙VCC的個(gè)人空間-好家伙VCC個(gè)人主頁-嗶哩嗶哩視頻](https://space.bilibili.com/434192043)
#淘寶主頁鏈接:[首頁-好家伙VCC-淘寶網(wǎng)](https://shop415231378.taobao.com)
#更多嵌入式手把手教程-盡在好家伙VCC
# 設(shè)置圖像傳感器的配置
sensor.reset() # 初始化傳感器
sensor.set_pixformat(sensor.RGB565) # 設(shè)置為RGB565顏色格式
sensor.set_framesize(sensor.QQVGA) # 設(shè)置圖像分辨率
# *************************** 如果不需要鏡像就注釋掉以下代碼 **************************
# 攝像頭鏡像和翻轉(zhuǎn)設(shè)置,根據(jù)攝像頭的安裝方向調(diào)整
sensor.set_vflip(True) # 設(shè)置垂直翻轉(zhuǎn),適用于攝像頭上下安裝的情況
sensor.set_hmirror(True) # 設(shè)置水平翻轉(zhuǎn),適用于攝像頭左右安裝的情況
# *************************** 如果不需要鏡像就注釋掉以上代碼 **************************
sensor.skip_frames(time=2000) # 跳過幀,確保傳感器穩(wěn)定
# 初始化 LCD 顯示
lcd.init()
# 設(shè)定閾值范圍變量 后面會(huì)更新到這里的
threshold = [0, 0, 0, 0, 0, 0] # LAB色彩通道的閾值 [Lmin, Lmax, Amin, Amax, Bmin, Bmax]
#****************[0]-獲取指定位置閾值-控制閾值計(jì)算只執(zhí)行一次的標(biāo)志********************
threshold_calculated = False #控制閾值計(jì)算只執(zhí)行一次的標(biāo)志
threshold_roi = (80, 60, 30, 30) # 設(shè)定ROI,(x, y, w, h)格式# 設(shè)定要分析的區(qū)域
target_roi = (80, 80, 20, 20) # 設(shè)定目標(biāo)區(qū)域,(x, y, w, h)格式,用于后續(xù)判斷是否滿足閾值
#****************[1]-獲取指定位置閾值-閾值獲取函數(shù) ********************
# 封裝為函數(shù):識(shí)別指定區(qū)域的閾值
def get_threshold(roi):
# 循環(huán)多次(默認(rèn)150次)更新閾值
threshold = [0, 0, 0, 0, 0, 0] # LAB色彩通道的閾值 [Lmin, Lmax, Amin, Amax, Bmin, Bmax]
for _ in range(150):
img = sensor.snapshot()
# 獲取指定區(qū)域的顏色直方圖
hist = img.get_histogram(roi=roi)
img.draw_rectangle(roi, color=(0, 255, 0), thickness=2) # 使用綠色(0, 255, 0),厚度為2# 在圖像上繪制綠色矩形框標(biāo)識(shí)采集區(qū)域
# 在綠色矩形框上方顯示“采集計(jì)算閾值中...”并加上省略號(hào)
img.draw_string(roi[0], roi[1] - 10, "Collecting Threshold...", color=(0, 255, 0), scale=1)
img_copy = img.copy(0.7, 0.7) # 調(diào)整圖像顯示比例
lcd.display(img_copy)# 在 LCD 上顯示圖像
# 獲取L、A、B三個(gè)通道的5%和95%分位值
lo = hist.get_percentile(0.05) # 獲取5%分位值,表示顏色分布的下邊界
hi = hist.get_percentile(0.95) # 獲取95%分位值,表示顏色分布的上邊界
print("采集計(jì)算閾值中...請(qǐng)等待") # 打印檢查結(jié)果,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"計(jì)算閾值的位置區(qū)域是 ROI Info: x={roi[0]}, y={roi[1]}, width={roi[2]}, height={roi[3]}") # 輸出roi區(qū)域的信息
# 打印每個(gè)通道的閾值信息
print("計(jì)算出的閾值 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]
))
# 返回計(jì)算得到的閾值列表,包含L、A、B三個(gè)通道的最小值和最大值
return threshold # 返回最終的閾值數(shù)組
while(True):
# 捕獲圖像
img = sensor.snapshot()
#*****************[2]-獲取指定位置閾值-進(jìn)行閾值計(jì)算的內(nèi)容********************
if not threshold_calculated:# 僅在閾值未計(jì)算時(shí)進(jìn)行計(jì)算
# 調(diào)用函數(shù)獲取指定區(qū)域的閾值
threshold = get_threshold(threshold_roi)
# 設(shè)置閾值計(jì)算完成的標(biāo)志
threshold_calculated = True
# 檢查目標(biāo)區(qū)域是否滿足閾值條件
blobs = img.find_blobs([threshold], roi=target_roi)
# 檢查是否找到了 blobs
if blobs:
# 顯示數(shù)字 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)
# 在目標(biāo)區(qū)域上繪制矩形框
img.draw_rectangle(target_roi, color=(255, 0, 0), thickness=2) # 使用紅色(255, 0, 0),厚度為2
img_copy = img.copy(0.7, 0.7) # 調(diào)整圖像顯示比例
lcd.display(img_copy)# 在 LCD 上顯示圖像
閱讀全文