Extract features from training images (Matlab - Computer Vision)

3 ビュー (過去 30 日間)
Travis Smith
Travis Smith 2018 年 11 月 20 日
回答済み: Udit06 2024 年 7 月 5 日
Hello. Thanks for reading my question. Sorry if it's an easy question but I'm new in Matlab and Computer Vision. I'm trying to extract features from training images. I need to use histogram of color, histogram of gradient, SIFT-like features and compare their performance. I'm lost and have spent days to find a good way to use. How would you extract features in this example? I have put %Start and %End where I need to do that.
A written code would be greatly appreciated.
% Step-1: Load training and test data using imageSet.
imageDir = fullfile('./data');
% imageSet recursively scans the directory tree containing the images.
flowerImageSet = imageSet(imageDir, 'recursive');
%% step-2: Partition the data set into a training set and a test set.
tmparr=randperm(80);
arrTrainID=tmparr(1:50); %training ID;
arrTestID=tmparr(51:end);% testing ID;
%% Step-3: Train the classifier using features extracted from the training set.
% Step 3.1 Extract features from training images
trainingFeatures = [];
trainingLabels = [];
featureSize =128;
for flower = 1:numel(flowerImageSet)
numImages = numel(arrTrainID);
features = zeros(numImages, featureSize);
for i = 1:numImages
img = rgb2gray(read(flowerImageSet(flower), arrTrainID(i)));
%START
features(i, :) = rand(1,featureSize);
%END
end
% Use the imageSet Description as the training labels.
labels = repmat(flowerImageSet(flower).Description, numImages, 1);
trainingFeatures = [trainingFeatures; features];
trainingLabels = [trainingLabels; labels ];
end
%%
% Step 3.2, train a classifier using the extracted features.
classifier = fitcecoc(trainingFeatures, trainingLabels);
%% Step 4: Evaluate the Flower Classifier
testFeatures=[];
testLabels=[];
% Step 4.1: Loop over the testing images and extract features from each image.
for flower = 1:numel(flowerImageSet)
numImages = numel(arrTestID); %
features = zeros(numImages, featureSize);
for i = 1:numImages
img = rgb2gray(read(flowerImageSet(flower), arrTestID(i)));
%Start
features(i, :) = rand(1,featureSize);
%End
end
% Use the imageSet Description as the training labels.
labels = repmat(flowerImageSet(flower).Description, numImages, 1);
testFeatures = [testFeatures; features];
testLabels = [testLabels; labels ];
end
% Step 4.2: Make class predictions using the test features.
predictedLabels = predict(classifier, testFeatures);
% Step 4.3: Tabulate the results using a confusion matrix.
confMat = confusionmat(testLabels, predictedLabels);
% Step 4.4: calculate accuracy
fprintf('accuracy=%f',sum(diag(confMat))/sum(confMat(:)));

回答 (1 件)

Udit06
Udit06 2024 年 7 月 5 日
You can use the MATLAB functions "detectSIFTFeatures" and "extractHOGFeatures" to extract the SIFT and histogram of gradient features of an image respectively. Please refer to the following MathWorks documentation for more details:
The following MATLAB answer describe how you can get color histogram of an image.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by