📄 main.m
字号:
r=imread('cruve_test2.bmp');
I = rgb2gray(r);
figure,imshow(I),axis on;%原始图像
%hold on;
BBW=edge(I,'sobel');
%figure,imshow(BBW),axis on;%二值图像
J=double(I);
tArray=J(100:110,:);
[rr,cc]=size(tArray);
sum=0;
for(ti=1:rr)
for(tj=1:cc)
sum=sum+tArray(ti,tj);
end
end
T=sum/(rr*cc);
% hold on
% J = double(I);
% t = djxx(J);
% T = double(t/255);
% K = double(J)/255;
[r,c] = size(I);
for(row=1:r)
for(clu=1:c)
if(BBW(row,clu)==0)
BW(row,clu)=0;
else
if(J(row,clu)>T)
BW(row,clu)=1;
else
BW(row,clu)=0;
end
end
end
end
%BW = im2bw(K,T);
BW(1:90,:) = 0;
figure,imshow(BW),axis on;
BW1=BW;
BW(130:r,:)=0;
BWAbove = BW;
BW1(91:125,:)=0;
BWBelow = BW1;
figure,imshow(BWAbove),axis on;
figure,imshow(BWBelow),axis on;
figure,imshow(BBW),axis on;%二值图像
% A = [1 1 1;1 1 1;1 1 1];
% B = convn(A,BW);
% %figure,imshow(B);
%
% [cAbove,dAbove] = find(BWAbove);%b为行数即纵坐标,a为列数即横坐标
% [cBelow,dBelow] = find(BWBelow);
% % % bNum = numel(b);
% % % [d,index] = sort(b);
% % % for(cIndex=1:bNum)
% % % c(cIndex) = a(index(cIndex));
% % % end
tic
[LAbove,aboveLabel]=bwlabel(BWAbove,8);
[LBelow,belowLabel]=bwlabel(BWBelow,8);
stBelow = BelowPolyfit(LBelow,belowLabel);
stAbove = AbovePolyfit(LAbove,aboveLabel);
%
% parentAbove=FindEqu(cAbove,dAbove);%对1像素点归类
% parentBelow=FindEqu(cBelow,dBelow);
% [stBelow,sbNum] = PolyfitBelow(parentBelow,cBelow,dBelow);%拟合同类车道线
% [stAbove,saNum] = PolyfitAbove(parentAbove,cAbove,dAbove);
[arrayBelow,n1,n2] = JoinBelow(stBelow,belowLabel);%连接下半段车道
[arrayAbove,n3,n4] = JoinAbove(stAbove,aboveLabel);%连接上半段车道
toc
% figure,imshow(BWAbove),axis on;
% hold on;
% xMin1=min(arrayAbove(n3).top.x,arrayAbove(n3).low.x);
% xMin2=min(arrayAbove(n4).top.x,arrayAbove(n4).low.x);
% xMax1=max(arrayAbove(n3).top.x,arrayAbove(n3).low.x);
% xMax2=max(arrayAbove(n4).top.x,arrayAbove(n4).low.x);
% x=linspace(xMin1,xMax1,30);
% if(arrayAbove(n3).StyleFlag==1)
% y=arrayAbove(n3).fn(1)*x+arrayAbove(n3).fn(2);
% else
% y=arrayAbove(n3).fn(1)*x.*x+arrayAbove(n3).fn(2)*x+arrayAbove(n3).fn(3);
% end
% plot(x,y,'LineWidth',2,'color','red'),hold on;
% x=linspace(xMin2,xMax2,30);
% if(arrayAbove(n4).StyleFlag==1)
% z=arrayAbove(n4).fn(1)*x+arrayAbove(n4).fn(2);
% else
% z=arrayAbove(n4).fn(1)*x.*x+arrayAbove(n4).fn(2)*x+arrayAbove(n4).fn(3);
% end
% plot(x,z,'LineWidth',2,'color','red');
% hold off;
%
% figure,imshow(BWBelow),axis on,hold on;
%
% xy = [arrayBelow(n1).top.x arrayBelow(n1).top.y;arrayBelow(n1).low.x arrayBelow(n1).low.y];
% plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%
% xy = [arrayBelow(n2).top.x arrayBelow(n2).top.y;arrayBelow(n2).low.x arrayBelow(n2).low.y];
% plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% hold off;
%
figure,imshow(I),axis on;%原始图像
hold on;
[stLine1,stLine2] = LineConect(arrayAbove(n3),arrayAbove(n4),arrayBelow(n1),arrayBelow(n2));
for(stIndex=1:2)
if(stLine1(stIndex).top.x>stLine1(stIndex).low.x)
xMax=stLine1(stIndex).top.x;
xMin=stLine1(stIndex).low.x;
else
xMin=stLine1(stIndex).top.x;
xMax=stLine1(stIndex).low.x;
end
x=linspace(xMin,xMax,100);
if(stLine1(stIndex).StyleFlag==1)
y=stLine1(stIndex).fn(1)*x+stLine1(stIndex).fn(2);
else
y=stLine1(stIndex).fn(1)*x.*x+stLine1(stIndex).fn(2)*x+stLine1(stIndex).fn(3);
end
plot(x,y,'LineWidth',2,'color','black'),hold on;
if(stLine2(stIndex).top.x>stLine2(stIndex).low.x)
xMax2=stLine2(stIndex).top.x;
xMin2=stLine2(stIndex).low.x;
else
xMin2=stLine2(stIndex).top.x;
xMax2=stLine2(stIndex).low.x;
end
x=linspace(xMin2,xMax2,100);
if(stLine2(stIndex).StyleFlag==1)
z=stLine2(stIndex).fn(1)*x+stLine2(stIndex).fn(2);
else
z=stLine2(stIndex).fn(1)*x.*x+stLine2(stIndex).fn(2)*x+stLine2(stIndex).fn(3);
end
plot(x,z,'LineWidth',2,'color','black'),hold on;
end
% xMin1 = min(stLine(1).top.x,stLine(1).low.x);
% xMin2 = min(stLine2.top.x,stLine2.low.x);
% xMax1 = max(stLine1.top.x,stLine1.low.x);
% xMax2 = max(stLine2.top.x,stLine2.low.x);
% x=linspace(xMin1,xMax1,30);
% if(stLine1.StyleFlag==1)
% y=stLine1.fn(1)*x+stLine1.fn(2);
% else
% y=stLine1.fn(1)*x.*x+stLine1.fn(2)*x+stLine1.fn(3);
% end
% plot(x,y,'LineWidth',2,'color','red'),hold on;
% x=linspace(xMin2,xMax2,30);
% if(stLine2.StyleFlag==1)
% z=stLine2.fn(1)*x+stLine2.fn(2);
% else
% z=stLine2.fn(1)*x.*x+stLine2.fn(2)*x+stLine2.fn(3);
% end
% plot(x,z,'LineWidth',2,'color','red');
% hold off;
% figure,imshow(BW),axis on;%二值图像
% figure,imshow(BBW),axis on;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -