Matrix Dimensions must agree error

5 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-3-6
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
that's exactly what i was doing but it's okay i figured it out after a while. thanks!

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2018-3-6
编辑:the cyclist 2018-3-6
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 个评论
Nicolas Dracopoulos
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

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by