车牌识别源代码.docx
《车牌识别源代码.docx》由会员分享,可在线阅读,更多相关《车牌识别源代码.docx(40页珍藏版)》请在冰点文库上搜索。
![车牌识别源代码.docx](https://file1.bingdoc.com/fileroot1/2023-8/3/a0b6a422-2317-4320-9ec6-6c33b61dd43a/a0b6a422-2317-4320-9ec6-6c33b61dd43a1.gif)
车牌识别源代码
//---------------------------------------------------------------------------
#include
#include
#pragmahdrstop
#include"Unit1.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm1*Form1;
Graphics:
:
TBitmap*bitmap1;//定义bitmap1和bitmap2是TBitmap
Graphics:
:
TBitmap*bitmap2;
Graphics:
:
TBitmap*bitmap3;
structCOLOUR//定义颜色结构体,有r,g,b(红,绿,蓝)三分量
{
Byter;
Byteg;
Byteb;
};
COLOUR**col;
//------------------------------------------------------------------------------
//彩色图象转灰度图象,col是原图象彩色象素数组,w是图象宽度,h是图象高度
Byte**ColorToGray(COLOUR**col,intw,inth)
{
inti,j,k;
doubley;
Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*));//开辟动态数组result,存放处理结果
for(i=0;iresult[i]=newByte[w];
for(i=0;ifor(j=0;j{
y=0.3*col[i][j].r+0.59*col[i][j].g+0.11*col[i][j].b;//灰度合成
result[i][j]=(Byte)y;
}
returnresult;//返回处理结果
}
//------------------------------------------------------------------------------
//灰度图像均衡化,Image是画均衡化直方图的位置,w是原图像宽度,h是原图像高度
Byte**Equalize(TImage*Image,intw,inth)
{
inti,j,k;
longsum=0;
Byte*ptr,*newscan;
doubleH;//用来计算某灰度值在灰度图中垂直方向的长度
longhgray[256];//定义灰度值的数组
inttt[256];//新直方图灰度级的数组
longtgray[256];//新直方图各灰度级像素的数组
Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*));
for(i=0;iresult[i]=newByte[w];
for(i=0;i<256;i++)//数组初始化
{
hgray[i]=0;
tgray[i]=0;
}
for(i=0;i{
ptr=static_cast(bitmap1->ScanLine[i]);
for(j=0;jhgray[ptr[j*3]]++;
}
//ShowMessage(n);
for(i=0;i<256;i++)
{
sum+=hgray[i];
tt[i]=sum;
}
for(i=0;i<256;i++)
tt[i]=(int)(255*tt[i]/sum+0.5);
for(i=0;i<256;i++)
tgray[tt[i]]+=hgray[i];
for(k=0;k<256;k++)
{
H=int(tgray[k]*5000/(h*w));
Image->Canvas->MoveTo(k,Image->Height);
Image->Canvas->LineTo(k,Image->Height-H);
}
for(i=0;i{
ptr=static_cast(bitmap1->ScanLine[i]);
for(j=0;j{
result[i][j]=(Byte)(tt[ptr[j*3]]);
}
}
returnresult;
}
//---------------------------------------------------------------------------
//3*3中值滤波,image是原图象素数组,w是图象宽度,h是图象高度
Byte**MeanFilter(Byte**image,intw,inth)
{
inti,j,k1,k2,k;
Byteneighbour[9],temp;
Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*));
for(i=0;iresult[i]=newByte[w];
for(i=0;ifor(j=0;jresult[i][j]=image[i][j];
for(i=1;ifor(j=1;j{
k=0;
for(k1=-1;k1<=1;k1++)
for(k2=-1;k2<=1;k2++)
{
neighbour[k]=image[i+k1][j+k2];//记录邻域象素
k++;
}
for(k1=0;k1<8;k1++)//用冒泡法排序
for(k2=0;k2<8-k1;k2++)
{
if(neighbour[k2]>neighbour[k2+1])
{
temp=neighbour[k2];//如果前面比后面大交换,使最大放在后面
neighbour[k2]=neighbour[k2+1];
neighbour[k2+1]=temp;
}
}
result[i][j]=neighbour[4];//取排在中间的值输出
}
returnresult;
}
//-----------------------------------------------------------------------------
//Sobel算子锐化,image是原图象素数组,w是图象宽度,h是图象高度
Byte**Sobel(Byte**image,intw,inth)
{
inti,j,k,temp1,temp2,temp;
inttemplate1[9]={2,1,0,-1,-2,-1,0,1,0};//定义Sobel模板
inttemplate2[9]={0,1,2,1,0,-1,-2,-1,0};
intneighbourx[9]={1,1,0,-1,-1,-1,0,1,0};//定义3*3邻域(用链码)
intneighboury[9]={0,-1,-1,-1,0,1,1,1,0};
Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*));
for(i=0;iresult[i]=newByte[w];
for(i=0;ifor(j=0;jresult[i][j]=0;
for(i=1;ifor(j=1;j{
temp1=temp2=0;//初始化为0
for(k=0;k<9;k++)
{
temp1=temp1+image[i+neighboury[k]][j+neighbourx[k]]*template1[k];//求加权和
temp2=temp2+image[i+neighboury[k]][j+neighbourx[k]]*template2[k];
}
//temp=(int)(sqrt(temp1*temp1+temp2*temp2+0.5));//取平方和根号输出
temp=(int)(abs(temp1));
if(temp>255)//值不能超过255
temp=255;
//if(temp<0)
//temp=0;
result[i][j]=(Byte)temp;
}
returnresult;
}
//-----------------------------------------------------------------------------
//二值化求阈值
Bytethreshold(Byte**image,intw,inth)
{
ByteTh,Thnew;
inti,j;
longhisnum[256],SumLeft,SumRight;
doubleavg1,avg2,temp=0.0;
//w=right-left;
//h=bottom-top;
for(i=0;i<=255;i++)
hisnum[i]=0;
for(i=0;ifor(j=0;jhisnum[image[i][j]]++;
for(i=0;i<=255;i++)
{
temp=temp+i*hisnum[i]*1.0;
}
Th=(Byte)(temp*1.0/(w*h));
Thnew=Th;
//ShowMessage(Th);
do
{
Th=Thnew;
avg1=0;avg2=0;SumRight=0;SumLeft=0;
for(i=0;i
{ avg1+=hisnum[i]*i;//求阈值左半部分的期望 SumLeft+=hisnum[i]; } avg1=avg1/SumLeft; for(i=Th;i<256;i++) { avg2+=hisnum[i]*i;//求阈值右半部分的期望 SumRight+=hisnum[i]; } avg2=avg2/SumRight; //ShowMessage(avg1); //ShowMessage(avg2); Thnew=(avg1+avg2)/2;//根据两部分的期望再确定新阈值 }while(Thnew! =Th);//反复迭代,直到两次结果相等 returnTh; } //----------------------------------------------------------------------------- //二值化,image是原图象素数组,w是图象宽度,h是图象高度,th为阈值 Byte**Binarization(Byte**image,intw,inth,Byteth) { inti,j; Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*)); for(i=0;iresult[i]=newByte[w]; for(i=0;ifor(j=0;j{ if(image[i][j] result[i][j]=0; else result[i][j]=255;//否则输出255(白色象素,代表目标) } returnresult; } //------------------------------------------------------------------------------ //腐蚀,image是原图象素数组,w是图象宽度,h是图象高度 Byte**Erosion(Byte**image,intw,inth) { inti,j,k,temp1,temp2,temp,n,m; intneighbourx[2]={0,0}; intneighboury[2]={1,0}; Byte**result=(Byte**)malloc((unsigned)h*sizeof(Byte*)); for(i=0;iresult[i]=newByte[w]; for(i=0;ifor(j=0;jresult[i][j]=255; for(i=1;ifor(j=1;j{ for(k=0;k<2;k++) { if(image[i+neighboury[k]][j+neighbourx[k]]==0)//如果模板的所有元素与白色象素(用255表示)重合,该位置输出255,否则为0 { result[i][j]=0; break; } } if(k==2) result[i][j]=255; } returnresult; } //--------------------------------------------------------------------------- __fastcallTForm1: : TForm1(TComponent*Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void__fastcallTForm1: : Button1Click(TObject*Sender) { bitmap1=newGraphics: : TBitmap;//定义bitmap1,bitmap2 bitmap2=newGraphics: : TBitmap; bitmap3=newGraphics: : TBitmap; if(OpenPictureDialog1->Execute()) { StringFileName=OpenPictureDialog1->FileName;//打开文件 Image1->Picture->LoadFromFile(FileName); bitmap1->Assign(Image1->Picture->Graphic);//初始化bitmap1,bitmap2,使它指向Image1内容 bitmap2->Assign(Image1->Picture->Graphic); bitmap3->Assign(Image1->Picture->Graphic); bitmap1->PixelFormat=pf24bit; bitmap2->PixelFormat=pf24bit; bitmap3->PixelFormat=pf24bit; } } //--------------------------------------------------------------------------- void__fastcallTForm1: : Button2Click(TObject*Sender) { inti,j,k; COLOUR**col; Byte*ptr,*newscan; doubleH;//用来计算某灰度值在灰度图中垂直方向的长度 longhgray[256];//定义灰度值的数组 intw=bitmap1->Width;//取位图宽和高 inth=bitmap1->Height; col=(COLOUR**)malloc((unsigned)h*sizeof(COLOUR*)); for(i=0;icol[i]=newCOLOUR[w]; for(i=0;i{ ptr=static_cast(bitmap1->ScanLine[i]);//用ScanLine加快速度,下同 for(j=0;j{ col[i][j].b=ptr[j*3];//按蓝\绿\红象素顺序读取象素信息 col[i][j].g=ptr[j*3+1]; col[i][j].r=ptr[j*3+2]; } } Byte**y=ColorToGray(col,w,h);//调用彩色转灰度函数 for(i=0;i{ newscan=static_cast(bitmap2->ScanLine[i]);//用ScanLine加快速度,下同 for(j=0;j{ newscan[j*3]=y[i][j];//将输出结果保存在newscan newscan[j*3+1]=y[i][j]; newscan[j*3+2]=y[i][j];//红、绿、蓝都取亮度信号,变灰度图象 } } Image1->Picture->Assign(bitmap2);//显示结果 bitmap1->Assign(bitmap2); for(i=0;i<256;i++)//灰度值数组初始化 hgray[i]=0; Image2->Canvas->Brush->Color=clWhite; Image2->Canvas->FillRect(Rect(0,0,Image2->Width,Image2->Height)); for(i=0;i{ ptr=static_cast(bitmap1->ScanLine[i]); for(j=0;jhgray[ptr[j*3]]++; } for(k=0;k<256;k++)//画出灰度直方图 { //Image2->Canvas->Pen->Color=50; H=int(hgray[k]*5000/(h*w)); Image2->Canvas->MoveTo(k,Image2->Height); Image2->Canvas->LineTo(k,Image2->Height-H); } } //--------------------------------------------------------------------------- void__fastcallTForm1: : FormShow(TObject*Sender) { Image2->Parent->DoubleBuffered=true; Image2->Canvas->Brush->Color=clWhite; Image2->Canvas->FillRect(Rect(0,0,Image2->Width,Image2->Height)); Image2->Parent->DoubleBuffered=true; for(intk=0;k<256;k++)//画出黑白颜色渐变条 { Image5->Canvas->Pen->Color=RGB(k,k,k); Image5->Canvas->MoveTo(k,0); Image5->Canvas->LineTo(k,Image2->Height); } } //--------------------------------------------------------------------------- void__fastcallTForm1: : Button4Click(TObject*Sender) { inti,j,k; Byte*ptr,*newscan; intw=bitmap1->Width;//取位图宽和高 inth=bitmap1->Height; Image2->Canvas->Brush->Color=clWhite; Image2->Canvas->FillRect(Rect(0,0,Image2->Width,Image2->Height)); //ShowMessage(n); Byte**y=Equalize(Image2,w,h); for(i=0;i{ newscan=static_cast(bitmap2->ScanLine[i]); for(j=0;j{ newscan[j*3]=y[i][j];//将输出结果保存在newscan newscan[j*3+1]=y[i][j]; newscan[j*3+2]=y[i][j]; } } Image1->Picture->Assign(bitmap2);//显示结果 bitmap1->Assign(bitmap2); } //--------------------------------------------------------------------------- void__fastcallTForm1: : Button5Click(TObject*Sender) { inti,j; intw=bitmap1->Width; inth=bitmap1->Height; Byte**image=(Byte**)malloc((unsigned)h*sizeof(Byte*)); Byte**result; for(i=0;iimage[i]=newByte[w]; Byte*ptr,*newscan; for
展开阅读全文
相关搜索
资源标签
| |