在好例子网,分享、交流、成长!
您当前所在位置:首页Others 开发实例一般编程问题 → matlab 车道线检测

matlab 车道线检测

一般编程问题

下载此实例
  • 开发语言:Others
  • 实例大小:0.05M
  • 下载次数:65
  • 浏览次数:1388
  • 发布时间:2019-03-30
  • 实例类别:一般编程问题
  • 发 布 人:crazycode
  • 文件格式:.doc
  • 所需积分:2
 相关标签: 检测 MATLAB

实例介绍

【实例简介】
【实例截图】

【核心代码】

clc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%读图最后得到灰度图像rg
A=imread('新4.bmp');
[r c d]=size(A);
r2g=zeros(r,c);
red=zeros(r,c);
green=zeros(r,c);
blue=zeros(r,c);
rg=zeros(r,c);
for i=1:r;
    for j=1:c;
      red(i,j)=A(i,j,1);%提取图像的红色分量   
      green(i,j)=A(i,j,2);%提取图像的绿色分量
      blue(i,j)=A(i,j,3);%提取图像的蓝色分量
    end
end
for i=1:r;
    for j=1:c;
        rg(i,j)=0.5*red(i,j) 0.5*green(i,j);
       
    end
end
rg=uint8(rg);
 for i=1:r;
    for j=1:c;
        if rg(i,j)>178;
            rg(i,j)=255;
        end
    end
