2013-02-20 188 views
0

我寫了下面的代碼片段,但是,我收到了索引超出界限的錯誤。我不確定如何解決這個問題。有誰知道我可以如何解決它。提前致謝!MATLAB索引超出範圍錯誤

image = imread('image_3.jpg'); 

%parameters 
N = 100; 
smth = rgb2gray(image); 

% Calculating size of image 
[row col] = size(image); 

eline = smth; %eline is simply the image intensities 

[grady,gradx] = gradient(double(smth)); 

eedge = -1 * sqrt ((gradx .* gradx + grady .* grady)); %eedge is measured by gradient in the image 

m1 = [-1 1]; 
m2 = [-1;1]; 
m3 = [1 -2 1]; 
m4 = [1;-2;1]; 
m5 = [1 -1;-1 1]; 

cx = conv2(smth,m1,'same'); 
cy = conv2(smth,m2,'same'); 
cxx = conv2(smth,m3,'same'); 
cyy = conv2(smth,m4,'same'); 
cxy = conv2(smth,m5,'same'); 

for i = 1:row 
for j= 1:col-1 

    eterm(i,j) = (cyy(i,j)*cx(i,j)*cx(i,j) -2 *cxy(i,j)*cx(i,j)*cy(i,j) + cxx(i,j)*cy(i,j)*cy(i,j))/((1+cx(i,j)*cx(i,j) + cy(i,j)*cy(i,j))^1.5); 
end 
end 

??? Attempted to access cyy(1,901); index out of bounds because 
size(cyy)=[700,900]. 

Error in ==> snake at 31 
    eterm(i,j) = (cyy(i,(j-1)+1)*cx(i,j)*cx(i,j) -2 
    *cxy(i,j)*cx(i,j)*cy(i,j) + 
    cxx(i,j)*cy(i,j)*cy(i,j))/((1+cx(i,j)*cx(i,j) + 
    cy(i,j)*cy(i,j))^1.5); 
+0

的嵌套for循環之前驗證的'COL-1' – slayton 2013-02-20 15:49:20

+0

的值COL-1是2699 – 2013-02-20 15:52:32

+0

然而CYY的大小是700由900 – 2013-02-20 16:10:08

回答

1

變化,你計算圖像尺寸與行:

[行山口] =尺寸(不便);

圖像是RGB,所以是(我推測)700乘900乘3.由於輸入有三個維度,而您只需要兩個輸出,所以它將最後兩個維度相乘,給出col = 2700 。