⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 disrecunit.~pas

📁 用Delphi写的车牌字符各种特征提取实验系统
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
        begin
          for j:=-1 to 1 do
          begin
            if (i=0)and(j=0) then continue;
            x0:=x+i+1;y0:=y+j+1;
            if (x0<1)or(x0>bmp1.Width)or(y0<1)or(y0>bmp1.Height) then continue;
            if (mindis[y0,x0]=-1)then continue;
            if bmp1.Canvas.Pixels[x0-1,y0-1]<>RGB(DisColor-40,DisColor-40,DisColor-40) then continue;
            disTry:=abs(i)+abs(j)+mindis[y0,x0];
           // ListBox1.Items.Add(INtToStr(disTry));
            if mindisTry>=disTry then mindisTry:=disTry;
          end;//end for j
        end;//end for i;
        if mindisTry<1000 then
        begin
          mindis[y+1,x+1]:=mindisTry; Flag:=False;
          p[3*x]:=DisColor;p[3*x+1]:=DisColor;p[3*x+2]:=DisColor;
        //  ListBox1.Items.Add(IntToStr(mindis[y+1,x+1])+'  '+intToStr(x)+'  '+intToStr(y));
        end; //end if
      end; //end for x
    end;   //end for y
  end; //end while
end;



procedure  TDisRecForm.XDisRec(bmp:TBitmap;PCharPic:CharPicture);
var   //距离匹配方法程序
  E:Widestring;
  FPas:String;
  N1,N2,k,x,y,i,j,x0,y0,ZFNum:integer;
  bmp1,bmp2:TBitmap;
  p,q:pbytearray;
  mindis,PPMindis:RecDistance;
  PiPeiNum,disColor,disTry,mindisTry:integer;
  PiPeiSub,PiPeiDis,PiPeiDis1,PiPeiDis2,PiPeiMin:double;
  Flag:boolean;
  Img:TImage;
begin
  Img:=TImage.Create(Application);
  E:=EFile.Text;
  ZFNum:=StrToInt(CharNumEdit.Text)-1;
  bmp1:=TBitMap.Create;bmp2:=TBitmap.Create;
  setLength(mindis,(bmp.Height+1),(bmp.Width+1));
  GetRelativeDis(bmp,mindis,N1); //计算参考图像与识别图像距离最小相关系数
  ZFNum:=StrToInt(charNumEdit.Text)-1;
  bmp1.Assign(bmp); PiPeiMin:=100000;PiPeiNum:=-1;
  ListBox1.Clear;
  for i:=0 to ZFNum do
  begin
    bmp2.Assign(PCharPic[i+1]);
    Img.Picture.Assign(bmp2);
    if CHeckBox1.Checked=True then TextForm.GetBone(Img,Img);
    bmp2.Assign(Img.Picture.Bitmap);
    GetRelativeDis(bmp2,PPmindis,N2);
    PiPeiDis1:=0;PiPeiSub:=0;PiPeiDis2:=0;
    for y:=0 to bmp.Height-1 do
    begin
      p:=bmp1.ScanLine[y];
      q:=bmp2.ScanLine[y];
      for x:=0 to bmp.Width-1 do
      begin
        if (q[3*x]=255)and(p[3*x]=255) then continue;
        if q[3*x]=0 then PiPeiDis2:=PiPeiDis2+MinDis[y+1,x+1];
        if p[3*x]=0 then PiPeiDis1:=PiPeiDis1+PPMinDis[y+1,x+1];
      end;
    end;
    PiPeiDis1:=PiPeiDis1/N1;
    PiPeiDis2:=PiPeiDis2/N2;
    PiPeiDis:=(PiPeiDis1+PiPeiDis2)/2;
    if PiPeiMin>PiPeiDis then begin PiPeiMin:=PiPeiDis;PiPeiNum:=i;end;
    ListBox1.items.add(E[i+1]+':  '+FloatToStr(PiPeiDis));
  end;
  bmp2.Assign(PCharPic[PiPeiNum+1]);
  RelativedEdit.Text:=FloatTostr(PiPeiMin);
  MatchImage.Picture.Bitmap.Assign(bmp2);
  ResLabel.Caption:='匹配结果:'+E[PiPeiNum+1];
  IMg.Free;
end;

procedure  TDisRecForm.Zoom(Image1,NormalImage:TImage);
var
  Mybmp: TBitmap;
