当前位置:   article > 正文

opencv实现数码管数字的识别小白篇(python)_python识别数码管字体

python识别数码管字体

1.测试图片

在这里插入图片描述

2.基本思路

原图 -> 灰度处理 -> 二值化处理(数字部分为白色)-> 膨胀处理 -> 寻找轮廓
找出所有数字的轮廓,分别传入识别函数
(穿针法识别:tubo_roi为各段的区域,通过判断abcdefg有哪几个是白色,来组合成不同的数字)
特殊数字:1 ,通过长宽比大于2来判断为数字1
在这里插入图片描述

3.代码运行效果

经过多重处理,成功识别出1,2,3,4,5,6,7,8,9
仅本测试图片可用,其他测试场景可能需要优化图片预处理逻辑
在这里插入图片描述

4.代码

使用测试图片可直接运行,注意图片名称以及路径

import cv2
import numpy as np

#原图
image = cv2.imread('photo.jpg',cv2.IMREAD_COLOR)
cv2.imshow("image",image)

#灰度图处理
image_gry = cv2.imread('photo.jpg',cv2.IMREAD_GRAYSCALE)
if image_gry is None:
    print("not read")
    exit()
cv2.imshow("image_gry", image_gry)

#二值化
_, image_bin = cv2.threshold(image_gry,80,255,cv2.THRESH_BINARY)
cv2.imshow("image_bin",image_bin)

#进行膨胀处理
element = cv2.getStructuringElement(cv2.MORPH_RECT,(20,20))
image_dil = cv2.dilate(image_bin, element)
cv2.imshow("image_dil",image_dil)

#轮廓寻找
contours_out,hierarchy=cv2.findContours(image_dil, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#cv2.drawContours(image_bin,contours,-1,(0,255,0),2)
num_location = [cv2.boundingRect(contour) for contour in contours_out]
num_location.sort(key=lambda x: x[0])
inmage_with_contours = cv2.cvtColor(image_bin,cv2.COLOR_GRAY2BGR)
cv2.drawContours(inmage_with_contours,contours_out,-1,(0,225,0),2)

cv2.imshow("Contours",inmage_with_contours)

# 定义判断区域是否全为白色的函数
def is_all_white(image, row_start, row_end, col_start, col_end):
    white_num = 0
    j=row_start
    i=col_start
 
    while(j <= row_end):
        while(i <= col_end):
            if(image[j][i] == 255):                
                white_num+=1
            i+=1
        j+=1
        i=col_start
    #print('white num is',white_num)
    if(white_num >= 5):
        return True
    else:
        return False
    
# 定义穿线法识别数字的函数
def tube_identification(inputmat):
    tube = 0
    tubo_roi = [
         [inputmat.shape[0] * 0/3, inputmat.shape[0] * 1/3, inputmat.shape[1] * 1/2,inputmat.shape[1] * 1/2], #a
         [inputmat.shape[0] * 1/3, inputmat.shape[0] * 1/3, inputmat.shape[1] * 2/3,inputmat.shape[1] - 1  ], #b
         [inputmat.shape[0] * 2/3, inputmat.shape[0] * 2/3, inputmat.shape[1] * 2/3,inputmat.shape[1] - 1  ], #c
         [inputmat.shape[0] * 2/3, inputmat.shape[0] -1   , inputmat.shape[1] * 1/2,inputmat.shape[1] * 1/2], #d
         [inputmat.shape[0] * 2/3, inputmat.shape[0] * 2/3, inputmat.shape[1] * 0/3,inputmat.shape[1] * 1/3], #e
         [inputmat.shape[0] * 1/3, inputmat.shape[0] * 1/3, inputmat.shape[1] * 0/3,inputmat.shape[1] * 1/3], #f
         [inputmat.shape[0] * 1/3, inputmat.shape[0] * 2/3, inputmat.shape[1] * 1/2,inputmat.shape[1] * 1/2]  #g
         ] 
    i = 0
    while(i < 7):
        if(is_all_white(inputmat,int(tubo_roi[i][0]),int(tubo_roi[i][1]), int(tubo_roi[i][2]),int(tubo_roi[i][3]))):
            tube = tube + pow(2,i)
            
        cv2.line(inputmat, ( int(tubo_roi[i][3]),int(tubo_roi[i][1])), 
                (int(tubo_roi[i][2]), int(tubo_roi[i][0])),                
                (255,0,0), 1)                       
        i += 1
    if (inputmat.shape[0] / inputmat.shape[1] > 2):   # 1 is special, which is much narrower than others
        tube = 6
    if(tube==63):
        onenumber = 0
    elif(tube==6):
        onenumber = 1
    elif(tube==91):
        onenumber = 2
    elif(tube==79):
        onenumber = 3
    elif(tube==102 or tube==110):
    #110是因为有干扰情况
        onenumber = 4
    elif(tube==109):
        onenumber = 5
    elif(tube==125):
        onenumber = 6
    elif(tube==7):
        onenumber = 7
    elif(tube==127):
        onenumber = 8
    elif(tube==111):
        onenumber = 9
    else:
        print("error tube = ",tube)
        onenumber = -1 

    return onenumber      

# 存储通过穿线法识别的数字
detected_numbers = []

# 遍历每个数字区域
for i in range(len(num_location)):
    x, y, w, h = num_location[i]
    num_region = image_dil[y:y+h, x:x+w]  # 提取数字区域

    # 调用穿线法识别数字
    detected_number = tube_identification(num_region)
    detected_numbers.append(detected_number)

    # 显示数字区域
    #cv2.imshow(str(i), num_region)

# 输出识别到的数字
print("Detected Numbers:", detected_numbers)

cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122

5.参考

OpenCV之七段数码管识别(含代码)

opencv识别数码管数字

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/686029
推荐阅读
相关标签
  

闽ICP备14008679号