我必须在二进制iamge中找到每个通道的中点,我写了一个代码,但这太长了,当我改变道路的图片时它会给出错误。我必须保存每个车道的中点,然后通过找到斜率和截距,我必须在该二进制图像上绘制线条。 这是代码
x=imread('C:\users\guest\documents\matlab\1.png');
[q,r]= size(x);
n4=zeros(q,r);
midpoint= zeros (720,2); % Array to store midlle points of road lane.
% finding mid points of both lanes.
for n3=540:720
s=x(n3,:);
startIndex =1;
lastIndex =1280;
pixelsRow =s;
FirstWhiteStart=0; FirstWhiteEnd=0; SecondWhiteStart=0; SecondWhiteEnd=0;
for k=1:1280
if (pixelsRow(k) == 1)&&(FirstWhiteStart == 0)
FirstWhiteStart =k;
elseif (pixelsRow(k)==0)&&(FirstWhiteStart>0)&&(FirstWhiteEnd==0)
FirstWhiteEnd=k-1;
elseif (pixelsRow(k)== 1)&&(FirstWhiteEnd>0)&&(SecondWhiteStart==0)
SecondWhiteStart=k;
elseif (pixelsRow(k)==0)&&(SecondWhiteStart>0)&&(SecondWhiteEnd==0)
SecondWhiteEnd=k-1;
end
end
m1=(FirstWhiteStart + FirstWhiteEnd)./2; % first lanes middle point
m1r = round(m1);
if (m1r <= 1)
mp= sub2ind(size(midpoint),n3,1);
midpoint(mp) = 0;
elseif (m1r > 1)
indices = sub2ind(size(n4),n3,m1r);
n4(indices) = 1;
if (m1r >=640)
mp= sub2ind(size(midpoint),n3,2);
midpoint(mp) = m1r;
elseif (m1r <= 640)
mp= sub2ind(size(midpoint),n3,1);
midpoint(mp) = m1r;
end
end
m2=(SecondWhiteStart + SecondWhiteEnd+1)./2; % second lane middle point.
m2r = round(m2);
if (m2r <= 1)
indices = sub2ind(size(n4),n3,m2r);
n4(indices) = 0;
mp= sub2ind(size(midpoint),n3,1);
midpoint(mp) = 0;
elseif (m2r > 1)
indices = sub2ind(size(n4),n3,m2r);
n4(indices) = 1;
if (m2r >=640)
mp= sub2ind(size(midpoint),n3,2);
midpoint(mp) = m2r;
elseif (m2r <=640)
mp= sub2ind(size(midpoint),n3,1);
midpoint(mp) = m2r;
end
end
end
pairpoints = nchoosek([540:720],2);
var1 = zeros (16290,2); % array to store variables a and b of first lane.
var2 = zeros (16290,2); % array to stote variables a and b of second lane.
% calling middle points previously stored in array,putting in equation.
for n = 1 : 16290
x1 = pairpoints(n,1); %value of frst row
x2 = pairpoints(n,2); %value of 2nd row
y1 = midpoint (pairpoints(n,1), 1); %rows of midpoint matrix are specified from pairpoints location martix
y2 = midpoint (pairpoints(n,2), 1);
z1 = midpoint (pairpoints(n,1), 2);
z2 = midpoint (pairpoints(n,2), 2);
a1 = (y2 - y1) ./ (x2 - x1);
a2 = (z2 - z1) ./ (x2 - x1);
b1=(((x2)*(y1)) - (x1)*(y2)) ./ (x2 - x1);
b2=(((x2)*(z1)) - (x1)*(z2)) ./ (x2 - x1);
% variables a and b of first lane.
line = sub2ind(size(var1),n,1);
var1(line) = a1;
line = sub2ind(size(var1),n,2);
var1(line) = b1;
% variables A and b of second lane.
line = sub2ind(size(var2),n,1);
var2(line) = a2;
line = sub2ind(size(var2),n,2);
var2(line) = b2;
end
v11=round(var1);
v22=round(var2);
% eleminating zeros from array.
[i,j] = find(v11);
a1 = v11(i,1);
a1= a1(a1~=0);
b1 = v11(i,2);
b1= b1(b1~=0);
a11=median(a1)
b11=median(b1)
% eleminating zeros from array.
[k,l] = find(v22);
row = i;
a2 = v22(k,1);
a2= a2(a2~=0);
b2 = v22(k,2);
b2= b2(b2~=0);
a22=median(a2)
b22=median(b2)
%assign variables
lin=zeros(720,2);
% implementation of final line equation.
for x1 = 1:720
% equation becomes (w = eh + f) as actual was (y = ax + b)
y1 = (a11 * x1) + b11;
y2 = (a22 * x1) + b22;
col = sub2ind( size(lin),x1,1); % equation for first lane.
lin(col)= y1;
col = sub2ind( size(lin),x1,2); % equation for second lane.
lin(col)= y2;
end
array=lin;
r= 1:720;
c= 1:1280;
x(r,c)= 0;
imshow(x);
imwrite(x,'a.png');
image =imread('C:\users\guest\documents\matlab\a.png');
for r1 = 1:720
for c = 1:2;
if array(r1,c) < 0;
lin(r1,c) = abs (array(r1,c));
image(r1,lin(r1,c))= 0;
elseif array(r1,c) > 0;
image(r1,lin(r1,c))= 1;
end
end
end
imshow(image)