my code continuously showing undefined variable or function
2 次查看(过去 30 天)
显示 更早的评论
hello everyone. I am trying to run a code but it's giving me error saying 'undefined variable or function X1' and sometimes it says 'undefined variable and function X2'. i am attaching the code below. Please help me on this.
clc;clear all;close all
s=600;
V = VideoReader('2.mp4');
Is=read(V,[1 s]);
r=240;c=320;
r1=round(r*0.4);r2=round(r*0.94);
ch=160;
ri=r2-r1;
for i=1:2:s
tic
C=Is(:,:,:,i);
CI=C(r1:r2,:,:);%»®·Ö%»®·Ö
G=rgb2gray(CI);%±ä»Ò
%%Ñ°ÕÒãÐÖµ
g=G(:);
mu=mean(g);
g=single(g);sige=std(g);%±ê×¼²î
%ãÐÖµ
T=mu+3*sige;
%Ä£°åB
B=G>T;
t=0;
for u=1:ri
for v=ch:-1:1
if B(u,v)
t=t+1;
Y1=u+r1;
X1=v;
break
end
end
end
t=0;
for u=1:ri
for v=ch:c
if B(u,v)
t=t+1;
Y2=u+r1;
X2=v;
break
end
end
end
p1=polyfit(X1,Y1,1);
p2=polyfit(X2,Y2,1);
x1=1:10:ch;x2=ch:10:c;
F1=x1.*p1(1)+p1(2);
F2=x2.*p2(1)+p2(2);
toc
figure(1),imshow(C),hold on
plot(x1',F1,'g',x2',F2,'g','LineWidth',2)
title(i)
hold off
mov(i)=getframe(gca);
end
movie2avi(mov,'F1´¦Àíºó');
0 个评论
采纳的回答
Rik
2018-10-20
编辑:Rik
2018-10-20
There are two main problems with the loops you have set up: you run the risk of not defining your variables (which is causing your errors) and they are very inefficient.
Replace this
t=0;
for u=1:ri
for v=ch:-1:1
if B(u,v)
t=t+1;
Y1=u+r1;
X1=v;
break
end
end
end
t=0;
for u=1:ri
for v=ch:c
if B(u,v)
t=t+1;
Y2=u+r1;
X2=v;
break
end
end
end
with this
[u,v]=deal(1:r1,ch:-1:1);
[u_ind,v_ind]=find(B(u,v))
if ~isempty(u_ind)
[u_ind,v_ind]=deal(u_ind(end),v_ind(end));
Y1=u(u_ind)+r1;
X1=v(v_ind);
else
%prevent Y1 from not being defined
Y1=0;X1=0;
end
[u,v]=deal(1:r1,ch:c);
[u_ind,v_ind]=find(B(u,v))
if ~isempty(u_ind)
[u_ind,v_ind]=deal(u_ind(end),v_ind(end));
Y2=u(u_ind)+r1;
X2=v(v_ind);
else
%prevent Y2 from not being defined
Y2=0;X2=0;
end
5 个评论
Rik
2018-10-21
My updated code has the same functionality as your code, except for the case where they would be undefined. You will have to think about what value they should have in case the code that replaced the double loop fails.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!