python中的opencv读取数字_用python和OpenCV从图像中提取数字
我正在寻找一些编码解决方案/帮助,从液晶显示器提取数字,然后从图像输出值。在
下面是代码和示例图像,以及到目前为止我是如何得到的,但是我需要一些进一步的帮助来从图像中提取“数字”并输出值。在
我从前面的例子中做了个数字查找表
# 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("Multimeter_1.jpg")
# pre-process the image by converting it to
# graycale, blurring it, and computing an edge map
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,87,9)
# find contours in the edge map,
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
rect = None
# loop over the contours
for c in cnts:
# approximate the contour
x,y,w,h = cv2.boundingRect(c)
#cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 1)
cv2.drawContours(image, [c], -1, (36,255,12),3)
rect = c
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
rect = approx
break
# extract the display, apply a perspective transform
warped = four_point_transform(thresh, rect.reshape(4, 2))
output = four_point_transform(image, rect.reshape(4, 2))
# Warp the image and perform morphology to clean it
thresh = cv2.threshold(warped, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# pre-process the image by converting it to
# graycale, blurring it, and computing an edge map
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,87,9)
# find contours in the edge map,
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
rect = None
# loop over the contours
for c in cnts:
# approximate the contour
x,y,w,h = cv2.boundingRect(c)
#cv2.rectangle(image, (x, y), (x+w, y+h), (36, 255, 12), 1)
cv2.drawContours(image, [c], -1, (36,255,12),3)
rect = c
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.05 * peri, True)
if len(approx) == 4:
rect = approx
break
# extract the display, apply a perspective transform
warped = four_point_transform(thresh, rect.reshape(4, 2))
output = four_point_transform(image, rect.reshape(4, 2))
# Warp the image and perform morphology to clean it
thresh = cv2.threshold(warped, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
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:
(x,y,w,h) = cv2.boundingRect(c)
# if the contour is sufficiently large, it must be a digit
if w >= 25 and (h >= 50 and h <= 60):
digitCnts.append(c)
cv2.imshow("Multimeter", image)
cv2.imshow("Threshed", thresh)
cv2.imwrite("Threshed.jpg",thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
有人能在python,特别是在Open CV中非常有经验的人帮我解决这个问题吗。这是一个关于图像处理部分的学校作业,我决定使用Python和OpenCV,所以这不是一个编程作业,代码将被用来做和解释实际的图像处理。我只需要帮助从树上的图像中提取数字。我还提供了从液晶显示器的原始图像,以防有更好的方法来实现我所寻找的。在
python中的opencv读取数字_用python和OpenCV从图像中提取数字相关推荐
- Python+opencv 机器视觉 - 基于霍夫圈变换算法检测图像中的圆形实例演示
Python+opencv 机器视觉 - 基于霍夫圈变换算法检测图像中的圆形实例演示 第一章:霍夫变换检测圆 ① 实例演示1 ② 实例演示2 ③ 霍夫变换函数解析 第二章:Python + openc ...
- Python使用matplotlib可视化多个时间序列数据、在同一个可视化图像中可视化多个时间序列数据(Multiple Time Series)
Python使用matplotlib可视化多个时间序列数据.在同一个可视化图像中可视化多个时间序列数据(Multiple Time Series) 目录
- python一帧一帧读取视频_用Python从视频中提取每一帧的图片
大家应该都有这样的情况:在看到某些视频的画面时感觉美如画,想截取下来却又烦于截图的繁琐,现在我就教大家使用Python提取视频中每一帧的画面,让大家不错过每一个精彩的瞬间! •语言:Python •所 ...
- 关于python浮点数类型错误的是_关于Python的数字类型,以下选项中描述错误的是...
[单选题]以下程序的输出结果是: lcat =["狮子","猎豹","虎猫","花豹","孟加拉虎" ...
- python中的mat什么意思_用python中的openCV创建Mat
对于OpenCV 1.x: 您可以使用CreateMat来执行此操作:Creates a matrix header and allocates the matrix data.Python: cv. ...
- 数组中查找並返回数组_用Python查找数组中出现奇数次的那个数字
有一个数组,其中的数都是以偶数次的形式出现,只有一个数出现的次数为奇数次,要求找出这个出现次数为奇数次的数. 集合+统计 解题思路 最简单能想到的,效率不高.利用集合的特性,通过 Python 的 s ...
- python中读取文本文件_利用Python读取文本文件?
文件的遍历 因为文件保存了很多字符和行,因此也是循环常见的典型使用案例,最原始的方法可以调用文件对象的read方法,把文件内容一次性加载至字符串对象 file = open('myfile.txt', ...
- python从文件中读取数据_【Python】从文件中读取数据
从文件中读取数据 1.1 读取整个文件 要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下) PI_DESC.txt 3.1415926535 ...
- 关于python浮点数类型错误的是_关于 Python 的数字类型,以下选项中描述错误的是( )...
关于 Python 的数字类型,以下选项中描述错误的是( ) 答:浮点数也有十进制.二进制.八进制和十六进制等表示方式 学业成就女性不亚于男性,而在事业成就女子表现如何? 答:明显落后于男子 对于间壁 ...
- python中的字体英文名_对python opencv 添加文字 cv2.putText 的各参数介绍
如下所示: cv2.putText(img, str(i), (123,456)), font, 2, (0,255,0), 3) 各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字 ...
最新文章
- LVM逻辑卷创建管理
- ii第六单元 文本处理工具
- 零基础学python爬虫-零基础如何学爬虫技术?一篇带你入门!(理论+实操+荐书)...
- web前端入门学习 html5(2)
- how to rank conferences or journals?
- do{...}while(0) 的意义和用法
- 80--查询分组关联属性
- vue自定义下拉菜单,点击下拉其它空白区域,下拉消失
- 新闻发布系统,我学会了什么?
- 宝塔界面,添加数据库,数据库管理密码错误!
- 美发店员工合同WORD版(可直接下载打印)【卓美业网拓软件整理】
- jszip压缩服务器文件,JSZip压缩与解压的基本使用
- ROS基础篇(四)-- 调试时,常用的命令有哪些
- HDU 2079 选课时间(题目已修改,注意读题)(母函数)
- matplotlib-04 xlabel设置x轴的标签
- Axure9的使用经验分享:如何高效的实现弹窗交互模型
- Java WORD转换PDF 并添加水印 (附赠jar提取链接)
- 基于thinkphp的开源App商城
- 图神经网络 —— 排列不变函数
- Php 360度跟随图,一种360度全景图像无缝拼接的方法与流程