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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值