error in corr2 function in matlab

I want to read letter from input image by template matching using corr2 function. For this purpose, I have created templates of number and symbols. While running the code for reading letter, i got the following error:

Error using corr2
Expected input number 1, A, to be two-dimensional.

enter image description here

MAtlab code for template creation

%CREATE TEMPLATES
one=imread('1.png'); 
one=imresize(one,[42 24]);
two=imread('2.png');
two=imresize(two,[42 24]);
three=imread('3.png');
three=imresize(three,[42 24]);
four=imread('4.png');
four=imresize(four,[42 24]);
five=imread('5.png'); 
five=imresize(five,[42 24]);
zero=imread('0.png');
zero=imresize(zero,[42 24]);
sign=imread('sign.png');
sign=imresize(sign,[42 24]);
number=[one two three four  five zero ];
character=[number sign];
NewTemplates1=mat2cell(character,42,[24 24 24 24 24 24 24], 3);
save ('NewTemplates1','NewTemplates1')
clear all

matlab code for reading the letter

load NewTemplates1 % Loads the templates of characters in the memory.
snap=imread('untitled.png');
snap=imresize(snap,[42 24]); % Resize the input image so it can be compared with the template's images.
comp=[ ];
for n=1:length(NewTemplates1)
    sem=corr2(NewTemplates1{1,n},snap); % Correlation the input image with every image in the template for best matching.
    comp=[comp sem]; % Record the value of correlation for each template's character.
end
vd=find(comp==max(comp)); % Find the index which correspond to the highest matched character.
%*-*-*-*-*-*-*-*-*-*-*-*-*-
% Accodrding to the index assign to 'letter'.
% Alphabets listings.
if vd==1 
    letter='1';
elseif vd==2 
    letter='2';
elseif vd==3
    letter='3';
elseif vd==4 
    letter='4';
elseif vd==5
    letter='5';
elseif vd==6
    letter='0';
elseif vd==7
    letter='sign';
end

For kind information: The size of individual component in template is 42x24x3 uint8 and the input image also same dimension. Please provide the suggestion to do further.

1 answer

  • answered 2018-01-14 11:30 Hossein Ka

    You only need to use rgb2gray to convert rgb to gray images( 2-d matrices) like :

    %CREATE TEMPLATES
    one=rgb2gray(imread('1.png')); 
    one=imresize(one,[42 24]);
    two=rgb2gray(imread('2.png'));
    two=imresize(two,[42 24]);
    three=rgb2gray(imread('3.png'));
    three=imresize(three,[42 24]);
    number=[one two three];
    NewTemplates1=mat2cell(number,42,[24 24 24]);
    snap=rgb2gray(imread('2Ttt0.png'));
    snap=imresize(snap,[42 24]); % Resize the input image so it can be compared with the template's images.
    comp=[ ];
    for n=1:length(NewTemplates1)
        sem=corr2(NewTemplates1{1,n},snap); % Correlation the input image with every image in the template for best matching.
        comp=[comp sem]; % Record the value of correlation for each template's character.
    end
    vd=find(comp==max(comp)); % Find the index which correspond to the highest matched character.
    %*-*-*-*-*-*-*-*-*-*-*-*-*-
    % Accodrding to the index assign to 'letter'.
    % Alphabets listings.
    if vd==1 
        letter='1';
    elseif vd==2 
        letter='2';
    elseif vd==3
        letter='3';
    elseif vd==4 
    
    end