数字识别,使用开放式

2024-09-27 09:31:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有4张小图片。在

enter image description here

enter image description here

enter image description here

enter image description here

有数字6、16、9和9。我用我的模板比较图片和数字,只有30个变体[0-30]。图片-截图是。数字出现在正方形的不同位置(例如,9在左角,9在右角)。在

我使用两种方法:计算白色像素的数量。在

original = cv2.imread('im/16.png')
sought = [254,254,254]
result = np.count_nonzero(np.all(original==sought,axis=2)) 

这种方法总是有效的,除了6和9。在这种情况下,白色像素的数量是相等的。在

第二种方法:获取图像上所有像素的位置,并将数据与其他图像进行比较:

^{pr2}$

这个方法帮我区分6和9,但是!在两张不同角有9的图像中,它也能识别出不同之处。在

我希望我的代码能识别每个数字,而不是在图像的左右两侧看到一个数字之间的差异。在


Tags: 方法图像模板数量np图片数字像素
2条回答

opencv中,您可以在69上训练一个分类器,基于Haar特征的级联分类器,用于对象检测(https://docs.opencv.org/3.4/d5/d54/group__objdetect.htmlhttps://docs.opencv.org/3.4/dc/d88/tutorial_traincascade.html

示例代码在i.e.https://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html

我不知道这是否是一个任务,如果你被固定到opencv,如果不是你可以使用神经网络,cf章节隧道视觉https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721https://towardsdatascience.com/convnets-series-spatial-transformer-networks-cff47565ae81,但是对于这个问题,空间变换器网络有点过于复杂

您可以找到许多关于OCR的论文和软件,因为它在许多应用程序中得到了广泛的应用。我想用numpy和opencv为您的问题提供一个非常简单的解决方案,这样就可以解决问题了。在

我们要做的是:

  1. 导入numpy和opencv
  2. 加载您提供的图像
  3. 把他们拖走
  4. Make函数,它将返回给定图像中的数字数组
  5. 比较图像1和图像2中的数字
  6. 做我们的“数字银行”,这样我们就知道数字9是什么样子了
  7. 将我们在图3中找到的数字与我们的“数字库”进行比较

代码:

import cv2
import numpy as np

treshold = 70

#Treshold every image, so "0" in image means no digit and "1" is digit 
image1 = (cv2.imread("number_1.png",0) > treshold).astype(np.uint8)
image2 = (cv2.imread("number_2.png",0) > treshold).astype(np.uint8)
image3 = (cv2.imread("number_3.png",0) > treshold).astype(np.uint8)
image4 = (cv2.imread("number_4.png",0) > treshold).astype(np.uint8)

函数,它将返回给定图像中的数字数组:

^{pr2}$

获取数字

d_1 = get_images_of_digits(image1)[0] #Digit "9" from first image
d_2 = get_images_of_digits(image2)[0] #Digit "9" from second image
d_3 = get_images_of_digits(image4)[0] #Digit "6" from last image

print(cv2.filter2D(d_1,-1,d_2).max()) #Digit "9" on image 1 and 2 match perfectly (result of convolution is 1).
#Filter2D does convolution (correlation to be precise, but they are the same for our purpose)

将第一张图片中的数字“9”和最后一张图片中的数字“6”放入数字银行。然后浏览我们在图3中找到的每个数字,并将其与我们的数字银行进行比较。如果分数低于0.9,则不匹配。在

bank_of_digits = {"9":d_1, "6":d_3} 
for digit in get_images_of_digits(image3):
    #print(digit)
    best_restult = 0.9 #If score is above 0.9, we say it is match
    #Maybe tweak this higher for separating chars "8" and "9" and "0"
    matching_digit = "?" #Default char, when there is no match
    for number in bank_of_digits:
        score = cv2.filter2D(digit,-1,bank_of_digits[number]).max() #Returns 0-1 . 1 Means perfect match       
        print("Score for number " + number +" is: "+ str(np.round(score,2)) )
        if score > best_restult: #If we find better match
            best_restult = score #Set highest score yet
            matching_digit = number #Set best match number
    print("Best match: " + matching_digit)

最终结果将是“?”对于图3中的第一个数字,因为我们的银行中没有数字“1”,第二个结果将是“6”,得分为0.97。在

TLDR:我做了一个算法,从你的图像中分离出数字,并对这些数字进行比较。最佳匹配将打印出来。在

相关问题 更多 >

    热门问题