2017-06-28 127 views
0
# Read image in grayscale mode 
img = cv2.imread(inp_pic,0) 

    # Median Blur and Gaussian Blur to remove Noise 
img = cv2.medianBlur(img,3) 
img = cv2.GaussianBlur(img, (5, 5), 0) 
print(img) 

    # Adaptive Threshold for handling lightning 
im_th = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,5) 
print(im_th) 

功能自適應閾值這裏將返回一個空矩陣,但傳遞的圖像(IMG)是不是空矩陣(我檢查用print語句)爲什麼會這樣呢?cv2.adaptiveThreshold()返回空矩陣

你可以在這裏找到源代碼: https://github.com/tanmay-edgelord/HandwrittenDigitRecognition/blob/master/performRecognition.ipynb

+1

你可以發佈圖片嗎? –

+0

已添加到存倉庫 –

+0

您是否將'[0 0 0 ...,0 0 0]'指定爲空矩陣? – ZdaR

回答

1

我跑在圖像上你的代碼在https://github.com/tanmay-edgelord/HandwrittenDigitRecognition/blob/master/Digit1.jpg和它的工作如預期。我做的唯一改變是使用cv2.imshow()顯示圖像。

import numpy as np 
import cv2 
    # Read image in grayscale mode 
img = cv2.imread(r"path\to\img",0) 

    # Median Blur and Gaussian Blur to remove Noise 
img = cv2.medianBlur(img,3) 
img = cv2.GaussianBlur(img, (5, 5), 0) 
#print(img) 

    # Adaptive Threshold for handling lightning 
im_th = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,5) 
cv2.imshow("te", im_th) 
if cv2.waitKey(): 
    cv2.destroyAllWindows() 

這裏的結果:

enter image description here

爲什麼你一定這是一個空矩陣?如果你只是從[0 0 0 ..., 0 0 0]來判斷,這是一個非常大的陣列。 Python只是將其作爲數組的縮寫版本顯示。只需使用np.maximum(img_th)即可獲得數組中的最大值,您將看到最大值不爲零。

+0

代碼是否能夠成功識別數字? for循環運行成功了嗎?主要問題是for循環中的resize函數報告了一個空圖像,我認爲這是因爲閾值矩陣是空的。 –

+0

我發佈了結果圖片。沒有錯誤報告。 希望這回答了您發佈的問題 –