使用opencv+python识别七段数码显示器的数字识别
生活随笔
收集整理的這篇文章主要介紹了
使用opencv+python识别七段数码显示器的数字识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# 導入必要的包
from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2# 定義Python字典,代表0~9數字的七段數組
DIGITS_LOOKUP = {(1, 1, 1, 0, 1, 1, 1): 0,(0, 0, 1, 0, 0, 1, 0): 1,(1, 0, 1, 1, 1, 0, 1): 2,(1, 0, 1, 1, 0, 1, 1): 3,(0, 1, 1, 1, 0, 1, 0): 4,(1, 1, 0, 1, 0, 1, 1): 5,(1, 1, 0, 1, 1, 1, 1): 6,(1, 0, 1, 0, 0, 1, 0): 7,(1, 1, 1, 1, 1, 1, 1): 8,(1, 1, 1, 1, 0, 1, 1): 9
}# 加載圖像
image = cv2.imread("images\\clock.jpg")# 1. LCD邊緣可見
# 預處理步驟:保持寬高比的縮放,轉換灰度,高斯模糊以減少高頻噪音,Canny邊緣檢測器計算邊緣圖
image = imutils.resize(image, height=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)# 2. 提取LCD本身
# 在邊緣圖中尋找輪廓,并按面積大小倒序排列
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None# 遍歷輪廓
for c in cnts:# 應用輪廓近似peri = cv2.arcLength(c, True)approx = cv2.approxPolyDP(c, 0.02 * peri, True)# 如果邊緣有4個頂點(vertices),則找到了恒溫器并展示if len(approx) == 4:displayCnt = approxbreak# 獲得四個頂點后,可以通過四點透視變換提取LCD
# 提取恒溫器,應用透視變換獲得從上至下鳥瞰LCD圖
warped = four_point_transform(gray, displayCnt.reshape(4, 2))
output = four_point_transform(image, displayCnt.reshape(4, 2))# 3. 從LCD提取數字
# 閾值化透視變換后的圖以在較亮的背景(即LCD顯示屏的背景)上顯示出較暗的區域(即數字);
# 應用一系列形態學運算來清理閾值圖像
thresh = cv2.threshold(warped, 0, 255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)# 應用輪廓過濾,尋找實際的數字
# 在閾值圖像上尋找輪廓,并初始化數字輪廓lists
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
digitCnts = []# 遍歷數字候選區域
for c in cnts:# 計算輪廓的邊界框(x, y, w, h) = cv2.boundingRect(c)# 確定適當的寬度和高度約束需要幾輪反復試驗。建議循環遍歷每個輪廓,分別繪制它們,并檢查其尺寸。執行此過程可確保找到數字輪廓屬性的共同點。# 如果輪廓足夠大,則它是一個數字if w >= 15 and (h >= 30 and h <= 40):digitCnts.append(c)# 4. 實際識別每個數字
# 從左到右排序輪廓,并初始化實際的數字列表
digitCnts = contours.sort_contours(digitCnts,method="left-to-right")[0]
digits = []# 遍歷每一個數字
for c in digitCnts:# 提取數字ROI區域(x, y, w, h) = cv2.boundingRect(c)roi = thresh[y:y + h, x:x + w]# 計算每一個七段部分的寬度、高度(roiH, roiW) = roi.shape(dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))dHC = int(roiH * 0.05)# 定義七段的集合# 根據ROI尺寸計算出每個段的近似寬度和高度。segments = [((0, 0), (w, dH)), # top((0, 0), (dW, h // 2)), # top-left((w - dW, 0), (w, h // 2)), # top-right((0, (h // 2) - dHC), (w, (h // 2) + dHC)), # center((0, h // 2), (dW, h)), # bottom-left((w - dW, h // 2), (w, h)), # bottom-right((0, h - dH), (w, h)) # bottom]on = [0] * len(segments)# 遍歷分段部分for (i, ((xA, yA), (xB, yB))) in enumerate(segments):# 提取分段ROI,計算segment的面積,并計算每個線段的非零像素總值segROI = roi[yA:yB, xA:xB]total = cv2.countNonZero(segROI)area = (xB - xA) * (yB - yA)# 如果非0像素的總數大于面積的50%,則認為分段是打開的if total / float(area) > 0.5:on[i] = 1# 查找digit并顯示在圖像上digit = DIGITS_LOOKUP[tuple(on)]digits.append(digit)cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 1)cv2.putText(output, str(digit), (x - 10, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 255, 0), 2)# 顯示數字
print(u"{}{}.{} \u00b0C".format(*digits))
cv2.imshow("Input", image)
cv2.imshow("Output", output)
cv2.waitKey(0)
本文譯自:Recognizing digits with OpenCV and Python - PyImageSearch
總結
以上是生活随笔為你收集整理的使用opencv+python识别七段数码显示器的数字识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用html表单制作个人简历
- 下一篇: 面试笔记:面经-瓜子