my code continuously showing undefined variable or function

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´¦Àíºó');

 Akzeptierte Antwort

Rik
Rik am 20 Okt. 2018
Bearbeitet: Rik am 20 Okt. 2018
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 Kommentare

Thank you so much for your help. my code is working now. but i am facing another problem. The two lines that are supposed to align with the road marking has shifted from the desired position. I am attaching the video for your convenience. I will highly appreciate you support.
There is a small difference between my code and your loops:
A=false(5,5);
A(2,4)=true;A(4,2)=true;
[u,v]=find(A,1);
That returns u=4,v=2 in my code, and u=2,v=4 in your loops. I'll edit my code to fix this. This difference might be causing your issue. I have no clue what you are trying to do, so I can't help you with other bugs in you code. It also doesn't help that your code has few comments and the comment that are in there are unreadable for me.
Thank you so much for the help. My code is to detect lanes marking in the road. But now it can't detect the lane anymore. The two lines that were about to show the lane have moved. Please help me if you can. Thanks in advance.
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.
thank you..i will try to fix that.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by