matlab基于SVM的手写字体识别,MATLAB--数字图像处理 HOG+SVM识别手写数字

syntheticDir = fullfile(toolboxdir('vision'), 'visiondata','digits','synthetic');

handwrittenDir = fullfile(toolboxdir('vision'), 'visiondata','digits','handwritten');

% |imageDatastore| recursively scans the directory tree containing the

% images. Folder names are automatically used as labels for each image.

trainingSet = imageDatastore(syntheticDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');

testSet = imageDatastore(handwrittenDir, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');

img = readimage(trainingSet, 206);

% Extract HOG features and HOG visualization

[hog_2x2, vis2x2] = extractHOGFeatures(img,'CellSize',[2 2]);

[hog_4x4, vis4x4] = extractHOGFeatures(img,'CellSize',[4 4]);

[hog_8x8, vis8x8] = extractHOGFeatures(img,'CellSize',[8 8]);

cellSize = [4 4];

hogFeatureSize = length(hog_4x4);

numImages = numel(trainingSet.Files);

trainingFeatures = zeros(numImages, hogFeatureSize, 'single');

for i = 1:numImages

img = readimage(trainingSet, i);

img = rgb2gray(img);

% Apply pre-processing steps

img = imbinarize(img);

trainingFeatures(i, :) = extractHOGFeatures(img, 'CellSize', cellSize);

end

% Get labels for each image.

trainingLabels = trainingSet.Labels;

classifier = fitcecoc(trainingFeatures, trainingLabels);

[testFeatures, testLabels] = helperExtractHOGFeaturesFromImageSet(testSet, hogFeatureSize, cellSize);

% Make class predictions using the test features.

predictedLabels = predict(classifier, testFeatures);

% Tabulate the results using a confusion matrix.

confMat = confusionmat(testLabels, predictedLabels);

helperDisplayConfusionMatrix(confMat)

你可能感兴趣的:(matlab基于SVM的手写字体识别,MATLAB--数字图像处理 HOG+SVM识别手写数字)