在車牌識別中,字符模板匹配的模板庫是很大的。
包括 10個阿拉伯數字以及26個英文字母還有幾十個漢字,每個庫都是一張小圖片,加載起來也比較繁瑣。
後面還有可能爲提高識別增加額外的模板庫。
之前的處理中,是把這些庫的圖片文件放到一個文件夾中,程序啓動後,再一個一個讀取,這樣文件的數量就比較多。
原圖片模板如下:
程序穩定後,我們就不要這些字符模板庫了,可以用數組的形式代替,就是把這些文件數據保存一個c語言數組裏面,直接編譯到程序中,運行程序的時候直接使用,不用一個一個加載,再去匹配。
目前使用的moan庫圖片是20x40的8bit灰度BMP格式文件,其信息頭長度54+256x4=1078,直接略過信息頭和調色板獲取圖片數據信息
模板製作的c代碼如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CHAR_NUM 66
#define CHAR_WIDHT 20
#define CHAR_HEIGHT 40
char template[CHAR_NUM][CHAR_WIDHT * CHAR_HEIGHT];
int readstr(FILE *inFIL, unsigned char *srcBmp)
{
int width,height, headlength;
int i,j,line8;
unsigned char *temp;
unsigned char temp1;
width = CHAR_WIDHT;
height = CHAR_HEIGHT;
headlength = 1078;//54 + 256 * 4;
line8=(width*8+31)/32*4;
temp=(char *)malloc(height * line8 * sizeof(char));
fseek(inFIL, headlength, SEEK_SET);
fread(temp, line8 * height,1, inFIL);
if(temp==NULL)
{
printf("\n讀取失敗\n");
return -1;
}
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
temp1 = temp[i*line8+j];
if(temp1 > 150)
temp1 = 255;
else
temp1 = 0;
srcBmp[(height-i-1)*width+j]= temp1;// (temp1 > 150 ? 255:0);//(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;
return 0;
}
int readtemplate(char *path, char src[CHAR_NUM][CHAR_WIDHT * CHAR_HEIGHT])
{
FILE *f[72];
int i;
char str[100];
for(i = 0; i <= CHAR_NUM; i++)
{
sprintf(str, "%s%d.bmp", path, i);
f[i]=fopen(str,"rb");
if(f[i]==NULL)
{
printf("can't open patch:%s\n", str);
return -1;
}
readstr(f[i], src[i]);
fclose(f[i]);
}
return 0;
}
int main()
{
int i, j;
FILE *f;
unsigned char p;
char buf[5];
f = fopen("Template.h", "wb");
if(f == NULL)
{
printf("Can't open the file\n");
return -1;
}
if(readtemplate(".\\test\\moban\\", template) != 0)
{
printf("readtemplate error\n");
return -1;
}
for(i = 0; i < CHAR_NUM; i++)
for(j = 0; j < CHAR_WIDHT * CHAR_HEIGHT; j ++)
{
if((j%800 == 0))
fwrite("\n",strlen("\n"),1,f);
p = template[i][j];
sprintf(buf,"%4d,", p);
fwrite(buf,strlen(buf),1,f);
}
fclose(f);
return 0;
}
製作後的數組如下(變量名自行添加)
之前算法是一個一個讀取,然後重新複製一遍
void readmoban(char *path,struct BMP_Plate *img2)
{
FILE *f[72];
int i;
char str[80];
for(i=0;i<=66;i++)
{
sprintf(str,"%s%d.bmp", path, i);
f[i]=fopen(str,"rb");
if(f[i]==NULL)
{
printf("can't open moban:%d,%s\n", i, str);
exit(-1);
}
readstr(f[i],img2->strc[i]);
displayGray(img2->strc[i],20,40,0,0);
fclose(f[i]);
}
}
現在,只需要,指定數組指針,不用加載圖片,也不用複製
int readtemplate(char *moban,struct BMP_Plate *img2)
{
int i;
#if 0
for(i = 0; i <= CHAR_NUM; i++)
{
memcpy(img2->strc[i],&moban[i * TEMPPLATE_SIZE], TEMPPLATE_SIZE);
}
#else
for(i = 0; i <= CHAR_NUM; i++)
{
img2->strc[i] = &moban[i * TEMPPLATE_SIZE];
}
#endif
return 0;
}
不用再重新複製數據了,優化了代碼,節省了時間(其實節省的也不是很多^^ )。