欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

opencv+python识别七段数码显示器的数字(数字识别)

程序员文章站 2022-06-17 14:44:21
目录一、什么是七段数码显示器二、创建opencv数字识别器一、什么是七段数码显示器七段lcd数码显示器有很多叫法:段码液晶屏、段式液晶屏、黑白笔段屏、段码lcd液晶屏、段式显示器、tn液晶屏、段码液晶...

一、什么是七段数码显示器

        七段lcd数码显示器有很多叫法:段码液晶屏、段式液晶屏、黑白笔段屏、段码lcd液晶屏、段式显示器、tn液晶屏、段码液晶显示器、段码屏幕、笔段式液晶屏、段码液晶显示屏、段式lcd、笔段式lcd等。

        如下图,每个数字都由一个七段组件组成。

opencv+python识别七段数码显示器的数字(数字识别)

opencv+python识别七段数码显示器的数字(数字识别)

        七段显示器总共可以呈现 128 种可能的状态:

opencv+python识别七段数码显示器的数字(数字识别)

        我们要识别其中的0-9,如果用深度学习的方式有点小题大做,并且如果要进行应用还有很多前序工作需要进行,比如要确认识别什么设备的,怎么找到数字区域并进行分割等等。

opencv+python识别七段数码显示器的数字(数字识别)

二、创建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 thermostat
 
digits_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 contour
	peri = cv2.arclength(c, true)
	approx = cv2.approxpolydp(c, 0.02 * peri, true)
	# if the contour has four vertices, then we have found
	# the thermostat display
	if len(approx) == 4:
		displaycnt = approx
		break
 
# 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 digit
	if 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 segments
	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)
 
	# loop over the segments
	for (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 segment
		segroi = 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 image
	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)
 
# display the digits
print(u"{}{}.{} \u00b0c".format(*digits))
cv2.imshow("input", image)
cv2.imshow("output", output)
cv2.waitkey(0)

opencv+python识别七段数码显示器的数字(数字识别)

原始图片

opencv+python识别七段数码显示器的数字(数字识别)

边缘检测

opencv+python识别七段数码显示器的数字(数字识别)

识别的结果图片

到此这篇关于opencv+python识别七段数码显示器的数字(数字识别)的文章就介绍到这了,更多相关opencv数字识别内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!