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

📄 screenhistostretchgrays.pas

📁 给出了基于神经网络的手写体数字的识别程序
💻 PAS
📖 第 1 页 / 共 5 页
字号:
               if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] =
                  255) and (Q[3 * (X - 1) + 2] = 255)) or ((Q[3 * (X
                     +
                     1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and
                  (Q[3 * (X + 1) + 2] = 255)) or ((P[3 * X] = 0) and
                  (P[3 * X + 1] = 255) and (P[3 * X + 2] = 255))
                  or ((R[3 * X] = 255) and (R[3 * X + 1] = 255) and
                  (R[3
                  * X + 2] = 255))) then
               begin
                  O[3 * X] := 255;
                  O[3 * X + 1] := 255;
                  O[3 * X + 2] := 255;
                  //// 将满足条件的黑色点置为白色
               end;
            end;
         end;
      end;
   end
   else
   begin
      for Y := 1 to newbmp.Height - 2 do
      begin
         O := bitmap.ScanLine[Y];
         // P := newbmp.ScanLine[Y - 1];
         Q := newbmp.ScanLine[Y];
         // R := newbmp.ScanLine[Y + 1];
         for X := 1 to newbmp.Width - 2 do
         begin
            //  判断一个黑点上下邻居是否有白点,有则腐蚀,置黑点为白色
            //  白色点就保持不变
            if ((O[3 * X] = 0) and (O[3 * X + 1] = 0) and (O[3 * X + 2]
               = 0)) then
            begin
               if (((Q[3 * (X - 1)] = 255) and (Q[3 * (X - 1) + 1] =
                  255) and (Q[3 * (X - 1) + 2] = 255)) or ((Q[3 * (X
                     +
                     1)] = 255) and (Q[3 * (X + 1) + 1] = 255) and
                  (Q[3 * (X + 1) + 2] = 255))) then
               begin
                  O[3 * X] := 255;
                  O[3 * X + 1] := 255;
                  O[3 * X + 2] := 255;
                  // 将满足条件的黑色点置为白色
               end;
            end;
         end;
      end;
   end;
   result := True;
end;


function TFormHistoStretchGrays.BitmapDilate(Bitmap: TBitmap; Hori: Boolean): Boolean;
var
   X, Y: integer;
   O, P, Q, R: pByteArray;
   newbmp: TBitmap;
begin
   newbmp := TBitmap.Create;
   newbmp.Assign(bitmap);
   Hori := True;
   if (Hori) then
   begin
      for Y := 1 to newbmp.Height - 2 do
      begin
         O := bitmap.ScanLine[Y];
         P := newbmp.ScanLine[Y - 1];
         Q := newbmp.ScanLine[Y];
         R := newbmp.ScanLine[Y + 1];
         for X := 1 to newbmp.Width - 2 do
         begin
            if ((O[3 * X] = 255) and (O[3 * X + 1] = 255) and (O[3 * X
               + 2] = 255)) then
            begin
               if (((Q[3 * (X - 1)] = 0) and (Q[3 * (X - 1) + 1] = 0)
                  and (Q[3 * (X - 1) + 2] = 0)) or ((Q[3 * (X + 1)]
                  = 0)
                  and (Q[3 * (X + 1) + 1] = 0) and
                  (Q[3 * (X + 1) + 2] = 0)) or ((P[3 * X] = 0) and
                  (P[3 * X + 1] = 0) and (P[3 * X + 2] = 0))
                  or ((R[3 * X] = 0) and (R[3 * X + 1] = 0) and
                  (R[3 * X + 2] = 0))) then
               begin
                  O[3 * X] := 0;
                  O[3 * X + 1] := 0;
                  O[3 * X + 2] := 0;
               end;

            end;
         end;
      end;
   end
   else
      for Y := 1 to newbmp.Height - 2 do
      begin
         O := bitmap.ScanLine[Y];
         Q := newbmp.ScanLine[Y];
         for X := 1 to newbmp.Width - 2 do
         begin
            if ((O[3 * X] = 255) and (O[3 * X + 1] = 255) and (O[3 * X
               + 2] = 255)) then
            begin
               if (((Q[3 * (X - 1)] = 0) and (Q[3 * (X - 1) + 1] = 0)
                  and (Q[3 * (X - 1) + 2] = 0)) or ((Q[3 * (X + 1)]
                  = 0)
                  and (Q[3 * (X + 1) + 1] = 0) and
                  (Q[3 * (X + 1) + 2] = 0))) then
                  O[3 * X] := 0;
               O[3 * X + 1] := 0;
               O[3 * X + 2] := 0;
            end;
         end;

      end;
   result := True;
end;
procedure TFormHistoStretchGrays.SelectionSort(var a: array of integer);
var
  i, j, t: integer;
begin
  for i := low(a) to high(a) - 1 do
    for j := high(a) downto i + 1 do
      if a[i] > a[j] then
      begin
        //交换值(a[i], a[j], i, j);
        t := a[i];
        a[i] := a[j];
        a[j] := t;
      end;
      end;


procedure TFormHistoStretchGrays.Button1Click(Sender: TObject);
begin
self.DoubleBuffered := True;
   // 设立双缓冲模式
  PictureTwoValue(ImageHistoStretched.Picture.Bitmap,TwoValueTrackBar.Position+128);
   // 调用位图二值化的过程
   if (BitmapErose(self.ImageHistoStretched.Picture.Bitmap, True)) then
   begin
      ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
   end
   else
      showmessage('腐蚀失败');
   // 调用图象腐蚀函数
   if (BitmapDilate(self.ImageHistoStretched.Picture.Bitmap, False)) then
   begin
      ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
   end
   else
      showmessage('膨胀失败');

end;
procedure TFormHistoStretchGrays.connect(tbmp:tbitmap; x,y:integer; var xmin,xmax,ymin,ymax:integer);
begin
    tbmp.canvas.pixels[x,y]:=254;
    xmin:=min(x,xmin);
    xmax:=max(x,xmax);
    ymin:=min(y,ymin);
    ymax:=max(y,ymax);
     if tbmp.canvas.pixels[x+1,y]=clwhite then
        connect(tbmp, x+1,y,xmin,xmax,ymin,ymax);
     if tbmp.canvas.pixels[x+1,y-1]=clwhite then
         connect(tbmp,x+1,y-1,xmin,xmax,ymin,ymax);
     if tbmp.canvas.pixels[x,y-1]=clwhite then
          connect(tbmp,x,y-1,xmin,xmax,ymin,ymax);
      if tbmp.canvas.pixels[x-1,y-1]=clwhite then
          connect(tbmp,x-1,y-1,xmin,xmax,ymin,ymax);
      if tbmp.canvas.pixels[x-1,y]=clwhite then
           connect(tbmp,x-1,y,xmin,xmax,ymin,ymax);
       if tbmp.canvas.pixels[x-1,y+1]=clwhite then
           connect(tbmp,x-1,y+1,xmin,xmax,ymin,ymax);
       if tbmp.canvas.pixels[x,y+1]=clwhite then
            connect(tbmp,x,y+1,xmin,xmax,ymin,ymax);
       if tbmp.canvas.pixels[x+1,y+1]=clwhite then
            connect(tbmp,x+1,y+1,xmin,xmax,ymin,ymax);
       if tbmp.canvas.pixels[x+1,y]=clwhite then
           connect(tbmp,x+1,y,xmin,xmax,ymin,ymax);
       end;
procedure TFormHistoStretchGrays.OrientClick(Sender: TObject);
var
   bmp1, bmp2: Tbitmap;
   // 临时位图
   p1: pByteArray;
   c,x,y,xmin,xmax,ymin,ymax: integer;
   PlateRect1,PlateRect2:trect;
   TheRect: TRect;
   //tm1,tm2:integer;
begin
   //tm1:=gettickcount;
  plateImg.Picture.Assign(nil);
   self.DoubleBuffered := true;
   //采用双缓冲模式
   bmp1 := Tbitmap.Create;
   bmp2 := Tbitmap.Create;
   //Create  bmp1,bmp2
   bmp1.Assign(ImageHistoStretched.Picture.Bitmap);
   bmp1.PixelFormat := pf24bit;
   //设置位图格式
  bmp2.Assign(OroginalColorImage.Picture.bitmap);
  bmp2.PixelFormat := pf24bit;
   for y := 1 to bmp1.Height - 2 do
   begin
      c:=0;
      p1 := bmp1.ScanLine[y];
   for x := 1 to bmp1.Width - 3 do
    begin
      xmin:=x;
      xmax:=x;
      ymin:=y;
      ymax:=y;
      if p1[3*x]=255 then begin
     connect(bmp1,x,y,xmin,xmax,ymin,ymax);
     if ((xmax-xmin)<=250) and ((xmax-xmin)>=50 )and ((ymax-ymin)<=150) and
     ((ymax-ymin)>=10) and ((xmax-xmin)/(ymax-ymin)>=2)
     and ((xmax-xmin)/(ymax-ymin)<=5)then
     begin
     //charGrayDistributerecon();
     TheRect := Rect(xmin,ymin,xmax,ymax);
      Platexmin:=xmin;
      Platexmax:=xmax;
      Plateymin:=ymin;
      Plateymax:=ymax;
     OroginalColorImage.Canvas.Brush.Color := clYellow;
      plateimg.Width:=xmax-xmin;
      plateimg.Height:=ymax-ymin;
      PlateRect1:=rect(xmin,ymin,xmax,ymax);
      PlateRect2:=rect(0,0,xmax-xmin,ymax-ymin);
      OroginalColorImage.Canvas.FrameRect(TheRect);
      plateimg.Canvas.copyrect(PlateRect2,ImageOriginal.canvas,PlateRect1);
      PlatePositionFurtherCertificationClick(Sender);
      end;
        end;
     end;
 end;
   bmp1.Free;
   bmp2.Free;
end;

procedure TFormHistoStretchGrays.HMargeClick(Sender: TObject);
var
    bmp1, bmp2,bmp3: Tbitmap;
    p1, p2,p3,p4,p5: pbytearray;
    //定义四个pbytearray类型变量
    i, j: integer;
    a,b: byte;
begin
     bmp1 := Tbitmap.Create;
    bmp2 := Tbitmap.Create;
    //bmp3 := Tbitmap.Create;
    bmp1.Assign(ImageHistoStretched.Picture.Bitmap);
    bmp1.PixelFormat := pf24bit;
    //24为格式便于处理
    bmp1.Width := ImageHistoStretched.Picture.Graphic.Width;
    bmp1.Height := ImageHistoStretched.Picture.Graphic.Height;
    bmp2.Assign(bmp1);
    //bmp3.Assign(bmp1);
    //备用的位图
    bmp2.PixelFormat := pf24bit;
    //bmp3.PixelFormat := pf24bit;
    for j := 1 to bmp1.Height - 2 do
    begin
        p1 := bmp1.ScanLine[j];
        //p5 := bmp3.ScanLine[j];
        p2:=bmp2.ScanLine[j-1];
        p3 := bmp2.ScanLine[j];
        p4:=bmp2.ScanLine[j+1];
        for i := 1 to bmp1.Width - 3 do
        begin
        a:=min(255,abs(p3[3*i]-p2[3*i])+abs(p4[3*i]-p3[3*i]));
        b:=min(255,abs(p2[3*i]-p2[3*(i-1)])+abs(p2[3*(i+1)]-p2[3*i]));
        p1[3*i]:=max(a,b);
        p1[3*i+1]:=p1[3*i];
        p1[3*i+2]:=p1[3*i]
        end;
        end;
        ImageHistoStretched.Picture.Bitmap.Assign(Bmp1);
    //重新显示
    ImageHistoStretched.Invalidate;
    Bmp1.Free;
    bmp2.Free;
    //释放资源
end;

procedure TFormHistoStretchGrays.ImageFilet(Bitmap: TBitmap);
var
  bmp1, bmp2: Tbitmap;
  p1, p2, p3, p4: pbytearray;
  i, j: integer;
begin
  //设置双缓冲
  self.DoubleBuffered := true;
  //创建两个位图实例
  bmp1 := Tbitmap.Create;
  bmp2 := Tbitmap.Create;
  //加在位图
  bmp1.Assign(Bitmap);
  //设置位图的象素格式
  bmp1.PixelFormat := pf24bit;
  //位图的大小
  bmp1.Width := Bitmap.Width;
  bmp1.Height := Bitmap.Height;
  //加载备份的位图
  bmp2.Assign(Bitmap);
  bmp2.PixelFormat := pf24bit;
  for j := 1 to bmp1.Height - 2 do
  begin
    //三条扫描线
    p1 := bmp1.ScanLine[j];
    p2 := bmp2.ScanLine[j - 1];
    p3 := bmp2.ScanLine[j];
    p4 := bmp2.ScanLine[j + 1];
    for i := 1 to bmp1.Width - 2 do
    begin
      //对存储9个R分量的数组进行赋值
      RvalueArray[0] := p2[3 * (i - 1) + 2];
      RvalueArray[1] := p2[3 * i + 2];
      RvalueArray[2] := p2[3 * (i + 1) + 2];
      RvalueArray[3] := p3[3 * (i - 1) + 2];
      RvalueArray[4] := p3[3 * i + 2];
      RvalueArray[5] := p3[3 * (i + 1) + 2];
      RvalueArray[6] := p4[3 * (i - 1) + 2];
      RvalueArray[7] := p4[3 * i + 2];
      RvalueArray[8] := p4[3 * (i + 1) + 2];
      //调用排序过程
      SelectionSort(RvalueArray);
      //获取R分量的中间值
      p1[3 * i + 2] := RvalueArray[4];
      //对存储9个G分量的数组进行赋值
      GvalueArray[0] := p2[3 * (i - 1) + 1];
      GvalueArray[1] := p2[3 * i + 1];
      GvalueArray[2] := p2[3 * (i + 1) + 1];
      GvalueArray[3] := p3[3 * (i - 1) + 1];
      GvalueArray[4] := p3[3 * i + 1];
      GvalueArray[5] := p3[3 * (i + 1) + 1];
      GvalueArray[6] := p4[3 * (i - 1) + 1];
      GvalueArray[7] := p4[3 * i + 1];
      GvalueArray[8] := p4[3 * (i + 1) + 1];
      //调用选择排序
      SelectionSort(RvalueArray);
      //获取G分量的中间值
      p1[3 * i + 1] := RvalueArray[4];
      //对存储9个B分量的数组进行赋值
      BvalueArray[0] := p2[3 * (i - 1)];
      BvalueArray[1] := p2[3 * i];
      BvalueArray[2] := p2[3 * (i + 1)];
      BvalueArray[3] := p3[3 * (i - 1)];
      BvalueArray[4] := p3[3 * i];
      BvalueArray[5] := p3[3 * (i + 1)];
      BvalueArray[6] := p4[3 * (i - 1)];
      BvalueArray[7] := p4[3 * i];
      BvalueArray[8] := p4[3 * (i + 1)];
      //调用选择排序过程
      SelectionSort(RvalueArray);
      //获取G分量的中间值
      p1[3 * i] := RvalueArray[4];
    end;
  end;
  Bitmap.Assign(Bmp1);
  Bmp1.Free;
  bmp2.Free;

end;

procedure TFormHistoStretchGrays.VSobelClick(Sender: TObject);
var
   bmp1, bmp2: Tbitmap;
   // 临时位图
   p1, p3, p2, p4: pByteArray;
   i, j: integer;
   r, g, b: Byte;
begin
  self.DoubleBuffered := true;
   //采用双缓冲模式
   bmp1 := Tbitmap.Create;
   bmp2 := Tbitmap.Create;
   //Create  bmp1,bmp2
   bmp1.Assign(ImageHistoStretched.Picture.Bitmap);
   bmp1.PixelFormat := pf24bit;
   //设置位图格式
   bmp2.Assign(bmp1);
   bmp2.PixelFormat := pf24bit;
   for j := 1 to bmp1.Height - 2 do
   begin
      p1 := bmp1.ScanLine[j];
      p2 := bmp2.ScanLine[j - 1];
      p3 := bmp2.ScanLine[j];
      p4 := bmp2.ScanLine[j + 1];
      for i := 1 to bmp1.Width - 2 do
      begin
         r := min(255, max(0, ((-p2[3 * (i - 1) + 2] - 0 * p2[3 * i +
            2] + p2[3 * (i + 1) + 2] - 2 * p3[3 * (i - 1) + 2] + 0 * p3[3 * i + 2]
            +2* p3[3 * (i + 1) + 2] - p4[3 * (i - 1) + 2] +
            0 * p4[3 * i + 2] + p4[3 * (i + 1)+ 2]))));
         p1[3 * i + 2] :=r; p1[3 * i + 1] :=r; p1[3 * i] :=r;
      end;
end;
 ImageHistoStretched.Picture.Bitmap.Assign(bmp1); // 读取已经利用存在垂直sobel进行检测后的位图
   bmp2.Free;
   Bmp1.Free; //释放资源
end;

procedure TFormHistoStretchGrays.HSobelClick(Sender: TObject);
var
   bmp1, bmp2: Tbitmap;
   p1, p3, p2, p4: pByteArray;
   i, j: integer;
   r, g, b: Byte;
begin
   self.DoubleBuffered := true; //采用双缓冲模式
   bmp1 := Tbitmap.Create;
   bmp2 := Tbitmap.Create;
   bmp1.Assign(ImageHistoStretched.Picture.Bitmap);

⌨️ 快捷键说明

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