my code continuously showing undefined variable or function

조회 수: 4 (최근 30일)
Shahrin Islam
Shahrin Islam 2018년 10월 20일
댓글: Shahrin Islam 2018년 10월 22일
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´¦Àíºó');

채택된 답변

Rik
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
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.
Shahrin Islam
Shahrin Islam 2018년 10월 22일
thank you..i will try to fix that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by