Matrix Dimensions must agree error

조회 수: 4 (최근 30일)
Nicolas Dracopoulos
Nicolas Dracopoulos 2018년 3월 5일
댓글: Nicolas Dracopoulos 2018년 3월 6일
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
Nicolas Dracopoulos 2018년 3월 6일
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
Nicolas Dracopoulos 2018년 3월 6일
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