begin
  self.DoubleBuffered := True;
  Mybmp := TBitmap.Create;
  Mybmp.Width := 32;
  Mybmp.Height := 32;
  NormalImage.Width := Mybmp.Width;
  NormalImage.Height := MyBmp.Height;
  SetStretchBltMode(Mybmp.Canvas.Handle, HalfTone);
  Stretchblt(Mybmp.Canvas.Handle, 0, 0, Mybmp.Width,
    Mybmp.Height, image1.Canvas.Handle, 0, 0, image1.Picture.Bitmap.Width,
    image1.Picture.Bitmap.Height,
    SRCCOPY);
  Mybmp.PixelFormat := pf24bit;
  NormalImage.Picture.Bitmap.Assign(MyBmp);
  MyBmp.Free;
end;
procedure  TDisRecForm.GetRegion(Bmp: TBitmap);
var
  x,y,top,bottom,left,right:integer;
   p,q:pbytearray;
   bitp:Tbitmap;
begin
  top:=bmp.Height;  bottom:=-1;
  bmp.PixelFormat:=pf24bit;
  left:=bmp.Width;  right:=-1;
  for y:=0 to bmp.Height-1 do
  begin
    p:=bmp.ScanLine[y];
    for x:=0 to bmp.Width-1 do
    begin
      if p[3*x]<>255 then
      begin
        if y<top then top:=y;
        if y>bottom then bottom:=y;
        if x<left then left:=x;
        if x>right then right:=x;
      end;
    end;
  end;
  CreateBmp(Left,Right,Top,Bottom,bmp);
end;
/////////////////////////////////////////////////////////////////////////////////////////
procedure  TDisRecForm.CreateBmp(Left,Right,Top,Bottom: integer;bmp1:TBitmap);
var
p,q:pbytearray;
x,y,r,g,b,n,gray:integer;
bmp,bitp:Tbitmap;
s,t:string;
begin
   bmp:=Tbitmap.Create;
   bmp.PixelFormat:=pf24bit;
   bmp.Assign(bmp1);
   bitp:=TBitmap.Create;
   bitp.PixelFormat:=pf24bit;
   bitp.Assign(ImageViewForm.Image1.Picture.Bitmap);
   bitp.NewInstance;
   bitp.Width:=right-left+1;
   bitp.Height:=bottom-top+1;
   for y:=top to bottom do
   begin
     p:=bmp.ScanLine[y];
     q:=bitp.ScanLine[y-top];
     for x:=left to right do
     begin
       q[3*(x-left)]:=p[3*x];
       q[3*(x-left)+1]:=p[3*x+1];
       q[3*(x-left)+2]:=p[3*x+2];
     end;
   end;
   bmp1.Assign(bitp);
   bitp.Free;
   bmp.Free;
  //image1.Picture.Bitmap:=bitp;
end;
procedure TDisRecForm.ClearSmall(Sender:Tobject;Img,Img2:TImage);
var
  x,y,x0,y0,x1,y1:integer;
  bitp:TBitmap;
  STX,STY:array[1..8000] of integer;
  m,MAXST,MaxNumber,MaxColor:integer;
  ClassNum:TColor;
begin
  bitp:=TBitmap.Create;
  bitp.Assign(Img.Picture.Bitmap);
  bitp.PixelFormat:=pf24bit;
  ClassNum:=Clblack;
  MaxNumber:=0;
  for y:=0 to bitp.Height-1 do
  begin
    for x:=0 to bitp.Width-1 do
    begin
      if bitp.Canvas.Pixels[x,y]<>clblack then continue;
      ClassNum:=ClassNum+50;
      bitp.Canvas.Pixels[x,y]:=ClassNum;
      m:=1;STX[m]:=x;STY[m]:=y;MaxST:=0;
      while m<>0 do
      begin
        x0:=STX[m];y0:=STY[m];dec(m);
       // ListBox1.Items.Add(IntToStr(x0)+'  '+IntTostr(y0));
        x1:=x0+1;y1:=y0;
        if (x1<bitp.Width)and(bitp.Canvas.Pixels[x1,y1]=0)then
        begin
          bitp.Canvas.Pixels[x1,y1]:=ClassNum;
          inc(m);STX[m]:=x1;STY[m]:=y1;
        end;
        x1:=x0-1;y1:=y0;
        if (x1>=0)and(bitp.Canvas.Pixels[x1,y1]=0)then
        begin
          bitp.Canvas.Pixels[x1,y1]:=ClassNum;
          inc(m);STX[m]:=x1;STY[m]:=y1;
        end;
        x1:=x0;y1:=y0+1;
        if (y1<bitp.Height)and(bitp.Canvas.Pixels[x1,y1]=0)then
        begin
          bitp.Canvas.Pixels[x1,y1]:=ClassNum;
          inc(m);STX[m]:=x1;STY[m]:=y1;
        end;
        x1:=x0;y1:=y0-1;
        if (y1>=0)and(bitp.Canvas.Pixels[x1,y1]=0)then
        begin
          bitp.Canvas.Pixels[x1,y1]:=ClassNum;
          inc(m);STX[m]:=x1;STY[m]:=y1;
        end;
        if MAXST<m then MaxST:=m;
      end;//end while
      if MaxNumber<MaxST then
      begin
        MaxColor:=ClassNum;
        MaxNumber:=MaxST;
      end;
    end;//end for x
  end;//end for y
 for y:=0 to bitp.Height-1 do
  begin
    for x:=0 to bitp.Width-1 do
    begin
      if bitp.Canvas.Pixels[x,y]=MaxColor then
        bitp.Canvas.Pixels[x,y]:=Clblack
      else bitp.Canvas.Pixels[x,y]:=clwhite;
    end;
  end;
  Img2.Picture.Bitmap.Assign(bitp);
  bitp.Free;
