二、Linux嵌入式系统代码
参考 https://blog.csdn.net/qq_36449541/article/details/80206477
原先的开发者是在友善之臂的平台上(好像cpu是arm9 S3C2440的)开发的
最原始的车牌识别程序
#include
#include
#include
//#include
#include
#include
#include
//#include "cv.h"
//#include "highgui.h"
#include "math.h"
#define pi 3.14
#define RADIAN(angle) ((angle)*3.14/180.0)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef float FLOAT;
typedef unsigned char byte;
//#define MAX(a,b,c) a>(b>c?b:c)?a:
struct BMP_img
{
WORD bfType;
DWORD size;
DWORD reser;
DWORD header_length;
DWORD infoheader_length;
DWORD width;
DWORD height;
WORD biplanes;
WORD bmp_type; /* 8bit 24bit; */
DWORD compres;
DWORD datasize;
DWORD bixpm;
DWORD biypm;
DWORD clrused;
DWORD relclrused;
BYTE *image;
byte *header_info;
DWORD lineBytes;
};
struct Bmp1{
DWORD width;
DWORD height;
byte strc[67][800];
BYTE *image;
int p1[15];//xl
int p2[15];//xr
int p3[15];//yu
int p4[15];//yd
int up;
int down;
byte strr[7][2500];
byte string[7];
float ang;//倾斜角度
};
struct HSV{
float H;
float S;
int V;
};
struct RGB{
byte bitb;
byte bitg;
byte bitr;
byte re;
};
void read_img(FILE *infile, struct BMP_img *img);
//void read_img(FILE *infile, struct BMP_img *img);
void displaytwo(struct Bmp1 img,byte *srcBmp,byte *dstBmp,int yuzhi);
unsigned char myGetMedianNum(unsigned char * bArray, int iFilterLen);
void cuizhitouying(struct Bmp1 *img,byte *temp);
void sob(byte *srcBmp,int width,int height,int type);
void shuipingtouying(struct Bmp1 *img,byte *temp);
//void display(BMP_img img,byte *temp);
void grayScale(struct BMP_img img,byte *srcBmp,byte *dstBmp);
void junheng(struct BMP_img img,byte *srcBmp,byte *dstBmp);
void CutBmp(struct BMP_img img,struct Bmp1 *img1,int HL,int HH,int VL,int VH);
void CutBmp1(struct Bmp1 *img1,int HL,int HH,int VL,int VH);
void testhsv(float h,float s,float v,byte r,byte g,byte b);
void hsvzation(byte *image,struct HSV *hsv,int width,int height);
void location(byte *image,int width,int height,int yuzhi,int *HL,int *HH,int *VL,int *VH);
void huidu(struct Bmp1 img,byte *srcBmp,byte *dstBmp);
void bmptwo(struct Bmp1 img,byte *srcBmp,byte *dstBmp,byte yuzhi);
void Thiningtest(struct BMP_img img,byte *srcBmp,byte *dstBmp);
void Thining(byte *srcBmp,int width,int height);
void ThinnerRosenfeld(void *image, unsigned long lx, unsigned long ly);
void delpoint(byte *dst,int width,int height,int yuzhi);
void Erosion(byte *image,int width,int height,int type,int num);
void deljunzao(byte *dst,int width,int height,int yuzhi);
void pingjun(byte *dst,int width,int height);
void changeGray(byte *srcBmp,byte *dstBmp,int width,int height,int nWidth,int nHeight);
byte *changeRGB(byte *srcBmp,int width,int height,int *lwidth,int *lheight,float f);
void strBmp(struct Bmp1 *img,byte *temp);
void guiyi(struct Bmp1 *img);
void readstr(FILE *infile, byte *srcBmp);
char *myitoa(int num, char *str, int radix); // 整数字符串转换。
void readmoban(char *path,struct Bmp1 *img2);
void writebmp(const char *path,const char *name,byte *temp);
int cmpstr(byte *src,byte *moban);
void Hough(struct Bmp1 *img);
void strout(struct Bmp1 *img);
void edgesob8(byte *image,int width,int height);
void edgesob4(byte *image,int width,int height);//4邻域
int hough(byte *srcBmp,int width,int height);
void Dilation(byte *image,int width,int height,int type,int num);
void RotateGray(byte *image,int width,int height, float iRotateAngle);
byte *RotateRGB(byte *image, float iRotateAngle,int width,int height,int *lwidth,int *lheight);
void xuanzhuan(struct Bmp1 *img1);
byte *myMalloc(int num,const byte *bmp,int type);
void guiyiRGB(struct Bmp1 *img1);
void outtext(struct Bmp1 img1,int x,int y);
int calstr(byte *srcBmp);
int main()
{
while(1)
{
int HL=0,HH=0,VH=0,VL=0;
struct BMP_img img;
struct Bmp1 img1;
FILE *f;
byte *temp,*temp1;
//char path[80]="C:\\test\\2.bmp";
char path1[80]="C:\\test\\moban\\";
char path[80];
char path3[40];
printf("Please Input Path\n");
scanf("%s",path3);
strcpy(path,path3);
printf("the path is :%s\n",path);
// char path1[80]="//mnt//arm1//test//moban//";
//char path[80]="C:\\Documents and Settings\\liujia\\桌面\\test\\example06.bmp";
//char path1[80]="C:\\Documents and Settings\\liujia\\桌面\\test\\moban\\";
if((f=fopen(path,"rb"))==NULL)
{
printf( "\nCan not open the path: %s \n", path);
exit(-1);
}
read_img(f, &img);
fclose(f);
temp=myMalloc(img.height*img.width*4,temp,0);//(byte *)malloc(sizeof(byte)*img.height*img.width*4);// byte *temp2=(byte *)malloc(sizeof(byte)*img.height*img.width);
temp1=myMalloc(img.height*img.width*4,temp,0);
location(img.image,img.width,img.height,15,&HL,&HH,&VL,&VH);
CutBmp(img,&img1,HL,HH,VL,VH);
Hough(&img1);
xuanzhuan(&img1);
location(img1.image,img1.width,img1.height,0,&HL,&HH,&VL,&VH);
CutBmp1(&img1,HL,HH,VL,VH);
huidu(img1,img1.image,temp);
displaytwo(img1,temp,temp1,30);
delpoint(temp1,img1.width,img1.height,3);
shuipingtouying(&img1,temp1);
delpoint(temp1,img1.width,img1.height,2);
cuizhitouying(&img1,temp1);
memset(temp,0,sizeof(char)*img1.width*img1.height);
strBmp(&img1,temp1);//把车牌字符放到数组里面
guiyi(&img1);
readmoban(path1,&img1);
strout(&img1);
outtext(img1,0,600);
free(temp1);
free(temp);
getch();
}
return 0;
}
void read_img(FILE *infile, struct BMP_img *img)
{
DWORD i,j,l,bitcolor;
DWORD line24;
DWORD line8;
struct RGB *bitmap;
fread(&img->bfType,sizeof(WORD),1,infile);//printf("\n打开的图为 %d",img->bfType);
fread(&img->size,sizeof(DWORD),1,infile); // printf("\nBMP size :%l",img->size);
fread(&img->reser,sizeof(DWORD),1,infile);//printf("\n保留位:");
fread(&img->header_length,sizeof(DWORD),1,infile); //printf("\nheader length :%l",img->header_length);
fread(&img->infoheader_length,sizeof(DWORD),1,infile);
fread(&img->width, sizeof(DWORD), 1, infile);
fread(&img->height, sizeof(DWORD), 1, infile); //printf( "\nwidth :%l\n height :%l ", img->width, img->height);
fread(&img->biplanes, sizeof(WORD), 1, infile);
fread(&img->bmp_type, sizeof(WORD), 1, infile); // printf("\nBMP Tpye :%l ", img->bmp_type);
fread(&img->compres, sizeof(DWORD), 1, infile); //if(img->compres==0) {printf("\nbmp图片为非压缩!");}printf(" ");
fread(&img->datasize, sizeof(DWORD), 1, infile);//printf("\nBMP Data Size :%l ",img->datasize);
fread(&img->bixpm, sizeof(DWORD), 1, infile);
fread(&img->biypm, sizeof(DWORD), 1, infile);
fread(&img->clrused, sizeof(DWORD), 1, infile); //printf("\n实际使用颜色数=%d ",img->clrused);printf(" ");
fread(&img->relclrused, sizeof(DWORD), 1, infile);
img->lineBytes=(img->width*img->bmp_type+31)/32*4;
//printf("\nLineBytes :%l\n",img->lineBytes);
line24=(img->width*24+31)/32*4;
line8=(img->width*8+31)/32*4;
if(img->bmp_type==1){bitcolor=2;printf("不能读取退出");exit(-1);}
if(img->bmp_type==4){bitcolor=16;printf("不能读取退出");exit(-1);}
if(img->bmp_type==8)
{
byte *temp=(BYTE*)malloc(img->height*line8*sizeof(BYTE));
memset(temp,0x00,img->height*img->lineBytes*sizeof(BYTE));
bitcolor=256;
bitmap=(struct RGB *)calloc(bitcolor,sizeof(struct RGB));
img->image=(unsigned char *)malloc(sizeof(unsigned char)*(line8*img->height));
memset(img->image,0x00,sizeof(byte)*line8*img->height);
if(img->image==NULL) {fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");}
fseek(infile,0x36, SEEK_SET);
fread(bitmap,sizeof(struct RGB),bitcolor,infile);
fseek(infile, img->header_length, SEEK_SET);
//fread(temp, sizeof(unsigned char),lineBytes*img->height, infile);
fread(temp, img->lineBytes*img->height,1, infile);
if(temp==NULL)printf("\n读取失败\n");
for(i=0;iheight;i++)
{
for(j=0;jwidth;j++)
{
img->image[i*img->width+j]=(byte)(0.299*bitmap[temp[i*line8+j]].bitb+0.578*bitmap[temp[i*line8+j]].bitg+0.114*bitmap[temp[i*line8+j]].bitr);
// putpixel(j,img->height-i,RGB(img->image[i*img->width+j],img->image[i*img->width+j],img->image[i*img->width+j]));
}
}
free(temp);
temp=NULL;
}
if(img->bmp_type==24)
{
byte *temp=(byte *)malloc(sizeof(byte)*img->height*img->lineBytes); if(temp==NULL)
exit(-1);
img->image=(unsigned char *)malloc(sizeof(unsigned char)*((line24)*img->height));
if(img->image==NULL) fprintf(stderr, "\n Allocation error for temp in read_bmp() \n");
fseek(infile, img->header_length, SEEK_SET);
fread(temp, sizeof(unsigned char), (img->lineBytes)*img->height, infile);
// byte *temp=(byte *)malloc(sizeof(byte)*img->lineBytes*img->height)
for(i=0;iheight;i++)
{
l=0;
for(j=0;jwidth*3;j+=3)
{
//l=(img->height-i-1)*img->lineBytes+j;
l=(img->height-i-1)*img->width*3+j;
img->image[l+2]=*(temp+i*img->lineBytes+j+2);
img->image[l+1]=*(temp+i*img->lineBytes+j+1);
img->image[l]=*(temp+i*img->lineBytes+j);
}
}
free(temp);
temp=NULL;
}
}
void readstr(FILE *infile, byte *srcBmp)
{
int width,height, headlength;
int i,j,bitcolor,line8;
byte *temp;
byte temp1;
struct RGB *bitmap;
width=20;
height=40;
headlength=1078;
line8=(width*8+31)/32*4;
temp=(BYTE*)malloc(height*line8*sizeof(BYTE));
memset(temp,0x00,height*line8*sizeof(BYTE));
bitcolor=256;
bitmap=(struct RGB *)malloc(sizeof(struct RGB)*bitcolor);
fseek(infile,0x36, SEEK_SET);
fread(bitmap,sizeof(struct RGB),bitcolor,infile);
fseek(infile, headlength, SEEK_SET);
fread(temp, line8*height,1, infile);
if(temp==NULL){printf("\n读取失败\n"); exit(-1);}
for(i=0;i=150)
temp1=255;
else temp1=0;
srcBmp[(height-i-1)*width+j]=temp1;//(byte)(0.299*bitmap[temp[i*line8+j]].bitb+0.578*bitmap[temp[i*line8+j]].bitg+0.114*bitmap[temp[i*line8+j]].bitr);
}
}
free(temp);
temp=NULL;
}
void grayScale(struct BMP_img img,byte *srcBmp,byte *dstBmp)
{
int i,j,temp1,n;
float gray;
temp1=img.width*3;
srcBmp=img.image;
for(i=0;i180.0)&&(hsv[i*width+j].V<250)&&(hsv[i*width+j].S>0.6))
{
temp1[i*width+j]=255;
//putpixel(j,i,RGB(255,255,255));
}
else
{
temp1[i*width+j]=0;
//putpixel(j,i,RGB(0,0,0));
}
}
}
for(i=0;imaxnum)
maxnum=temp[i];
}
maxnum=maxnum/3;
for(i=0;imaxnum)
{
*HL=i-yuzhi;
flag=1;
}
}
if(flag==1)
{
if(temp[i]==0)
{
*HH=i+yuzhi;
break;
}
}
}
memset(temp,0x00,sizeof(int)*width);
for(i=0;imaxnum)
maxnum=temp[i];
}
maxnum=maxnum/3;
for(i=0;imaxnum)
{
*VL=i-yuzhi;
break;
}
}
for(i=width;i>*VL;i--)
{
if(temp[i]>maxnum)
{
*VH=i+yuzhi;
break;
}
}
free(temp1);
temp1=NULL;
free(hsv);
}
void Hough(struct Bmp1 *img)
{
int i,j,n;
float ang;
byte *temp1;
//int temp[2000]={0};
struct HSV *hsv=(struct HSV *)malloc(sizeof(struct HSV)*img->width*img->height);
hsvzation(img->image,hsv,img->width,img->height);
temp1=(byte *)malloc(sizeof(byte)*img->height*img->width);
for(i=0;iheight;i++)
{
for(j=0,n=0;jwidth;n+=3,j++)
{
if((hsv[i*img->width+j].H<220.0)&&(hsv[i*img->width+j].H>180.0)&&(hsv[i*img->width+j].V<250)&&(hsv[i*img->width+j].S>0.6))
{
temp1[i*img->width+j]=255;
}
else
{
temp1[i*img->width+j]=0;
}
}
}
Dilation(temp1,img->width,img->height,1,5);
sob(temp1,img->width,img->height,0);
ang=hough(temp1,img->width,img->height);
if((ang<=8)&&(ang>=1))
ang=ang-1;
if(ang>90)
{
ang=ang-180;
}
img->ang=ang;
}
void bmptwo(struct Bmp1 img,byte *srcBmp,byte *dstBmp,byte yuzhi)
{
int i,j;
for(i=0;i=yuzhi)
{
dstBmp[i*img.width+j]=255;
}
else
{
dstBmp[i*img.width+j]=0;
}
}
}
}
void displaytwo(struct Bmp1 img,byte *srcBmp,byte *dstBmp,int yuzhi)
{
//int i,j,T,n,k,t,l;
//int m;
int totalPixels = img.width * img.height;
int bestT = 0;
int i,j;
int histogramArray[256]={0};
double densityArray[256]={0};
double u0 = 0;
double u1 = 0;
double w0 = 0;
double w1 = 0;
double bestDeviation = 0;
for(i=0;i bestDeviation)
{
bestT = i;
bestDeviation = w0 * w1 * (u0 - u1) * (u0 - u1);
}
}
for (i = 0; i < 256; i++)
{
histogramArray[i] = 0;
}
bestT=bestT+yuzhi;
if(bestT<0)
bestT=0;
if(bestT>255)
bestT=255;
for(i=0;ibestT)
{
dstBmp[i*img.width+j]=255;
}
else
{
dstBmp[i*img.width+j]=0;
}
}
}
}
从代码上来看,开发者可能还是学生,没经过职业训练,代码可读性不高,缩进对齐什么的都没有(我只是校对了最后一个函数)。下面的好像是抄来的,比较正规点。