2014-04-21 58 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');