end;
procedure TDisRecForm.PointDisRec(bmp:TBitmap);
var  //点匹配方法程序
  E:Widestring;
  FPas:String;
  x,y,i,j,x0,y0,ZFNum:integer;
  bmp1,bmp2:TBitmap;
  p,q:pbytearray;
  oridis,mindisnum,mindis:Integer;
begin
  E:=EFile.Text;
  ZFNum:=StrToInt(charNumEdit.Text)-1;
  FPas:=ExtractFilePath(Application.ExeName)+'\DisRecognice\'+RecCombobox.Text+'\';
  bmp1:=TBitMap.Create;bmp2:=TBitmap.Create;
  bmp1.Assign(bmp);mindis:=100000;mindisnum:=0;
  ListBox1.Clear;
  for i:=0 to ZFNum do
  begin
    MatchIMage.Picture.LoadFromFile(FPas+IntToStr(i)+'.bmp');
    bmp2.Assign(MatchImage.Picture.Bitmap);
    oridis:=0;
    for y:=0 to bmp2.Height-1 do
    begin
      p:=bmp1.ScanLine[y];
      q:=bmp2.ScanLine[y];
      for x:=0 to bmp2.Width-1 do
      begin
         if p[3*x]=q[3*x] then continue;
         inc(oridis);
      end; //end for x
    end;  //end for y
    ListBox1.Items.Add(E[i+1]+'  '+IntToStr(oridis));
    if mindis>oridis then begin mindisnum:=i;mindis:=oridis;end;
  end;  //end for i
  bmp2.LoadFromFile(FPas+IntToStr(mindisnum)+'.bmp');
  DiffPointdEdit.Text:=IntTostr(mindis);
  MatchImage.Picture.Bitmap.Assign(bmp2);
  RESLabel.Caption:='匹配结果:'+E[mindisnum+1];
  bmp1.Free;bmp2.Free;
end;

procedure TDisRecForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DisRecForm.Hide;
TextForm.show;
if TextForm.N3.Checked=True then ChartForm.show
else ChartForm.Hide;
if TextForm.N6.Checked=True then ImageViewForm.show
else ImageViewForm.Hide;
if TextForm.N4.Checked=True then FileListForm.show
else FileListForm.Hide;
if TextForm.N2.Checked=True then TextViewForm.show
else TextViewForm.Hide;
PictureOpen:=False;
end;

procedure TDisRecForm.BitBtn2Click(Sender: TObject);
begin
close;
end;

procedure TDisRecForm.MatchBitBtnClick(Sender: TObject);
var
 i:integer;
 Label loop;
begin
if PictureOpen=False then goto loop;
if PointMatchRadioButton.Checked=True then PointDisRec(CharImage.Picture.Bitmap)
else if DisMatchRadioButton.Checked=True then
begin
  if RecCombobox.Text='汉字' then   XDisRec(CharImage.Picture.Bitmap,ChinesePic)
  else if RecCOmbobox.Text='英文' then XDisRec(CharImage.Picture.Bitmap,EnglishPic)
  else XDisRec(CharImage.Picture.Bitmap,NumberPic);
end else if PropertyMatchRadioButton.Checked=True then begin
  if RecCombobox.Text='汉字' then PropertyRec(CharImage.Picture.Bitmap,ChinesePic)
  else if RecCOmbobox.Text='英文' then PropertyRec(CharImage.Picture.Bitmap,EnglishPic)
  else PropertyRec(CharImage.Picture.Bitmap,NumberPic);
