OpenCV-Python 识别万用表七段数码管电流值

项目简介

利用 OpenCV 和笔记本 PC 的摄像头(或者外接摄像头)识别万用表电流值。

步骤:

1、定位屏幕

照片要光线均匀,无大面积阴影。
另外,这款万用表轮廓不明显,故贴纸使轮廓更分明。
在这里插入图片描述
首先对图像做:灰度化、高斯滤波、边缘识别。
在处理后的图片上识别轮廓,并提取最大轮廓,即为屏幕区域。
得到:
在这里插入图片描述

2、提取屏幕,并做一系列图像处理,得到黑白屏幕

透视变换,提取屏幕区域:
在这里插入图片描述
图片二值化、形态学变换(腐蚀、膨胀):
在这里插入图片描述

3、提取数字,并分割

提取图片数字区域,消除边缘阴影,然后使用垂直投影法分割数字。
在这里插入图片描述
在这里插入图片描述

4、识别数字

进一步提取单个数字中每一根数码管的大致区域,通过统计该区域白色像素占比,若占比超过40%(有误差),则认为该管为亮。然后查表,判断数字。
在这里插入图片描述
在这里插入图片描述
结果:
在这里插入图片描述
代码附上:

# import the necessary packages
from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2
import numpy as np
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter

# 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, 0, 1): 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, 1, 1, 0, 0, 1, 0): 7,
	(1, 1, 1, 1, 1, 1, 1): 8,
	(1, 1, 1, 1, 0, 1, 1): 9
}


def get_vvList(list_data):
    #取出list中像素存在的区间
    vv_list=list()
    v_list=list()
    for index,i in enumerate(list_data):
        if i>0:
            v_list.append(index)
        else:
            if v_list:
                vv_list.append(v_list)
                #list的clear与[]有区别
                v_list=[]
    return vv_list

# load the example image
image = cv2.imread("15.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, 128, 200)

# find contours in the edge map, then sort them by their
# size in descending order
cnts,hi = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# the largest contour(default: target) is in the first place after sorting
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
cv2.drawContours(image,cnts,0,(0,0,255),3)
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))

#
thresh = cv2.threshold(warped, 0, 255,
	cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

# kernel = np.ones((5,5),np.uint8)
# erosion = cv2.erode(thresh,kernel)
# dst = cv2.dilate(erosion,kernel)
# dst1 = cv2.dilate(dst,kernel)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel,2)


ROI = thresh.copy()[20:103, 20:168]
# 黑白反转
where_0 = np.where(ROI == 0)
where_255 = np.where(ROI == 255)
ROI[where_0] = 255
ROI[where_255] = 0
print(thresh.shape)


rows, cols = ROI.shape
ver_list = [0]*cols
for j in range(cols):
	for i in range(rows):
		if ROI.item(i,j)==0:
			ver_list[j]=ver_list[j]+1
'''
对ver_list中的元素进行筛选,可以去除一些噪点qq
'''
ver_arr=np.array(ver_list)
ver_arr[np.where(ver_arr<1)]=0
ver_list=ver_arr.tolist()

#绘制垂直投影
img_white=np.ones(shape=(rows,cols),dtype=np.uint8)*255
for j in range(cols):
	pt1=(j,rows-1)
	pt2=(j,rows-1-ver_list[j])
	cv2.line(img_white,pt1,pt2,(0,),1)
cv2.imshow('垂直投影',img_white)

digits = []

#切割单一字符
vv_list=get_vvList(ver_list)
for i in vv_list:
	img_ver=ROI.copy()[:,i[0]:i[-1]]
	where_0 = np.where(img_ver == 0)
	where_255 = np.where(img_ver == 255)
	img_ver[where_0] = 255
	img_ver[where_255] = 0
	(roiH, roiW) = img_ver.shape
	(dW, dH) = (int(roiW * 0.3), int(roiH * 0.1))
	dHC = int(roiH * 0.05)
	# define the set of 7 segments
	segments = [
		((0, 0), (roiW, dH)),  # top
		((0, 0), (dW, roiH // 2)),  # top-left
		((roiW - dW, 0), (roiW, roiH // 2)),  # top-right
		((0, (roiH // 2) - dHC), (roiW, (roiH // 2) + dHC)),  # center
		((0, roiH // 2), (dW, roiH)),  # bottom-left
		((roiW - dW, roiH // 2), (roiW, roiH)),  # bottom-right
		((0, roiH - dH), (roiW, roiH))  # 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 = img_ver[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"
		proportion = total / float(area)
		print(proportion)
		if proportion > 0.35:
			on[i] = 1
	# lookup the digit and draw it on the image
	# special: digit = 1
	if roiH > 5 * roiW:
		digit = 1
	else:
		digit = DIGITS_LOOKUP[tuple(on)]
	print(digit)
	digits.append(digit)
	cv2.imshow('单一字符', img_ver)
	cv2.waitKey(0)

cv2.putText(warped, str(digits), (15, 15),
			cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 0), 2)
print(digits)

cv2.imshow("Output1", image)
cv2.imshow("Output2", warped)
cv2.imshow("Output3", thresh)

cv2.waitKey(0)

还有一份带简单UI界面,摄像头实时识别版,且有绘图功能,如需要联系我:

在这里插入图片描述

参考:
使用 OpenCV 和 Python 识别数字
文本分割之垂直投影法基于OpenCV(python)的实现

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值