Matrix Dimensions must agree error

5 visualizzazioni (ultimi 30 giorni)
Nicolas Dracopoulos
Nicolas Dracopoulos il 5 Mar 2018
this is part of the whole code but it's here where the error occurs. the code takes an image (called 'face') which has been cropped from a larger image (called 'tt') at the specified dimensions. it then normalizes and plots histograms of both images. after that i try to do euclidean distance on both. the matlab compiler identifies the error at line: ED = sum((crop-face_vec).^2); it says that matrix dimensions must agree. i can't figure out what i am doing wrong and as i am new to matlab any help would be much appreciated. thank you.
face = imcrop(im, [142,76,31,31]);
imagesc(face);
face_vec = face(:);
normalizedImage = double(255*mat2gray(face));
normalizedImage_tt = double(255*mat2gray(tt));
hn1 = imhist(normalizedImage_tt)./numel(normalizedImage_tt);
hn2 = imhist(face)./numel(face);
ED_map = zeros(size(tt,1)-31, size(tt,2)-31);
for i = 1: size(tt,1)-31
for j = 1: size(tt,2)-31
crop = imcrop(tt,[i,j,31,31]);
crop = crop(:);
ED = sum((crop-face_vec).^2);
ED_map(i,j) = ED;
imagesc(ED);
end
end
  2 Commenti
Walter Roberson
Walter Roberson il 6 Mar 2018
Not sure what you are doing, but that looks like pretty inefficient code. Are you trying to find the best match for face inside tt by using a sliding window and euclidean distance?
Nicolas Dracopoulos
Nicolas Dracopoulos il 6 Mar 2018
that's exactly what i was doing but it's okay i figured it out after a while. thanks!

Accedi per commentare.

Risposte (1)

the cyclist
the cyclist il 6 Mar 2018
Modificato: the cyclist il 6 Mar 2018
Presumably, the variables crop and face_vec are different length vectors.
You could verify this by using the debugger to halt execution of your program and see what is going on. (Or at the very least, just print out the size of those two vectors just before you try to subtract them.)
  1 Commento
Nicolas Dracopoulos
Nicolas Dracopoulos il 6 Mar 2018
i figured out the issue it was the for loops. the first one was going through the columns and the second through the rows. it should have been the other way round. it works fine now

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by