sum(Array)表示索引超出矩阵尺寸 [英] sum(Array) says index exceeds matrix dimensions
问题描述
我是Matlab的新手,我正在尝试进行一些图像匹配,图像X较大,图像Y较小,我们的目标是找到y与x的所有可能重叠,然后计算每个这样的RGB的平方差之和重叠(即x ^ 2 + y ^ 2 + z ^ 2).因此,我在较大的图像中找到了一个位置(i,j),我们从中选择一个图块并进行匹配.
确切地说,这里是代码:-
I am a newbie to Matlab .I was trying to do some image matching.Image X is bigger and Image Y is smaller.Our aim is to find all possible overlaps of y with x and then compute sum of square differences for RGB at each such overlap(ie x^2+y^2+z^2).So I find a location (i,j) in bigger image from where we pick a tile and match .
To be precise here is the code :-
a=size(X,1)-size(Y,1);
b=size(X,2)-size(Y,2);
Z=ones(a,b);
for i=1:a
for j=1:b
A=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,1)-Y(1:end,1:end,1)].^2;
B=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,2)-Y(1:end,1:end,2)].^2;
C=[X(i:i+size(Y,1)-1,j:j+size(Y,2)-1,3)-Y(1:end,1:end,3)].^2;
T=A+B+C
size(T)
P=sum(sum(T))
%Z(i,j)=sum(sum(T));
end;
end;
现在的问题是,行sum(sum(T))抛出一个错误-索引超出矩阵维数".有人可以帮忙吗?我的目的是将所有这些差值相加并将其放在矩阵Z中.>
Now the problem is the line sum(sum(T)) throws up an error-'Index exceeds Matrix Dimensions'.Can someone help?My aim is to add all these differences and put it in a matrix Z.
推荐答案
获取此类错误的唯一方法是在某处声明了变量sum
.
The only way you can get such a bug is if you have declared a variable sum
somewhere.
运行clear sum
(或clear all
,或重新启动Matlab),然后重试,然后一切正常.
Run clear sum
(or clear all
, or restart Matlab) and try again, then everything should work fine.
这篇关于sum(Array)表示索引超出矩阵尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!