Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用numpy比较多个数组,并识别差异最小的数组?_Python_Image Processing_Numpy_Scipy_Ocr - Fatal编程技术网

Python 如何使用numpy比较多个数组,并识别差异最小的数组?

Python 如何使用numpy比较多个数组,并识别差异最小的数组?,python,image-processing,numpy,scipy,ocr,Python,Image Processing,Numpy,Scipy,Ocr,我有一组数组(20x40),其值介于0和255之间(灰度图像) 我需要将一个给定的数组与一组10个用作参考的数组进行比较,然后选择一个最接近给定图像的数组 我知道这看起来像OCR,但在这种情况下,OCR不能做任何好事 我已经试过计算abs(x-y),但是结果还不够好 捕获: 参考: 只需将像素相乘,然后取所有像素之和: (0,0)的图像1*(0,0)的图像2+(0,1)的图像1*(0,1)的图像2+ 这类似于没有偏移的。() numpy中的命令只是 sum(a * b) 这个可能有个名字

我有一组数组(20x40),其值介于0和255之间(灰度图像)

我需要将一个给定的数组与一组10个用作参考的数组进行比较,然后选择一个最接近给定图像的数组

我知道这看起来像OCR,但在这种情况下,OCR不能做任何好事

我已经试过计算abs(x-y),但是结果还不够好

捕获:

参考:


只需将像素相乘,然后取所有像素之和:

  • (0,0)的图像1*(0,0)的图像2+(0,1)的图像1*(0,1)的图像2+
这类似于没有偏移的。()

numpy中的命令只是

sum(a * b)
这个可能有个名字,但我不知道是什么

我猜你会把参考数字一个接一个地与测量图像进行比较,看看它是哪个数字

您必须首先将参考数字与其自身进行比较,以找出完美匹配的外观,并通过此对每个数字进行规范化以获得相似性。不完全匹配的值将小于此值。例如:

0 1 3
1 2 3
2 0 0
与自身相比将产生28,但与自身相比将产生25

0 1 3
0 2 3
1 0 0

所以你的匹配是25/28=0.89。因此,您知道第二张图像很接近,但不相同,我在bskyb解决了一个类似的问题,即从视频流中获取OCR帧

最后,我为图像中的每个数字创建了一个坐标字典
(x,y,w,h)
,并编写了一个脚本,生成了数百个这些数字,并将它们保存为掩码。然后,其中一名测试人员选择了最好的蒙版(失真最小的蒙版),并将其保存为数字1的1.bmp,数字2的2.bmb

我们必须为每个数字创建18个不同的图像,以支持不同的分辨率和纵横比。然后在OCR过程开始时将这些掩码加载到字典中。我们将图像存储为numpy数组

def load_samples(parent_dir=r'c:\masks'):
"""Loads the OCR samples of all digits of all possible variations into
memory.
"""
m = dict() # m is our map, a dict of lists of numpy arrays
for d in os.listdir(parent_dir):
    if not os.path.isdir(os.path.join(parent_dir, d)):
        continue
    m[d] = []
    for i in range(10): # 10 images [0..9]
        filename = os.path.join(parent_dir, d, '%d.bmp'%i)
        m[d].append(imread(filename))
return m 
然后,对于我们读取的每个图像,我们将其分成数字,将数字转换为numpy数组,并将其与所有遮罩进行比较,我们必须找到最接近的匹配项,并基于此进行选择。数字映射是从上面的加载样本返回的内容

def image2digit(image, digits_map, video_args):
"""Our home made OCR, we compare each image of digit with 10 images of all
possible digits [0..10] and return the closest match.
"""
def absdiff(img1, img2):
    func = numpy.vectorize(lambda a, b: abs(int(a)-int(b)))
    v = func(img1, img2)
    w = coordinates[video_args]['w']
    h = coordinates[video_args]['h']
    return numpy.sum(v)/(w*h) # takes the average

# convert the image to a numpy array
image_array = fromimage(image) # from scipy.misc
# compare it with all variations
scores = []
for (i, ir) in enumerate(digits_map[video_args]):
    scores.append(absdiff(ir, image_array))
# return the best match as a string
index = numpy.argmin(scores)
return str(index)
这对我们来说效果很好,除了在一些失真的帧中,6被OCR识别为5。我正在试验在比较之前将图像转换为灰度,看看这是否有助于解决扭曲图像的问题。

这是同样的问题,小心。。。我说真的!