end
figure;
subplot(2,2,1);imshow(A);title('原图')% 显示原图像
subplot(222);imshow(rg);title('彩色通道提取法-灰度图');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
r2g=rg;
i=r2g;%输入灰度变换后的图像
subplot(221);imshow(i);title('原图')
subplot(223);imhist(i);%显示直方图
h1=histeq(i);
subplot(222);imshow(h1);title('直方图均衡化后的图')
subplot(224);imhist(h1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
i=h1;%直方图均衡化后的图像
j=imnoise(i,'salt & pepper',0.02)
k1=medfilt2(j);
figure;
subplot(121);imshow(j);title('添加椒盐噪声图像')
subplot(122);imshow(k1);title('3*3模板中值滤波')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc
r2g;
figure;
subplot(221);imshow(r2g);
title('原图像');
W_H1=[2 3 0;%选用自定义差分模板增强左车道标记线
      3 0 -3;
         0 -3 -2];    
W_V1=[ 0 3 2;%选用自定义差分模板增强右车道标记线
       -3 0 3;
       -2 -3 0];
T = 0.28;                           % the threshold in the 2-value
I = r2g;  % read the image
[height,width] = size(I);
I1 = double(I);
I3 = double(I);
I4 = double(I);
I2 = zeros(height 2,width 2);      % put the image's data into a bigger array to void the edge
I2(2:height 1,2:width 1) = I1;
for i=2:height 1                   % move the window and calculate the grads
    for j=2:width 1
        sum3 = 0;                  % 不同方向的模板算子
        sum4 = 0;
        for m=-1:1
            for n=-1:1
             sum3= sum3   W_H1(m   2,n   2) * I2(i   m,j   n);
            end
        end
           for m=-1:1
            for n=-1:1           
                sum4 = sum4   W_V1(m   2,n   2) * I2(i   m,j   n);
            end
           end        
        grey1 = abs(sum3)   abs(sum4);       
        I3(i-1,j-1) = grey1;
    end
end
big = max(max(I3));                          % 归一化
small = min(min(I3));
for i=1:height
    for j=1:width
        I3(i,j) = (I3(i,j)-small)/(big - small);  % 归一化
        if(I3(i,j) > T)
            I3(i,j) = 1;                          % 二值化
        else
            I3(i,j) = 0;
        end
    end
end 
subplot(222);
imshow(I3);title('sl、sr算子处理的图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;subplot(221);imshow(A);title('原图')
gg=bwmorph(I3,'thin',inf);
subplot(222);imshow(gg);title('细化的图像')
I = rg;
[x,y]=size(I);
[height,width] = size(I);
seedx=round(x);
seedy=round(y/2);
gr=I(seedx,seedy)
W_H = [ 1   1  1;                  % the model in the horizon direction
        1   1  1;
       1  1  1];
I1 = double(I);
I2 = zeros(height 2,width 2);      % put the image's data into a bigger array to void the edge
I2(2:height 1,2:width 1) = I1;
for i=2:height 1                   % move the window and calculate the grads
    for j=2:width 1
        sum1 = 0;                  % the cumulus 
        for m=-1:1
            for n=-1:1
                sum1 = sum1   W_H(m   2,n   2) * I2(i   m,j   n);
            end
        end
      grey=sum1/9;
      I1(i-1,j-1) = grey;
    end
end
I1=uint8(I1);%邻域平均化灰度图像
%subplot(222);imshow(I1);title('区域生长-路面区域图像')
[x,y]=size(I1);
I2=zeros(x,y);
I=double(I);
I1=double(I1);
for  i=1:x;
    for j=1:y;
        if abs(I1(i,j)-I(i,j))<=70&abs(I(seedx,seedy)-I1(i,j)<=90)
            I2(i,j)=1;
        end
    end
end
subplot(223)
imshow(I2);title('区域生长-路面区域图像')
I4=zeros(x,y);
for i=round(5):x-4;
    for j=5:y-4;
        if gg(i,j)==1
            for m=i-4:i 4;
                for n=j-4:j 4;
                    if I2(m,n)==0&sqrt((i-m)^2 (j-n^2))<=2
            I4(i,j)=1;
        end
    end
end
end
end
end
subplot(224)
imshow(I4);title('检测图像')
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
t0=clock
ff=I4;%输入检测的图像
[x,y]=size(ff);
a1=zeros(x,1);
b1=zeros(y,1);
k=1;
for i=1:x
    for j=1:round(y/2);
        if  ff(i,j)==1;
            a1(k)=i;
            b1(k)=j;
            k=k 1;
        end
    end
end
m=length(a1);
a2=max(a1)
h=1;
for i=1:m;
    if a1(i)==a2;
        jiaobiao(h)=i;
        h=h 1;
    end
end
b1=b1(jiaobiao);
b11=max(b1);
%ff(a1,b1)为选中的车道线第一个像素点
k=1;
for  i=round(1):round(x);
    for j=1:round(y/2 );
        if ff(i,j)==1&i~=a2&j~=b11;
            kkb(k)=(b11-j)/(a2-i);
            bbc(k)=b11-kkb(k)*a2;
           k=k 1;
        end
    end
end 
 theta=atan(-1./kkb);
 theta1= theta pi,
 roi=bbc.*sin(theta);
 roi1= roi abs(roi);
 maxtheta=max( theta1);
 maxroi=max(roi1);
 accum=zeros(round(maxtheta) 1,round( maxroi) 1);
 for  i=1:length(theta);
     thetaint=round( maxtheta/2 theta1(i)/2);
     roiint=round( maxroi/2 roi1(i)/2) 1;
     accum(thetaint,roiint)=accum( thetaint,roiint) 1;
 end   
   p=max(max(accum))%出现峰值处的累加器的值
for  i=1:length(theta);
      thetaint=round( maxtheta/2 theta1(i)/2);
     roiint=round( maxroi/2 roi1(i)/2) 1;
      if  accum(thetaint,roiint)==p;
          ji=i;
      end
end 
k=1;
m=1;
for  i=round(x/2):x;
    for j=1:round(y/2);
        if ff(i,j)==1&i~=a2&j~=b11;
            kk(k)=(b11-j)/(a2-i);
            bb(k)=b11-kk(k)*a2;
           theta(k)=atan(-1./kk(k));
         if theta(k) ==theta(ji) ;
             xji(m)=i;
             yji(m)=j;
             m=m 1;
         end
        k=k 1;
        end
    end
end 
%xji=median(xji);
%yji=median(yji);
ji;
theta(ji);
imshow(I4);hold  on;
line( [yji,b11] ,[xji,a2],'linewidth',3);title('根据改进的hough做标记线')
time = etime(clock, t0)




标签: 检测 MATLAB

网友评论

第 1 楼 18734841138 发表于: 2019-05-10 09:23 59
未定义函数或变量 'yji'。 出错 shiyan1 (line 243) line( [yji,b11] ,[xji,a2],'linewidth',3);title('根据改进的hough做标记线') >> 这个错误怎么改

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警