数字图像处理 使用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识别七段数码显示器的数字相关推荐
- 使用opencv+python识别七段数码显示器的数字识别
# 导入必要的包 from imutils.perspective import four_point_transform from imutils import contours import im ...
- python:七段数码管绘制数字详解
python:七段数码管绘制数字详解 七段数码管由七段数码管拼接而成,每段有亮或不亮两种情况.这里,先给出程序的全部代码,如下所示. 定义了drawDigit()函数,该函数根据输入的数字绘制七段数码 ...
- 数字图像处理二维码识别 python+opencv实现二维码实时识别
数字图像处理二维码识别 python+opencv实现二维码实时识别 特点: (1)可以实现普通二维码,条形码: (2)解决了opencv输出中文乱码的问题 (3)增加网页自动跳转功能 (4)实现二维 ...
- 如何利用cnocr 识别七段数码?
在博文两款开源的中文OCR工具 介绍了两款OCR工具.对于其中的**CNOCR ** 进行了测试.可以作为今后研究的工具. 01安装cnocr 可以使用pip进行安装 pip install cnoc ...
- python七段数码管设计图案-Python绘制七段数码管实例代码
七段数码管(seven-segmentindicator)由7段数码管拼接而成,每段有亮或不亮两种情况,改进型的七段数码管还包括一个小数点位置 绘制模式: input:输入当前日期的数字形式 proc ...
- 数字图像处理——实验一 Python中数字图像处理的基本操作
数字图像处理--实验一 Python中数字图像处理的基本操作 一.实验目的 二.实验主要仪器设备 三.实验原理 3.1 数字图像的表示和类别 3.2 opencv-python图像文件格式 四.实验内 ...
- python七段数码管设计图案-python实现七段数码管和倒计时效果
8是典型的七段数码管的例子,因为刚好七段都有经过,这里我写的代码是从1开始右转. 这是看Mooc视频写的一个关于用七段数码管显示当前时间 # -*-coding:utf-8 -*- import tu ...
- 仿真软件proteus构建七段数码管显示数字0-9实验
七段数码管显示原理研究在前一篇博客中已经阐述过,就是利用7个数码管构建一个"8"字形,然后利用数字的形状特点,依次点亮某一些段,就可以显示数字了. 七段数码管显示数字还是二极管发光 ...
- 七段数码显示的数字时钟
七段数码显示的数字时钟 作者: mt.hu 下载源代码 摘要 绝大多数的电子产品都使用了七段数码显示,如果软件也能模拟出这种效果该有多好?在本文之前,VC知识库在线杂志曾有两篇文章介绍过如何实现这种效 ...
最新文章
- HarmonyOS 怎样打印log/日志的打印
- 埃森哲为施耐德电气打造数字工厂,加速产业物联网开发
- 结构型模式之Flyweight模式
- 【Android 插件化】VirtualAppEx 编译运行 ( VirtualAppEx 简介 | 配置 VirtualAppEx 编译环境 | 编译运行 VirtualAppEx 代码 )
- 地形纹理Splatting技术(翻译)
- oracle导入java包时出错,Oracle导入导出的常见错误
- 请求转发和请求重定向的区别?
- jmap+MAT实战内存溢出
- Iptables详解+实例
- GitHub Desktop离线安装包
- How to install JDK on Linux
- java 内存管理_高性能Java代码之内存管理
- windows_正确修改windows用户名(win10/win11)实践/副作用说明
- simulink模型动静态测试
- CTF_Web:长安杯-2021 Old But A Little New asuka题解
- 安卓语音识别文字软件
- 自动驾驶 Automotive SPICE(ISO/IEC 15504) 和CMMI有什么不同?
- 让机器耳濡目染:MIT提出跨模态机器学习模型
- Google 内购 - Android
- wordpress友联_WordPress制作独立的友情链接(Links)页面