Subscripted assignment dimension mismatch.
2 views (last 30 days)
Show older comments
Dimitrios Kampolis
on 23 Aug 2017
Commented: Walter Roberson
on 11 Mar 2020
%%Simple Face Recognition Example
% Copyright 2014-2015 The MathWorks, Inc.
%%Load Image Information from ATT Face Database Directory
faceDatabase = imageSet('ATTFaces','recursive');
%%Display Montage of First Face
figure;
% (faceDatabase(1) means pull out all images belong to folder 1
montage(faceDatabase(1).ImageLocation);
title('Images of a Single Face');
Display Query Image and Database Side-Side
personToQuery = 1; % Call the a set of images at position 1
galleryImage = read(faceDatabase(personToQuery),1);
figure;
for i=1:size(faceDatabase,2)
imageList(i) = faceDatabase(i).ImageLocation(5);
end
subplot(1,2,1);
imshow(galleryImage);
subplot(1,2,2);
montage(imageList);
diff = zeros(1,9);
%%Split Database into Training & Test Sets in the ration 80% to 20%
[training,test] = partition(faceDatabase,[0.8 0.2]);
%%Extract and display Histogram of Oriented Gradient Features for single face
person = 1;
[hogFeature, visualization]= ...
extractHOGFeatures(read(training(person),1));
figure;
subplot(2,1,1);imshow(read(training(person),1));title('Input Face');
subplot(2,1,2);plot(visualization);title('HoG Feature');
%%Extract HOG Features for training set
trainingFeatures = zeros(size(training,2)*training(1).Count,4680);
featureCount = 1;
for i=1:size(training,2)
for j = 1:training(i).Count
trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));
trainingLabel{featureCount} = training(i).Description;
featureCount = featureCount + 1;
end
personIndex{i} = training(i).Description;
end
%%Create 40 class classifier using fitcecoc
faceClassifier = fitcecoc(trainingFeatures,trainingLabel);
%%Test Images from Test Set
person = 2;
queryImage = read(test(person),1);
queryFeatures = extractHOGFeatures(queryImage);
personLabel = predict(faceClassifier,queryFeatures);
% Map back to training set to find identity
booleanIndex = strcmp(personLabel, personIndex);
integerIndex = find(booleanIndex);
subplot(1,2,1);imshow(queryImage);title('Query Face');
subplot(1,2,2);imshow(read(training(integerIndex),1));title('Matched Class');
%%Test First 5 People from Test Set
figure;
figureNum = 1;
for person=1:5
for j = 1:test(person).Count
queryImage = read(test(person),j);
queryFeatures = extractHOGFeatures(queryImage);
personLabel = predict(faceClassifier,queryFeatures);
% Map back to training set to find identity
booleanIndex = strcmp(personLabel, personIndex);
integerIndex = find(booleanIndex);
subplot(2,2,figureNum);imshow(imresize(queryImage,3));title('Query Face');
subplot(2,2,figureNum+1);imshow(imresize(read(training(integerIndex),1),3));title('Matched Class');
figureNum = figureNum+2;
end
figure;
figureNum = 1;
end
Hello guys,
I found this tutorial on youtube that uses hog features for face recognition on MATLAB. I am playing around with it cause i am doing similar things for my dissertation. In this one when i run it as it is with the ATTFaces dataset (contains 10 images for each individual, 40 in total in .pgm format) it works fine. When i try to change the dataset, using the YALE faces, or a custom dataset i have my personal images i get this mismatch error that you can see in the title. The other datasets have images in .jpg by the way and are more that 10 per folder. Can someone help me and tell me why i get this error? I gave you information about the format of the images and thier size. Also the number of them in the folders. Maybe one of these parameters makes it fail. If yes, which one.
Regards to everyone :)
EDIT*** I 've googled the error first obviously but i wasn't able to find the reason why is happening. I suspect one of the things i asked in the end... Yea i forgot to mention the error with different datasets occurs here:
%%Extract HOG Features for training set
trainingFeatures = zeros(size(training,2)*training(1).Count,4680);
and it shows the error you see on the title.
The code as you it, it works using the AT&T dataset. IF i change it to a different dataset shows that error i mentioned in that particular line.
8 Comments
Walter Roberson
on 11 Mar 2020
The Description field does not have the label information. It is very likely to generate that error.
Walter Roberson
on 11 Mar 2020
Notice the original code is
trainingLabel{featureCount} = training(i).Description;
which assigns into a cell array, not into a rectangular array.
Accepted Answer
Walter Roberson
on 23 Aug 2017
.jpg files are almost always RGB files, even if they look gray. Code that is expecting grayscale images will often fail on RGB files.
6 Comments
Walter Roberson
on 28 Aug 2017
%%Test First 5 People from Test Set
figure;
figureNum = 1;
for person=1:5
ntest = test(person).Count;
for j = 1:ntest
queryImage = read(test(person),j);
queryFeatures = extractHOGFeatures(queryImage);
personLabel = predict(faceClassifier,queryFeatures);
% Map back to training set to find identity
booleanIndex = strcmp(personLabel, personIndex);
integerIndex = find(booleanIndex);
subplot(ntest,2,figureNum);imshow(imresize(queryImage,3));title('Query Face');
subplot(ntest,2,figureNum+1);imshow(imresize(read(training(integerIndex),1),3));title('Matched Class');
figureNum = figureNum+2;
end
figure;
figureNum = 1;
end
More Answers (1)
Dimitrios Kampolis
on 28 Aug 2017
1 Comment
Chidiebere Ike
on 3 Apr 2018
@ Dimitrios Kampolis. Could you please help me on this code ? I am having error message on my Classifier and it's frustrating not achieving testing. You can share your M file on this code to my email chidosky4ril@yahoo.com . Thanks so much
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!