日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

数字图像处理 使用opencv+python识别七段数码显示器的数字

發布時間:2024/3/12 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数字图像处理 使用opencv+python识别七段数码显示器的数字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????一、什么是七段數碼顯示器

????????七段LCD數碼顯示器有很多叫法:段碼液晶屏、段式液晶屏、黑白筆段屏、段碼LCD液晶屏、段式顯示器、TN液晶屏、段碼液晶顯示器、段碼屏幕、筆段式液晶屏、段碼液晶顯示屏、段式LCD、筆段式LCD等。

? ? ? ? 如下圖,每個數字都由一個七段組件組成。

???????????????????????????????????????

????????七段顯示器總共可以呈現 128 種可能的狀態:

????????我們要識別其中的0-9,如果用深度學習的方式有點小題大做,并且如果要進行應用還有很多前序工作需要進行,比如要確認識別什么設備的,怎么找到數字區域并進行分割等等。

????????二、創建opencv數字識別器

? ? ? ? ?我們這里進行使用空調恒溫器進行識別,首先整理下流程。

? ? ? ? 1、定位恒溫器上的 LCD屏幕。

? ? ? ? 2、提取 LCD的圖像。

? ? ? ? 3、提取數字區域

? ? ? ? 4、識別數字。

? ? ? ? 我們創建名稱為recognize_digits.py的文件,代碼如下。僅思路供參考(因為代碼中的一些參數只適合測試圖片)

# import the necessary packages from imutils.perspective import four_point_transform from imutils import contours import imutils import cv2 # define the dictionary of digit segments so we can identify # each digit on the thermostatDIGITS_LOOKUP = {(1, 1, 1, 0, 1, 1, 1): 0,(0, 0, 1, 0, 0, 1, 0): 1,(1, 0, 1, 1, 1, 1, 0): 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 }# load the example image image = cv2.imread("example.jpg")# # pre-process the image by resizing it, converting it to # graycale, blurring it, and computing an edge map 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)# find contours in the edge map, then sort them by their # size in descending order 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 # loop over the contours for c in cnts:# approximate the contourperi = cv2.arcLength(c, True)approx = cv2.approxPolyDP(c, 0.02 * peri, True)# if the contour has four vertices, then we have found# the thermostat displayif len(approx) == 4:displayCnt = approxbreak# extract the thermostat display, apply a perspective transform # to it warped = four_point_transform(gray, displayCnt.reshape(4, 2)) output = four_point_transform(image, displayCnt.reshape(4, 2))# threshold the warped image, then apply a series of morphological # operations to cleanup the thresholded image 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)# find contours in the thresholded image, then initialize the # digit contours lists cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) digitCnts = [] # loop over the digit area candidates for c in cnts:# compute the bounding box of the contour(x, y, w, h) = cv2.boundingRect(c)# if the contour is sufficiently large, it must be a digitif w >= 15 and (h >= 30 and h <= 40):digitCnts.append(c)# sort the contours from left-to-right, then initialize the # actual digits themselves digitCnts = contours.sort_contours(digitCnts, method="left-to-right")[0] digits = []# loop over each of the digits for c in digitCnts:# extract the digit ROI(x, y, w, h) = cv2.boundingRect(c)roi = thresh[y:y + h, x:x + w]# compute the width and height of each of the 7 segments# we are going to examine(roiH, roiW) = roi.shape(dW, dH) = (int(roiW * 0.25), int(roiH * 0.15))dHC = int(roiH * 0.05)# define the set of 7 segmentssegments = [((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)# loop over the segmentsfor (i, ((xA, yA), (xB, yB))) in enumerate(segments):# extract the segment ROI, count the total number of# thresholded pixels in the segment, and then compute# the area of the segmentsegROI = roi[yA:yB, xA:xB]total = cv2.countNonZero(segROI)area = (xB - xA) * (yB - yA)# if the total number of non-zero pixels is greater than# 50% of the area, mark the segment as "on"if total / float(area) > 0.5:on[i]= 1# lookup the digit and draw it on the imagedigit = 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)# display the digits print(u"{}{}.{} \u00b0C".format(*digits)) cv2.imshow("Input", image) cv2.imshow("Output", output) cv2.waitKey(0) 原始圖片

?

邊緣檢測

?

識別的結果圖片

?

總結

以上是生活随笔為你收集整理的数字图像处理 使用opencv+python识别七段数码显示器的数字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。