2014-04-21 62 views
0

我似乎在開發光學字符識別引擎時偶然發現了一個小問題。我已經在MNIST圖像上訓練了K最近鄰分類器,並對其進行了測試。它似乎工作正常。但是,當我輸入不同尺寸的圖像時,似乎無法正確分類輸入圖像。 有關如何解決此問題的任何建議?通過比較不同尺寸的圖像來檢測字符

I] KNN分類器 -

爲KNN分類的代碼是:

% herein, I resize the binary image 'b' to contain the 
% same dimensions as the training set 'trainingImages' as the input and training Images  
% should have the same no. of columns/dimensions 

b = imresize(b, size(trainingImages)); 

% now i try to classify the input image 'b' against the set of training images and 
% training labels. 

cls = knnclassify(b, trainingImages, trainingLabels, 3, 'euclidean'); 

CLS現在是分類矢量。但是,無論輸入圖像如何,這幾乎總是顯示1的錯誤分類。

另一方面,當我對MNIST測試圖像進​​行分類時,得到了非常高的準確性!對於相同的代碼如下 -

class = knnclassify(testImg, trainingImages, trainingLabels, 3, 'euclidean'); 

現在的主要問題是,不管是什麼樣的輸入圖像的,我給它來預測,它主要是給了我一個錯誤的結果(變化爲不同的圖像),即使是那些截然不同的圖像。似乎它不能正常工作。有人能幫我看看這裏的問題應該在哪裏?我無法從互聯網上的現有資源中找到任何解釋。提前致謝。

+0

如果訓練集trainingImages包含所有模板圖像,那麼您將調整單個圖像b到您的訓練集大小。你應該將b的大小設置爲訓練集中單個字符的大小 – dhanushka

+0

謝謝Dhanushka! 我也是在最後一天晚上得出這個結論的。 – vsdaking

回答

0

我相信我解決了上面列出的問題。 的問題是:

  1. 像Dhanushka說,我是將原來的輸入圖像的尺寸以匹配訓練圖像集的尺寸(在MNIST的情況下是60000 * 784,這意味着60000位和784每個數字的特徵[28 * 28])。 因此,我只是將輸入圖像的尺寸更改爲28 * 28。

  2. 預處理輸入圖像。 我只是將圖像轉換爲二進制圖像,並試圖對MNIST訓練圖像數據集進行分類。這是一個INCOMPLETE程序。 當我進一步檢測到輸入二值圖像(Canny,Prewitt或Zerocross - 無論哪個更適合您)的邊緣並將其用於分類時,我得到了非常準確的預測結果!

注意:在KNN分類中,您必須通過試錯法得出相鄰像素的數量。我設法在以下結論到達 -

  1. 3相鄰像素是通常足夠用於合成影像
  2. 1周邊像素是最適合於手寫圖像

對於相同的是如下所述的代碼:

% herein, I resize the binary image 'b' as the input and training Images  
    % should have the same no. of columns/dimensions 

    b = imresize(b, [28 28]); % this resizes the binary image b to 28*28 dimension 
    b = edge(b, 'canny');  % this uses Canny edge detection on the resized binary 
           % image 
    b = b(:)';     % This converts 'b' to a vector using b(:) and then 
           % transposes the result using the " ' " operator 
           % Thus, now 'b' has same no of dimensions/columns as 
           % MNIST training image set 

    % now i try to classify the input image 'b' against the set of training images 
    % and training labels. 

    cls = knnclassify(b, trainingImages, trainingLabels, 3, 'euclidean');