image processing - Face recognition in MATLAB -
i having error, saying:
subscripted assignment dimension mismatch.
error in facerecognition (line 14) images(:, n) = img(:);
can help? code have written below:
input_dir = 'd:\c.s\fyp\matlab projects\dip applications\face recognition\faces\'; image_dims = [48, 64]; filenames = dir(fullfile(input_dir, '*.jpg')); num_images = numel(filenames); images = []; n = 1:num_images filename = fullfile(input_dir, filenames(n).name); img = imread(filename); if n == 1 images = zeros(prod(image_dims), num_images); end images(:, n) = img(:); end % steps 1 , 2: find mean image , mean-shifted input images mean_face = mean(images, 2); shifted_images = images - repmat(mean_face, 1, num_images); % steps 3 , 4: calculate ordered eigenvectors , eigenvalues [evectors, score, evalues] = princomp(images'); % step 5: retain top 'num_eigenfaces' eigenvectors (i.e. principal components) num_eigenfaces = 20; evectors = evectors(:, 1:num_eigenfaces); % step 6: project images subspace generate feature vectors features = evectors' * shifted_images; % calculate similarity of input each training image feature_vec = evectors' * (input_image(:) - mean_face); similarity_score = arrayfun(@(n) 1 / (1 + norm(features(:,n) - feature_vec)), 1:num_images); % find image highest similarity [match_score, match_ix] = max(similarity_score); % display result figure, imshow([input_image reshape(images(:,match_ix), image_dims)]); title(sprintf('matches %s, score %f', filenames(match_ix).name, match_score))
;
the error looks it's being caused due inconsistent image dimensions specified @ beginning in comparison dimensions read in images have in directory. suggest dynamically read in image dimensions when read in first image. btw, only work if of images same dimensions in directory. also, assuming of images grayscale. however, can put code in place if images not same size, , can use first image reference dimensions. should image have dimensions unequal first image read in, we'll resize image conforms size.
if have colour images, you'll want convert them grayscale first before running code. such, modify first for
loop code (where you're reading in images) following. code, anywhere have modified, you'll see %// new
statement beside each line of code have placed in:
image_dims = []; %// new - set instead of [48,64] n = 1:num_images filename = fullfile(input_dir, filenames(n).name); img = imread(filename); if size(img,3) == 3 %// new - convert gray scale if necessary img = rgb2gray(img); %// use rgb2gray if colour end if n == 1 image_dims = size(img); %// new - read in image dimensions here images = zeros(prod(image_dims), num_images); else %// new - if image read in not same dimensions %// first image read in, resize image accordingly if size(img,1) ~= image_dims(1) || size(img,2) ~= image_dims(2) img = imresize(img, image_dims, 'bilinear'); end end images(:, n) = img(:); end
Comments
Post a Comment