train.m
上传用户:zslfgd
上传日期:2010-04-06
资源大小:115k
文件大小:3k
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %% %%
- %% Prof. Sclaroff's CS585 Image avd Video Processing %%
- %% Project ONE %%
- %% C H A R A C T E R R E C O G N I T I O N %%
- %% %%
- %% by Stanislav Rost %%
- %% ID: 31764117 %%
- %% %%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % TRAIN.M
- %
- % The training program of the letter recognition project. Loads the
- % training data, the shows the user each letter and asks the user what
- % the letter is. Then it calculates parameters (momnets, compactness,
- % top-to-bottom area ratio) for each letter. In the end, the parameters
- % for all instances of each vowel are used to calculate the mean vector
- % and the covariance matrix.
- trainData = im2bw(imread('training.tif','tif'),0.5);
- trainData = ~trainData;
- figure;
- imshow(trainData);
- colormap(gray);
- % Label letters
- figure;
- trainData = bwlabel(trainData, 4);
- imagesc(trainData);
- % Extract letters
- letters = imfeature(trainData,'Image', ...
- 'Area', 'BoundingBox');
- numLetters = max(trainData(:));
- numAs = 0;
- numOs = 0;
- numUs = 0;
- numEs = 0;
- fhandle = figure;
- disp('Enter - for non-letters and non-inportant letters');
- for i = 1:numLetters,
- imshow(letters(i).Image);
- letterName = ...
- input('Which letter is this ?','s');
- if ~strcmp(letterName, '-')
- curMoments = invmoments(letters(i).Image);
- perImg = bwperim(letters(i).Image);
- perArea = bwarea(perImg);
- compactness = perArea^2/letters(i).Area;
- % Do ratios of top to bottom
- midpoint = floor(letters(i).BoundingBox(4) /2);
- topPart = letters(i).Image(1:midpoint,:);
- bottomPart = letters(i).Image((midpoint+1):end,:);
-
- partRatio = bwarea(topPart)/bwarea(bottomPart);
- % Add the vector to arrays of info for each vowel
- if strcmp(letterName, 'a')
- numAs = numAs + 1;
- ainfo(numAs,:) = [ curMoments(1) curMoments(2) curMoments(3) compactness partRatio ];
- elseif strcmp(letterName, 'o')
- numOs = numOs + 1;
- oinfo(numOs,:) = [ curMoments(1) curMoments(2) curMoments(3) compactness partRatio ];
- elseif strcmp(letterName, 'u')
- numUs = numUs + 1;
- uinfo(numUs,:) = [ curMoments(1) curMoments(2) curMoments(3) compactness partRatio ];
- elseif strcmp(letterName, 'e')
- numEs = numEs + 1;
- einfo(numEs,:) = [ curMoments(1) curMoments(2) curMoments(3) compactness partRatio ];
- end
- end
- end
- close(fhandle);
- meanA = mean(ainfo, 1)
- meanU = mean(uinfo, 1)
- meanE = mean(einfo, 1)
- meanO = mean(oinfo, 1)
- covA = cov(ainfo)
- covU = cov(uinfo)
- covE = cov(einfo)
- covO = cov(oinfo)
- % Write out information for letter A
- save 'a.mat' meanA covA
- % Write out information for letter O
- save 'o.mat' meanO covO
- % Write out information for letter E
- save 'e.mat' meanE covE
- % Write out information for letter U
- save 'u.mat' meanU covU