Azzera filtri
Azzera filtri

How to correct this code?

2 visualizzazioni (ultimi 30 giorni)
luisa bertoli
luisa bertoli il 20 Mag 2018
Risposto: Stephen23 il 20 Mag 2018
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 Commenti
Walter Roberson
Walter Roberson il 20 Mag 2018
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 il 20 Mag 2018
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

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 20 Mag 2018
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