end else begin
  if RecCombobox.Text='汉字' then MomentRec(CharImage.Picture.Bitmap,ChinesePic)
  else if RecCOmbobox.Text='英文' then MomentRec(CharImage.Picture.Bitmap,EnglishPic)
  else MomentRec(CharImage.Picture.Bitmap,NumberPic);
end;
loop:;
end;

procedure TDisRecForm.RecComboBoxChange(Sender: TObject);
var
  FPas:string;
begin
EFile.Clear;
FPas:=ExtractFilePath(Application.ExeName)+'\carNumber\';
ViewMemo.Clear;
case RecCombobox.ItemIndex of
   0:begin
     charNumEdit.Text:='29';
     EFile.LoadFromFile(FPas+RecCombobox.Text+'.txt');
     ViewMemo.Lines.Add(EFile.Text);
   end;
   1:begin
     charNumEdit.Text:='26';
     EFile.LoadFromFile(FPas+RecCombobox.Text+'.txt');
     ViewMemo.Lines.Add(EFile.Text);
   end;
   2:begin charNumEdit.Text:='23';
     EFile.LoadFromFile(FPas+RecCombobox.Text+'.txt');
     ViewMemo.Lines.Add(EFile.Text);
   end;
end;
end;

procedure TDisRecForm.Button1Click(Sender: TObject);
begin
if disopenpicturedialog.Execute then
begin
  PictureOpen:=True;
  CharImage.Picture.LoadFromFile(disopenpicturedialog.FileName);
  Image1.Picture:=CharImage.Picture;
  if RecCOmbobox.Text<>'汉字' then  ClearSmall(Sender,CharImage,CharImage);
  GetRegion(CharImage.Picture.Bitmap);
  Zoom(CharImage,CharImage);
  if CHeckBox1.Checked=True then TextForm.GetBone(CharImage,CharImage);
//  TextForm.Thin(CharImage.Picture.Bitmap);/////////////////////////////////////////
end;
end;

procedure TDisRecForm.FormCreate(Sender: TObject);
var
  i:integer;
  FPas:string;
begin
FPas:=ExtractFilePath(Application.ExeName)+'\carNumber\汉字.txt';
EFile:=TStringList.Create;
EFile.LoadFromFile(FPas);
PictureOpen:=False;
FPas:=ExtractFilePath(Application.ExeName)+'\DisRecognice\汉字\';
for i:=0 to 28 do
begin
  ChinesePic[i+1]:=TBitmap.Create;
  ChinesePic[i+1].LoadFromFile(FPas+IntToStr(i)+'.bmp');
end;

FPas:=ExtractFilePath(Application.ExeName)+'\DisRecognice\英文\';
for i:=0 to 25 do
begin
  EnglishPic[i+1]:=TBitmap.Create;
  EngLishPic[i+1].LoadFromFile(FPas+IntToStr(i)+'.bmp');
end;

FPas:=ExtractFilePath(Application.ExeName)+'\DisRecognice\数字\';
for i:=0 to 23 do
begin
  NumberPic[i+1]:=TBitmap.Create;
  NumberPic[i+1].LoadFromFile(FPas+IntToStr(i)+'.bmp');
end;
end;

procedure TDisRecForm.FormDestroy(Sender: TObject);
var
 i:integer;
begin
EFile.Free;
for i:=0 to 28 do ChinesePic[i+1].Free;
for i:=0 to 25 do EnglishPic[i+1].Free;
for i:=0 to 23 do NumberPic[i+1].Free;
end;
procedure TDisRecForm.SpeedButton1Click(Sender: TObject);
begin
IF saveDialog1.Execute then
begin
  ListBox1.Items.SaveToFile(SaveDialog1.FileName);
end;
end;

procedure TDisRecForm.BitBtn3Click(Sender: TObject);
var
  FPas:String;
  y,x,i,j,m,N:integer;
  FNum:string;
  TRecDis:RecDistance;
begin
  FPas:=ExtractFilePath(Application.ExeName);
  SaveMemo.Clear; m:=0;
  for i:=0 to 28 do
  begin
    GetRelativeDis(ChinesePic[i+1],TRecDis,N);
    FNum:='';
    for y:=1 to 32 do
    begin
     for x:=1 to 32 do
     begin
       if TRecDis[y,x]>=10 then inc(m);
       FNum:=FNum+IntToStr(TRecDis[y,x]);
     end;
    end;
    FNum:=FNum+IntToStr(N);
    SaveMemo.Lines.Add(FNum);
  end;
  //Edit1.Text:=IntToStr(m);
  SaveMemo.Lines.SaveToFile(FPas+'\DisRecognice\汉字'+'.txt');
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -