我確實有一組數值(20x40),值在0到255之間(灰度圖像)。如何使用numpy比較幾個陣列並確定最小差異?
我需要比較給定的數組和一組10個用作參考的數組,並選擇最接近給定圖像的數組。
我知道這看起來像OCR,但在這種情況下,OCR是無法做到的。
我已經嘗試計算abs(x-y)
但結果不夠好。
捕獲:
參考:
我確實有一組數值(20x40),值在0到255之間(灰度圖像)。如何使用numpy比較幾個陣列並確定最小差異?
我需要比較給定的數組和一組10個用作參考的數組,並選擇最接近給定圖像的數組。
我知道這看起來像OCR,但在這種情況下,OCR是無法做到的。
我已經嘗試計算abs(x-y)
但結果不夠好。
捕獲:
參考:
像素僅有相乘在一起,然後取所有像素的總和:
這就像cross-correlation沒有偏移。 (scipy.signal.correlate)
在numpy的命令也只是
sum(a * b)
有可能是這個名字,但我不知道它是什麼。
我猜你會將參考數字與測量圖像逐一比較,看看它是哪個數字?
您必須首先比較參考數字與自己,才能找出完美匹配的樣子,並通過此規範化每個參數來獲得相似性。不完美的匹配會比這個值小。例如:當與自身相比
0 1 3
1 2 3
2 0 0
會產生28,但與
0 1 3
0 2 3
1 0 0
相比,所以,你的對手將是25/28 = 0.89將產生25。所以你知道第二個圖像是接近的,但不一樣
我一直在解決一個類似的問題在bskyb從OCRing幀取自一個視頻流。
我最終創建了圖像中的每個數字的座標字典(x,y, w, h)
,並寫了一個腳本,生成數百個這些數字並將它們保存爲掩碼。其中一名測試人員然後選擇了最好的蒙版(最少扭曲的蒙版),並將它們保存爲1.bmp代表數字1,2.bmb代表數字2 ...
我們必須爲每個數字創建18個不同的圖像支持我們擁有的各種分辨率,aspect_ratios。然後這些口罩在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的陣列,並與所有我們必須找到最接近的匹配,並選擇它的基礎上,該口罩其容比較它劃分成數字。 digits_map是從上面的load_samples返回的內容。
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。我與轉化的圖像轉換成灰度比較之前,看看是否能與失真的圖像問題,有助於試驗一些扭曲的幀。
這是*相同*的問題,保重! ... 我是認真的! – sorin