飞思卡尔智能车摄像头组黑线提取
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
unsigned char i=0,j=0,left=0,right=0,start_flag1=0,start_flag2=0;
unsigned char left_line=0,right_line=0,left_edge=0,right_edge=0;
int temp=0;
unsigned char left_flag1=0,right_flag1=0,left_flag2=0,right_flag2=0;
{
if(abs_1(Line_Center[i-1]-Line_Center[i])>5 ) //黑线是连续的值还需要确认即相差多少就认为是错误
{
temp=2*Line_Center[i-1]-Line_Center[i-2];
if(temp>RIHGT_LIMIT)temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT)temp=LEFT_LIMIT;
Line_Center[i]=temp;
}
}
//黑线的中值滤波程序!
for(i=1;i<ROW-1;i++)
{temp=get_mid(Line_Center[i-1],Line_Center[i],Line_Center[i+1]);
/////图像后部分用边缘检测法
///////////////////////////////////////////////////
for(i=ROW_MID;i<ROW;i++)
{
left=0; //左右跳变清零,这是必须的!!
right=0;
for(j=10;j<=COLUMN-10;j++) //两边搜索最边界干扰略去
Line_Center[0]=Line_Center[ROW-1];
////////////////////////////////////////////////////
/////图像前部分用边缘检测法
///////////////////////////////////////////////////
void SCI_Init()
{
SCI0BD=312;
SCI0CR1=0;
SCI0CR2=0X2C;
}
//---------------写SCI数据-------------------//
void SCI_Write(unsigned char sendchar)
{
while (!(SCI0SR1&0x80));
Line_Center[i]=temp;}
}
void Image_binaryzation(unsigned int row) //二值化程序
{ unsigned char *p_Image;
unsigned char *q_Image;
q_Image=&Buffer1[row][0];
for(p_Image=&Buffer[row][0];p_Image<=&Buffer[row][COLUMN-1];p_Image++)
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 8 Port0_Interrupt()
{
TFLG1_C0F=1;
line++; //行计数器加1
if ( SampleFlag == 0 || line<SAMP_ROW_START || line>SAMP_ROW_MAX )
#define BLACK_LINE_MIN0 4
#define BLACK_LINE_MAX1 12
#define BLACK_LINE_MIN1 2
#define LEFT_LIMIT 1
#define RIHGT_LIMIT 69
#define CENTER 35
unsigned char Buffer[ROW][COLUMN];
unsigned char Buffer1[ROW][COLUMN];
unsigned char SampleFlag=0 ;
unsigned char jiange;
unsigned int m=0;
unsigned int n=0;
unsigned int line=0;
unsigned char Flag=0;
CLKSEL=0x00; //48mhz
PLLCTL_PLLON=1;
SYNR=0XC0 | 0X05;
REFDV=0X80 | 0X01;
POSTDIV=0X00;
_asm(nop);
_asm(nop);
while(0==CRGFLG_LOCK);//锁相环锁定
CLKSEL_PLLSEL=1;//选定外部时钟
unsigned int Line_Center[ROW]={20};
//--------------初始化函数----------------//
/*时钟初始化程序*/
void PLL_Init(void) //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
{ //锁相环时钟=2*16*(2+1)/(1+1)=48MHz
{
Line_Center[i]=(right+left+1)/2;
_asm(nop);
//black_lost=0;//黑线丢失清零
}
}
else /////////////////过宽过细黑线进行插值
{
if(i<3) Line_Center[i]=Line_Center[i-1];
else
{temp=2*Line_Center[i-1]-Line_Center[i-2];
#define SAMP_ROW_MAX 280
#define THRESHOLD 0x20
#define ROW_START 1
#define ROW_MID 10
#define ROW_END 36
#define BLACK 0
#define WHITE 1
#define BLACK_LINE_MAX0 15
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define ROW 36
#define COLUMN 70
#define SAMP_ROW_START 11
for(i=ROW_START;i<ROW_MID;i++)
{
left=0; //左右跳变清零,这是必须的!!
right=0;
for(j=5;j<=COLUMN-5;j++) //两边搜索最边界干扰略去
{
if(Buffer1[i][j]-Buffer1[i][j+1]>0)
{left=j;left_flag1++;}
}
//--------------行场中断初始化-------------//
void IC_Init(void)
{
TIOS=0x00;
TCTL4=0x09;
TSCR1=0x80;
TIE=0x03;
TFLG1=0xFF;
}
void IO_Init(void)
{
DDRA=0X00;
}
//----------初始化SCI0---------------------//
{
if((*p_Image<THRESHOLD)&&(*(p_Image+1)<THRESHOLD))
*(q_Image++)=BLACK;
else
*(q_Image++)=WHITE;
}
}
void chuli(void)
{
unsigned char i,j;
int temp=0;
for(i=0;i<ROW;i++)
{
temp=Line_Center[i];
for(j=0;j<temp;j++)
{
SCI_Write(' ') ;
}
SCI_Write('1');
SCI_Write(0x0D);
SCI_Write(0X0A);
}
Flag=1;
}
void main(void)
{
/* put your own code here */
SCI0DRH=0;
SCI0DRL=sendchar;
}
//黑线的中值滤波
int get_mid(unsigned int a,unsigned int b,unsigned int c)
{
unsigned int x=0;
if(a>b){x=b;b=a;a=x;}
if(b>c){x=c;c=b;b=x;}
Buffer[m][69]=PORTA; Buffer[m][n+69]=PORTA;
m++;
}
Image_binaryzation(m);
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
if(a>b){x=b;b=a;a=x;}
return b ;
}
//求绝对值
int abs_1(int x)
{
if(x<0)x=-x;
return(x);
}
void black_extract(void)
{
unsigned char black_lost=0;//黑线丢失计数器
unsigned char stop=0; //停车圈数计数器
if((right-left>=BLACK_LINE_MIN1)&&(right-left<=BLACK_LINE_MAX1)) //过滤过宽过细的黑线
{来自百度文库
Line_Center[i]=(right+left+1)/2;
_asm(nop);
//black_lost=0;//黑线丢失清零
}
else /////////////////过宽过细黑线进行插值
{
return;//判断是否从新的一场开始
}
if(line<=132)jiange=12;
else if(line>132&&line<=200) jiange=8;
else if(line>200&&line<=248) jiange=6;
else jiange=4;
if(line%jiange==0)
{
Buffer[m][0]=PORTA; Buffer[m][n+0]=PORTA;
Buffer[m][1]=PORTA; Buffer[m][n+1]=PORTA;
Buffer[m][2]=PORTA; Buffer[m][n+2]=PORTA;
Buffer[m][68]=PORTA; Buffer[m][n+68]=PORTA;
PLL_Init();
IC_Init();
IO_Init();
SCI_Init();
EnableInterrupts;
for(;;)
{
black_extract();
chuli();
// _FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
{
temp=2*Line_Center[i-1]-Line_Center[i-2];
if(temp>RIHGT_LIMIT) temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT) temp=LEFT_LIMIT;
Line_Center[i]=temp;
}
}
for(i=2;i<ROW;i++)
if(Buffer1[i][COLUMN-j]-Buffer1[i][COLUMN-j-1]>0)
{right=COLUMN-j;right_flag1++;}
}
//////////求黑线的中心
if((left_flag1==1) && (right_flag1==1))
{
if((right-left>=BLACK_LINE_MIN0)&&(right-left<=BLACK_LINE_MAX0)) //过滤过宽过细的黑线
if(temp>RIHGT_LIMIT) temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT) temp=LEFT_LIMIT;
Line_Center[i]=temp;}
}
}
////////////////////////////////////////////////////
{
if(Buffer1[i][j]-Buffer1[i][j+1]>0)
{left=j;left_line=left;}
if(Buffer1[i][COLUMN-j]-Buffer1[i][COLUMN-j-1]>0)
{right=COLUMN-j;right_line=right;}
}
//////////求黑线的中心
unsigned char left_line=0,right_line=0,left_edge=0,right_edge=0;
int temp=0;
unsigned char left_flag1=0,right_flag1=0,left_flag2=0,right_flag2=0;
{
if(abs_1(Line_Center[i-1]-Line_Center[i])>5 ) //黑线是连续的值还需要确认即相差多少就认为是错误
{
temp=2*Line_Center[i-1]-Line_Center[i-2];
if(temp>RIHGT_LIMIT)temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT)temp=LEFT_LIMIT;
Line_Center[i]=temp;
}
}
//黑线的中值滤波程序!
for(i=1;i<ROW-1;i++)
{temp=get_mid(Line_Center[i-1],Line_Center[i],Line_Center[i+1]);
/////图像后部分用边缘检测法
///////////////////////////////////////////////////
for(i=ROW_MID;i<ROW;i++)
{
left=0; //左右跳变清零,这是必须的!!
right=0;
for(j=10;j<=COLUMN-10;j++) //两边搜索最边界干扰略去
Line_Center[0]=Line_Center[ROW-1];
////////////////////////////////////////////////////
/////图像前部分用边缘检测法
///////////////////////////////////////////////////
void SCI_Init()
{
SCI0BD=312;
SCI0CR1=0;
SCI0CR2=0X2C;
}
//---------------写SCI数据-------------------//
void SCI_Write(unsigned char sendchar)
{
while (!(SCI0SR1&0x80));
Line_Center[i]=temp;}
}
void Image_binaryzation(unsigned int row) //二值化程序
{ unsigned char *p_Image;
unsigned char *q_Image;
q_Image=&Buffer1[row][0];
for(p_Image=&Buffer[row][0];p_Image<=&Buffer[row][COLUMN-1];p_Image++)
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 8 Port0_Interrupt()
{
TFLG1_C0F=1;
line++; //行计数器加1
if ( SampleFlag == 0 || line<SAMP_ROW_START || line>SAMP_ROW_MAX )
#define BLACK_LINE_MIN0 4
#define BLACK_LINE_MAX1 12
#define BLACK_LINE_MIN1 2
#define LEFT_LIMIT 1
#define RIHGT_LIMIT 69
#define CENTER 35
unsigned char Buffer[ROW][COLUMN];
unsigned char Buffer1[ROW][COLUMN];
unsigned char SampleFlag=0 ;
unsigned char jiange;
unsigned int m=0;
unsigned int n=0;
unsigned int line=0;
unsigned char Flag=0;
CLKSEL=0x00; //48mhz
PLLCTL_PLLON=1;
SYNR=0XC0 | 0X05;
REFDV=0X80 | 0X01;
POSTDIV=0X00;
_asm(nop);
_asm(nop);
while(0==CRGFLG_LOCK);//锁相环锁定
CLKSEL_PLLSEL=1;//选定外部时钟
unsigned int Line_Center[ROW]={20};
//--------------初始化函数----------------//
/*时钟初始化程序*/
void PLL_Init(void) //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
{ //锁相环时钟=2*16*(2+1)/(1+1)=48MHz
{
Line_Center[i]=(right+left+1)/2;
_asm(nop);
//black_lost=0;//黑线丢失清零
}
}
else /////////////////过宽过细黑线进行插值
{
if(i<3) Line_Center[i]=Line_Center[i-1];
else
{temp=2*Line_Center[i-1]-Line_Center[i-2];
#define SAMP_ROW_MAX 280
#define THRESHOLD 0x20
#define ROW_START 1
#define ROW_MID 10
#define ROW_END 36
#define BLACK 0
#define WHITE 1
#define BLACK_LINE_MAX0 15
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define ROW 36
#define COLUMN 70
#define SAMP_ROW_START 11
for(i=ROW_START;i<ROW_MID;i++)
{
left=0; //左右跳变清零,这是必须的!!
right=0;
for(j=5;j<=COLUMN-5;j++) //两边搜索最边界干扰略去
{
if(Buffer1[i][j]-Buffer1[i][j+1]>0)
{left=j;left_flag1++;}
}
//--------------行场中断初始化-------------//
void IC_Init(void)
{
TIOS=0x00;
TCTL4=0x09;
TSCR1=0x80;
TIE=0x03;
TFLG1=0xFF;
}
void IO_Init(void)
{
DDRA=0X00;
}
//----------初始化SCI0---------------------//
{
if((*p_Image<THRESHOLD)&&(*(p_Image+1)<THRESHOLD))
*(q_Image++)=BLACK;
else
*(q_Image++)=WHITE;
}
}
void chuli(void)
{
unsigned char i,j;
int temp=0;
for(i=0;i<ROW;i++)
{
temp=Line_Center[i];
for(j=0;j<temp;j++)
{
SCI_Write(' ') ;
}
SCI_Write('1');
SCI_Write(0x0D);
SCI_Write(0X0A);
}
Flag=1;
}
void main(void)
{
/* put your own code here */
SCI0DRH=0;
SCI0DRL=sendchar;
}
//黑线的中值滤波
int get_mid(unsigned int a,unsigned int b,unsigned int c)
{
unsigned int x=0;
if(a>b){x=b;b=a;a=x;}
if(b>c){x=c;c=b;b=x;}
Buffer[m][69]=PORTA; Buffer[m][n+69]=PORTA;
m++;
}
Image_binaryzation(m);
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
if(a>b){x=b;b=a;a=x;}
return b ;
}
//求绝对值
int abs_1(int x)
{
if(x<0)x=-x;
return(x);
}
void black_extract(void)
{
unsigned char black_lost=0;//黑线丢失计数器
unsigned char stop=0; //停车圈数计数器
if((right-left>=BLACK_LINE_MIN1)&&(right-left<=BLACK_LINE_MAX1)) //过滤过宽过细的黑线
{来自百度文库
Line_Center[i]=(right+left+1)/2;
_asm(nop);
//black_lost=0;//黑线丢失清零
}
else /////////////////过宽过细黑线进行插值
{
return;//判断是否从新的一场开始
}
if(line<=132)jiange=12;
else if(line>132&&line<=200) jiange=8;
else if(line>200&&line<=248) jiange=6;
else jiange=4;
if(line%jiange==0)
{
Buffer[m][0]=PORTA; Buffer[m][n+0]=PORTA;
Buffer[m][1]=PORTA; Buffer[m][n+1]=PORTA;
Buffer[m][2]=PORTA; Buffer[m][n+2]=PORTA;
Buffer[m][68]=PORTA; Buffer[m][n+68]=PORTA;
PLL_Init();
IC_Init();
IO_Init();
SCI_Init();
EnableInterrupts;
for(;;)
{
black_extract();
chuli();
// _FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
{
temp=2*Line_Center[i-1]-Line_Center[i-2];
if(temp>RIHGT_LIMIT) temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT) temp=LEFT_LIMIT;
Line_Center[i]=temp;
}
}
for(i=2;i<ROW;i++)
if(Buffer1[i][COLUMN-j]-Buffer1[i][COLUMN-j-1]>0)
{right=COLUMN-j;right_flag1++;}
}
//////////求黑线的中心
if((left_flag1==1) && (right_flag1==1))
{
if((right-left>=BLACK_LINE_MIN0)&&(right-left<=BLACK_LINE_MAX0)) //过滤过宽过细的黑线
if(temp>RIHGT_LIMIT) temp=RIHGT_LIMIT;
else if(temp<LEFT_LIMIT) temp=LEFT_LIMIT;
Line_Center[i]=temp;}
}
}
////////////////////////////////////////////////////
{
if(Buffer1[i][j]-Buffer1[i][j+1]>0)
{left=j;left_line=left;}
if(Buffer1[i][COLUMN-j]-Buffer1[i][COLUMN-j-1]>0)
{right=COLUMN-j;right_line=right;}
}
//////////求黑线的中心