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

作者:bashendixie5 时间:2022-03-03 00:01:51 

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

        七段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识别七段数码显示器的数字(数字识别)

识别的结果图片

来源:https://blog.csdn.net/bashendixie5/article/details/122361536

标签:opencv,数字,识别
0
投稿

猜你喜欢

  • 汇总数据库备份 还原 压缩与数据库转移的方法

    2009-01-19 14:07:00
  • Ajax:拥抱JSON,让XML走开

    2007-10-12 20:01:00
  • python对配置文件.ini进行增删改查操作的方法示例

    2023-10-25 05:56:13
  • Python 打印不带括号的元组的实现

    2022-08-12 15:47:45
  • SQLServer 2008助你轻松编写T-SQL存储过程

    2010-12-06 13:38:00
  • Python项目实战之使用Django框架实现支付宝付款功能

    2021-12-11 23:28:11
  • Python接口自动化之浅析requests模块post请求

    2022-12-10 21:10:05
  • Golang Gin局部和全局中间件使用详解

    2023-07-10 03:03:00
  • Python 虚拟环境工作原理解析

    2023-02-21 02:18:50
  • HTTP Headers 傻瓜教程[译]

    2010-01-25 12:48:00
  • python绘制多个子图的实例

    2023-01-31 02:24:24
  • python中的class_static的@classmethod的巧妙用法

    2022-07-18 15:54:43
  • Python基于xlrd模块操作Excel的方法示例

    2021-07-18 16:04:06
  • 下载糗事百科的内容_python版

    2022-02-08 12:39:29
  • Python中的sort()方法使用基础教程

    2022-03-07 21:44:09
  • opencv用VS2013调试时用Image Watch插件查看图片

    2022-04-20 14:49:12
  • 用好Frontpage中的各种回车

    2008-02-21 14:33:00
  • laravel修改用户模块的密码验证实现

    2023-06-14 12:37:18
  • 通过SQL Server的位运算功能巧妙解决多选查询方法

    2012-01-29 17:54:35
  • python中实现字符串翻转的方法

    2021-06-08 04:27:59
  • asp之家 网络编程 m.aspxhome.com