车牌识别字体
文章来源 —— 绿睿科技: 车牌识别 三维网页 网页游戏
作者:赵磊
时间:2009.04
车牌识别软件需要车牌的字体信息。 作者根据《GA36-2007中华人民共和国机动车号牌》 编写了车牌字体模块。
整个模块只有一个入口函数:
bool LPRFontGet(char character,int x,int y)
在本文稍后的代码中前半部分就是这个函数的实现。 后半部分是使用这个函数的一个例子。
LPRFontGet 的功能是取得字符character在(x,y)坐标的二值像素。
输入参数:
character
需要获取字体的字符。取值范围包括数字: 0,1,2,3,4,5,6,7,8,9 和英文字母:A,B,C,D,E,F,G, H,I,J,K,L,M,N, O,P,Q,R,S,T, U,V,W,X,Y,Z。 注意,character并不区分大小写。
x
需要获取字体的x坐标。 取值范围是0~(LPT_FONT_WIDTH-1)。 LPT_FONT_WIDTH 是宏定义,目前的值是16。
y
需要获取字体的y坐标。 取值范围是0~(LPT_FONT_HEIGHT-1)。 LPT_FONT_HEIGHT 是宏定义,目前的值是LPT_FONT_WIDTH的二倍,即32。
返回值:
当输入参数合法时, 如果点(x,y)是字符上的点,LPRFontGet返回true, 否则返回false。
当输入参数非法时,LPRFontGet返回false。
注意:
1、字符1比较特殊,目前的返回值全是true。 这里并没有计算其左右的空白。 因为识别字符时, 也需要去掉图片上字符的上下左右空白。
2、LPRFontGet 本身的实现是一个类似于二值位图的数据结构, 字体信息保存在mask中;所以不需要添加任何字体文件。 其中的mask是静态常量,效率高。
3、不可以使用普通的字体,作为车牌识别的字体。
代码:
#define LPT_FONT_WIDTH 16
#define LPT_FONT_HEIGHT (LPT_FONT_WIDTH*2)
bool LPRFontGet(char character,int x,int y)
{
  static const int mask[][LPT_FONT_WIDTH*LPT_FONT_HEIGHT/(sizeof(int)*8)]=
  {
   {0x0ff00180,0x3ffc1ff8,0x781e7e7e,0xf00f700f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,
   0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0x700ff00f,0x7c3e781e,0x3ff83ffc,0x07c01ff0},
   {0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,
   0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff},
   {0x07e00100,0x3ff81ff0,0x783e7e7c,0xf00e701e,0xe000e008,0x70007000,0x3c007800,0x1e003c00,
   0x0f000f00,0x03c00780,0x01e003e0,0x00f800f0,0x003c0078,0x001e003e,0x7fff7fff,0x7fff7fff},
   {0x7ffe7ffe,0x7ffe7ffe,0x3e003c00,0x0f001f00,0x07800f80,0x03e003c0,0x0ff007f0,0x3e401fe0,
   0x70007800,0x70007000,0xf000f000,0x700ff000,0x700e700f,0x7c3e781e,0x1ff83ffc,0x03800ff0},
   {0x3c003c00,0x3e003c00,0x3f003e00,0x3f003f00,0x3f803f80,0x3fc03fc0,0x3de03de0,0x3cf03ce0,
   0x3c783c70,0x3c383c78,0x3c1e3c1c,0xffff3c1e,0xffffffff,0x3c00ffff,0x3c003c00,0x3c003c00},
   {0x7ffe7ffe,0x7ffe7ffe,0x001e001e,0x001e001e,0x001e001e,0x001e001e,0x1ffe07fe,0x3e7e3ffe,
   0x7000781c,0x70007000,0xf000f000,0x700f7000,0x700e700f,0x7c3e781e,0x1ff83ffc,0x03800ff0},
   {0x0e000000,0x0f001f00,0x07c00780,0x01e003e0,0x00f000f0,0x00780078,0x003c0038,0x0ffc07dc,
   0x3ffe3ffe,0x781e7c3e,0x700f700f,0xf00ff00f,0x701e700f,0x7e7c781e,0x1ff83ffc,0x03c00ff0},
   {0xffffffff,0xffffffff,0x7800f000,0x3c007800,0x3c003c00,0x1e001c00,0x1e001e00,0x0f000f00,
   0x0f000f00,0x07000700,0x07800780,0x07800780,0x03800380,0x03800380,0x03800380,0x03800380},
   {0x0fe00180,0x3ff81ff8,0x783c3e7c,0x701e701e,0x700e700e,0x781e701e,0x3e7c783c,0x1ff81ff8,
   0x3ffc3ffc,0x781e7c3e,0xf00f700e,0xf00ff00f,0x701f700f,0x7c3e781e,0x1ffc3ffc,0x03c00ff8},
   {0x07f00180,0x3ffc1ff8,0x7c1e3e7c,0x700e701e,0xf00f700f,0x700e700f,0x781e781e,0x7ffc7e7c,
   0x7ff07ff8,0x3c003c80,0x1e003c00,0x0f001e00,0x07800f80,0x01e003c0,0x00f801f0,0x00200078},
   {0x03c003c0,0x03c003c0,0x07e007e0,0x07e007e0,0x07e007e0,0x0ff00ff0,0x0e700ff0,0x1e780e70,
   0x1e781e78,0x1c381e78,0x3c383c38,0x3ffc3c3c,0x7ffc3ffc,0x781e7ffe,0x701e781e,0x0003701e},
   {0x07ff003f,0x3fff1fff,0x780f7fff,0x700f700f,0x700ff00f,0x780f700f,0x3fff7c0f,0x3fff1fff,
   0x780f3e0f,0x700f700f,0x700f700f,0x700f700f,0x700f700f,0x7c0f780f,0x1fff3fff,0x00ff0fff},
   {0x0ff00380,0x3ffc1ff8,0x781e7c7e,0x700f700f,0x000ff00f,0x000f000f,0x000f000f,0x000f000f,
   0x000f000f,0x000f000f,0xf00f000f,0xf00ff00f,0x700e700f,0x7c3e701e,0x1ff83ffc,0x01800ff0},
   {0x0fff03ff,0x3fff1fff,0x780f7c0f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,
   0xf00ff00f,0x700ff00f,0x700f700f,0x700f700f,0x700f700f,0x7c0f780f,0x1fff3fff,0x01ff0fff},
   {0x7fff7fff,0x7fff7fff,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x3fff3fff,
   0x000f3fff,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0xffffffff,0xfffeffff},
   {0x7fff7fff,0x7fff7fff,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x3fff3fff,
   0x3fff3fff,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000e000f},
   {0x0fe00000,0x3ffc1ff8,0x781e7e7c,0x700f700e,0x000f000f,0x000f000f,0xff8f000f,0xff8fff8f,
   0xf00fff8f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ef00f,0x783e701e,0x3ff83ffc,0x01801ff0},
   {0x700f700e,0x700f700f,0x700f700f,0x700f700f,0x700f700f,0x700f700f,0xf00f700f,0xffffffff,
   0xffffffff,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0x700ff00f},
   {0xffffffff,0xffffffff,0x0fe07ffe,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,
   0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0x0fe00fe0,0xffffffff,0xffffffff},
   {0xf0007000,0xf000f000,0xf000f000,0xf000f000,0xf000f000,0xf000f000,0xf000f000,0xf000f000,
   0xf000f000,0xf000f000,0xf000f000,0xf000f000,0x700e700e,0x7c3c781e,0x3ff83ffc,0x01800ff0},
   {0x700e000e,0x7c0e780e,0x1e0e3c0e,0x0f0e1e0e,0x078e0f0e,0x07ce078e,0x07ee07ce,0x0ffe0ffe,
   0x0e7e0ffe,0x1e3e0e3e,0x1c1e1e1e,0x3c0e1c0e,0x380e3c0e,0x780e380e,0x700f700f,0x000e700f},
   {0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,
   0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0xffffffff,0xfffeffff},
   {0xf01ff00e,0xf81ff81f,0xfc3ffc3f,0xfe7ffe7f,0xffefffff,0xf7cff7ef,0xf18ff3cf,0xf10ff18f,
   0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0xf00ef00f},
   {0xf00f700f,0xf01ff01f,0xf03ff03f,0xf07ff03f,0xf0fff07f,0xf0fff0ff,0xf1eff1ef,0xf3cff3cf,
   0xf78ff3cf,0xff0ff78f,0xff0fff0f,0xfe0fff0f,0xfc0ffe0f,0xf80ffc0f,0xf80ff80f,0x6000f00f},
   {0x0ff00180,0x3ffc1ff8,0x781e3e3e,0x700f700f,0x700f700f,0x700f700f,0x700f700f,0x700f700f,
   0x700f700f,0xf00ff00f,0xf00ff00f,0xf00ff00f,0x700ff00f,0x7c3e781e,0x1ff83ffc,0x01800ff0},
   {0x0fff01ff,0x3fff1fff,0x780f7e0f,0x700f700f,0xf00f700f,0x700ff00f,0x780f700f,0x3fff7e0f,
   0x07ff1fff,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f,0x000f000f},
   {0x07f00180,0x3ffc1ff8,0x781e3e3e,0x700e780e,0x700f700f,0x700f700f,0x700f700f,0x700e700e,
   0x700e700e,0x700e700e,0x73ce73ce,0xf78e738e,0x7f0ef70e,0x7e3e7e1e,0x3ff83ffc,0x01007ff0},
   {0x07ff007c,0x3fff1fff,0x780f3e0f,0x700f780f,0xf00f700f,0x700ff00f,0x780f700f,0x3fff7e0f,
   0x07ff1fff,0x0f0f078f,0x0e0f0f0f,0x1e0f0e0f,0x3c0f1e0f,0x380f3c0f,0x780f780f,0xf00f700f},
   {0x07f00180,0x1ffc0ff8,0x3c1e3e7c,0x780e781e,0x000e700e,0x001e000e,0x007c001e,0x1ff807f8,
   0x7c003fe0,0x70007800,0x70007000,0x7000f000,0x701e700f,0x3c3e781e,0x1ff83ffc,0x01800ff0},
   {0xffff03ff,0xffffffff,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,
   0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0,0x03c003c0},
   {0xf00e7000,0xf00ef00e,0xf00ef00e,0xf00ef00e,0xf00ef00e,0xf00ef00e,0xf00ef00e,0xf00ef00e,
   0xf00ef00e,0xf00ef00e,0xf00ef00e,0xf00ef00e,0x700ef00e,0x7c3c781e,0x3ff83ffc,0x03c01ff0},
   {0xf00e000e,0x701e700e,0x781e701e,0x781c781e,0x3c3c383c,0x3c3c3c3c,0x1c381c38,0x1e781e78,
   0x0e781e78,0x0ff00f70,0x0ff00ff0,0x07f007f0,0x07e007e0,0x03e007e0,0x03e003e0,0x01c003c0},
   {0xf10ef00f,0xf10ef10e,0x718ef18e,0x739e718e,0x73de73de,0x77de73de,0x77fe77de,0x7ffe77fe,
   0x7ffc7ffe,0x7efc7ffc,0x7efc7efc,0x7e7c7e7c,0x3c7c7e7c,0x3c3c3c7c,0x3c3c3c3c,0x383c383c},
   {0x701ef00e,0x781c701e,0x3c3c383c,0x1e783c38,0x0ef01e78,0x0ff00ef0,0x07e00fe0,0x03c007c0,
   0x03c003c0,0x07e007c0,0x0ff00fe0,0x1e700ef0,0x3c381e78,0x3c3c3c3c,0x781e781c,0xf00f701e},
   {0x781ef00f,0x783c781e,0x3c783c3c,0x1ef81e78,0x0ff00ef0,0x07e00fe0,0x07c007c0,0x03800380,
   0x03800380,0x03800380,0x03800380,0x03800380,0x03800380,0x03800380,0x03800380,0x03800380},
   {0x7fff7fff,0x7fff7fff,0x38007800,0x1c003c00,0x0e001e00,0x0f000e00,0x07800700,0x03c00380,
   0x01e001c0,0x00f001e0,0x007800f0,0x00380078,0x003c003c,0x001e001e,0x7ffe7ffe,0xfffefffe},
  };
  int character_index;
  if(character>=0&&character<=9)
   character_index=character-0;
  else if(character>=a&&character<=z)
   character_index=character-a+10;
  else if(character>=A&&character<=Z)
   character_index=character-A+10;
  else
   return false;
  if(x<0)
   return false;
  if(x>=LPT_FONT_WIDTH)
   return false;
  if(y<0)
   return false;
  if(y>=LPT_FONT_HEIGHT)
   return false;
  int int_index;
  int_index=y*LPT_FONT_WIDTH+x;
  int bit_index;
  bit_index=int_index%(sizeof(int)*8);
  int_index/=(sizeof(int)*8);
  if(mask[character_index][int_index]&(1<<bit_index))
   return true;
  else
   return false;
}
void CChildView::OnPaint()
{
  CPaintDC dc(this);
  int y;
  for(y=0;y<LPT_FONT_HEIGHT;y++)
  {
   int x;
   for(x=0;x<LPT_FONT_WIDTH;x++)
   {
     if(LPRFontGet(F,x,y))
      dc.SetPixel(x,y,RGB(64,128,128));
     else
      dc.SetPixel(x,y,RGB(200,200,200));
   }
  }
}
读者如果正在研发车牌识别系统的话, 还可以参考一下绿睿车牌识别。 其识别率和识别速度还是不错的。 目前这款高性能软件市场参考价只有¥210。