Subscripted assignment dimension mismatch.

%%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

Jan
Jan on 23 Aug 2017
Edited: Jan on 23 Aug 2017
Please do not let us guess, where the error occurs, but post the complete error message, which mentions the failing line also.
Did you ask the author Avi Nehemiah already?
I'm not guessing either. Read this link and fix your post and then maybe you'll get an answer.
I Edited the question...hope it is more clear now.
Sorry for repeating, Dimitrios: Please post the complete copy of the error message. Mentioning some parts only is less useful.
It is hard to believe, that this line:
trainingFeatures = zeros(size(training,2)*training(1).Count,4680);
causes a "Subscripted assignment dimension mismatch" message. If it really does, use the debugger to examine the problem:
dbstop if error
Then runs the code again until it stops at the error. Now check the parts:
which training
class training
which size
size training
training(1).Count
size(training,2) * training(1).Count
which zeros
What do you see? Why is this line failing? I do not see and potential problem with a dimension here.
Note that almost the complete posted code does not explain the problem.
I run the thing as you mentioned and the line that throws the error is exactly this one here:
trainingFeatures(featureCount,:) = extractHOGFeatures(read(training(i),j));
Here you can see exactly what the command windows prints when the error happens. Note that previously on the mismatch error was printed. http://prntscr.com/gcjtu4
(Now i am using the YALE faces dataset, not the AT&T, for your information). Images from the YALE dataset look like this http://prntscr.com/gck2lw , while images from the AT&T dataset are .pgm format and i cannot even view them. http://prntscr.com/gck3qc
How can I solve this error?
Error:
Subscripted assignment dimension mismatch.
trainingLabel(featureCount,:) = TrainingimgSets(i).Description;
The Description field does not have the label information. It is very likely to generate that error.
Notice the original code is
trainingLabel{featureCount} = training(i).Description;
which assigns into a cell array, not into a rectangular array.

Sign in to comment.

 Accepted Answer

.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

As you see the code now it works...using the AT&T dataset. Thats exactly as i found it from a tutorial. If a change the dataset it fails in the place i 've stated.
I cannot find the AT&T dataset.
In the forth line from the beginning you can see the ATTFaces, With that is works, as i foudn it like that form the tutorial. I Changed that to the YALEFACESFOLDERS folder, which is the dataset of the YALE faces, and i have the error i shown above. Here is a screenshot of the names of the different datasets that i try to test this code. http://prntscr.com/gck0gj
The code you are using assumes that the returned HOG feature vector will be exactly 4680 long. That size depends on the size of the input image and upon the default value of some parameters, and is explained at https://www.mathworks.com/help/vision/ref/extracthogfeatures.html#output_argument_d0e143422 . If you change datasets to use images of a different size, you will need to alter that 4680.
Instead of hard-coding at 4680, the code could instead use length(hogFeature) -- the length of the vector returned by processing the first image.

Hello Mr Robertson,

Sorry for the late reply. I was very busy with work and just now i came back to this problem. Don't think i forgot or i found my solution and i ignored the website. :)

I changed the Count,4680 to Count, length(hogFeature) as you entioned and the code worked with a differetn dataset. Now i face another error in the end of the code. This is the error http://prntscr.com/gdzcv3 and appears in the last part of the code.

%% 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

It appears when i run this part but there is no error there. The error is in a class called subplot, which is embedded on MATLAB. http://prntscr.com/gdzfc1 When i run that part instead of oppening 5 windows with matched images of the first 5 subjects in the dataset, only the first one is opened and this error is thrown. I played little bit with the numbers in the integerIndex subplot line by changing the 2,2 to other numbers. I did not get a correct result.

Any explanation/help for this issue would be much appreciated. Dimitrios

%%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

Sign in to comment.

More Answers (1)

Hello everyone,
I managed to fix all the issues, by resizing all the photos of all my datasets in the same size. Also by increasing the indexes i was able to open as many as i wanted. I want to specifically thank Walter Roberson as his help with the length(hogFeature) was very important.

1 Comment

@ 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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!