How to correct this code?

2 次查看(过去 30 天)
luisa bertoli
luisa bertoli 2018-5-20
回答: Stephen23 2018-5-20
Hi, I'm dealing with image processing. I have some problems with this code:
%The first step is to analyze the video and translate it into a structure in the matlab workspace.
T=30000;u=1;
mov_new(1).cdata=mov(1).cdata; %initialize the first element of the new structure
for k=2:length(mov)
if sum(sum(sum(mov(k).cdata-mov(k-1).cdata)))>T
u=u+1;
mov_new(u).cdata=mov(k).cdata;
end;
end;
A= mov_new;
max=0;
conx=0;
cony=0;
v=0;
DD=zeros(1995,3);
for k=1:1995
for i=1:336
for j=1:432
if (i < 4-[size((A(k).cdata),1)]) && (j < 4-[size((A(k).cdata),2)]);
v=A(k).cdata(i,j)+A(k).cdata(i+1,j)+A(k).cdata(i,j+1)+A(k).cdata(i+1,j+1)+A(k).cdata(i+2,j)+A(k).cdata(i +2,j+1)+A(k).cdata(i,j+2)+A(k).cdata(i+1,j+2)+A(k).cdata(i+2,j+2)+A(k).cdata(i,j+3)+A(k).cdata(i+1,j+3) +A(k).cdata(i+2,j+3)+A(k).cdata(i+3,j+3)+A(k).cdata(1+3,j)+A(k).cdata(1+3,j+1)+A(k).cdata(1+3,j+2);
else
v=A(k).cdata(i,j);
end;
if v>max
max=v;
conx=i;
cony=j;
end;
end;
end;
DD(k,1)=max;
DD(k,2)=conx;
DD(k,3)=cony;
max=0;
end;
The problem is that the following errors appear:
Index exceeds array bounds.
Error in met1 (line 21)
if (i < 4-[size((A(k).cdata),1)]) && (j < 4-[size((A(k).cdata),2)]);
Can anybody help me to correct it? thank you Luisa
  2 个评论
Walter Roberson
Walter Roberson 2018-5-20
We as outsiders have no reason to expect that the sum sum condition succeeded 1995 or more times to create mov_new as large as your k loop limit.
Stephen23
Stephen23 2018-5-20
The code is badly aligned. Badly aligned code is one way that beginners hide bugs in their code. You should use the default code indentation of the MATLAB editor. You can indent it by selecting all of the code and the pressing ctrl+i. Then it will look something like this:
%The first step is to analyze the video and translate it into a structure in the matlab workspace.
T = 30000; u = 1;
mov_new(1).cdata = mov(1).cdata; %initialize the first element of the new structure
for k = 2:length(mov)
if sum(sum(sum(mov(k).cdata - mov(k - 1).cdata))) > T
u = u + 1;
mov_new(u).cdata = mov(k).cdata;
end;
end;
A = mov_new;
max = 0;
conx = 0;
cony = 0;
v = 0;
%
DD = zeros(1995, 3);
for k = 1:1995
for i = 1:336
for j = 1:432
if (i < 4 - [size((A(k).cdata), 1)]) && (j < 4 - [size((A(k).cdata), 2)]);
v = A(k).cdata(i, j) + A(k).cdata(i + 1, j) + A(k).cdata(i, j + 1) + A(k).cdata(i + 1, j + 1) + A(k).cdata(i + 2, j) + A(k).cdata(i + 2, j + 1) + A(k).cdata(i, j + 2) + A(k).cdata(i + 1, j + 2) + A(k).cdata(i + 2, j + 2) + A(k).cdata(i, j + 3) + A(k).cdata(i + 1, j + 3) + A(k).cdata(i + 2, j + 3) + A(k).cdata(i + 3, j + 3) + A(k).cdata(1 + 3, j) + A(k).cdata(1 + 3, j + 1) + A(k).cdata(1 + 3, j + 2);
else
v = A(k).cdata(i, j);
end;
if v > max
max = v;
conx = i;
cony = j;
end;
end;
end;
DD(k, 1) = max;
DD(k, 2) = conx;
DD(k, 3) = cony;
max = 0;
end

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-5-20
The basic problem is that you assume that structure A has atleast 1995 elements, but there is nothing in your code to ensure that this is the case. At the most extreme if the differences between all adjacent frames are less than T then mov_new will have just one element, and so will A. How do you expect to get the 2nd, 3rd, ... 1995th element of a one element structure?
One solution would be to adjust the number of iterations to the size of A.

Community Treasure Hunt

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

Start Hunting!

Translated by