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

📄 childwin.pas

📁 数字图像预出处理系统
💻 PAS
📖 第 1 页 / 共 5 页
字号:
   withdraw.Enabled :=true;
   if (BitmapErode(LiverImage.Picture.Bitmap, True)) then
   begin
      LiverImage.Picture.Assign(LiverImage.Picture.Bitmap);
      cellStatistic.Enabled :=true;
   end
   else
      showmessage('腐蚀失败');
end;
function TMDIChild.BitmapErode(Bitmap: TBitmap; Horic: Boolean): Boolean;
var
   X, Y: Integer;
   NewBmp: TBitmap;
   P, Q, R, O: pByteArray;
begin
   NewBmp := TBitmap.Create;   //动态创建位图
   NewBmp.Assign(bitmap);
        if (Horic) 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] = 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)) 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];
         Q := NewBmp.ScanLine[Y];
         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;


procedure TMDIChild.withdrawClick(Sender: TObject);
begin
  LiverImage.Picture .bitmap.Assign(withdrawBmp);
  withdraw.Enabled :=false;
end;

procedure TMDIChild.HSIClick(Sender: TObject);
begin
  if MessageDlg('请点击鼠标选取红细胞的一块颜色',
         mtConfirmation, [mbYes, mbNo],0)=mryes then
    begin
      mark1:=true ;LiverImage.cursor:=crcross;
    end else mark1:=false;
end;

procedure  TMDIChild.RGB_TO_HSl(R,G,B:Integer;VAR H,S,L:Integer);
var
  delta:double;
  cmax,cmin:double;
  red,green,blue,hue,sat,lum:double;
begin
  red:=r/255;
  green:=g/255;
  blue:=b/255;
  cmax:=max(red,max(green,blue));
  cmin:=min(red,min(green,blue));
  Lum:=(cmax+cmin)/2;
  if cmax=cmin then
    begin
      sat:=0;
      hue:=0;
   end
  else
  begin
    if lum<0.5 then
    sat:=(cmax-cmin)/(cmax+cmin)
    else sat:=(cmax-cmin)/(2-cmax-cmin);
        delta:=cmax-cmin;
        if red=cmax then
           hue:=(green-blue)/delta
           else
             if green =cmax then
               hue:=2+(blue-red)/delta
                else
                  hue:=4.0+(red-green)/delta ;
                  hue:=hue/6;
                  if hue<0 then
                  hue:=hue+1;
                  end;
  h:=Round(hue*360);
  s:=Round(sat*100);
  l:=Round(lum*100);
end;

procedure TMDIChild.LiverImageMousedown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  h,s,l,RColor,GColor,BColor:array [0..130] of Integer;
  i,t,j,Gray:Integer;

begin
//    DotN.Click;
//    Windown16N.Click;
//    Distance5N.Click;
//    GrayLayer8N.Click;
  if ((mark1=true) or (mark2=true)) then
  begin
    if mark1=true then
    begin
      RColor[0]:=getrvalue(LiverImage.Canvas.Pixels[x, y]);
      GColor[0]:=getgvalue(LiverImage.Canvas.Pixels[x, y]);
      bColor[0]:=getbvalue(LiverImage.Canvas.Pixels[x, y]);

      RColor[1]:=getrvalue(LiverImage.Canvas.Pixels[x, y-1]);
      GColor[1]:=getgvalue(LiverImage.Canvas.Pixels[x, y-1]);
      bColor[1]:=getbvalue(LiverImage.Canvas.Pixels[x, y-1]);

      RColor[2]:=getrvalue(LiverImage.Canvas.Pixels[x+1, y]);
      GColor[2]:=getgvalue(LiverImage.Canvas.Pixels[x+1, y]);
      bColor[2]:=getbvalue(LiverImage.Canvas.Pixels[x+1, y]);

      RColor[3]:=getrvalue(LiverImage.Canvas.Pixels[x, y+1]);
      GColor[3]:=getgvalue(LiverImage.Canvas.Pixels[x, y+1]);
      bColor[3]:=getbvalue(LiverImage.Canvas.Pixels[x, y+1]);

      RColor[4]:=getrvalue(LiverImage.Canvas.Pixels[x-1, y]);
      GColor[4]:=getgvalue(LiverImage.Canvas.Pixels[x-1, y]);
      bColor[4]:=getbvalue(LiverImage.Canvas.Pixels[x-1, y]);

      RColor[5]:=getrvalue(LiverImage.Canvas.Pixels[x+1, y-1]);
      GColor[5]:=getgvalue(LiverImage.Canvas.Pixels[x+1, y-1]);
      bColor[5]:=getbvalue(LiverImage.Canvas.Pixels[x+1, y-1]);

      RColor[6]:=getrvalue(LiverImage.Canvas.Pixels[x+1, y+1]);
      GColor[6]:=getgvalue(LiverImage.Canvas.Pixels[x+1, y+1]);
      bColor[6]:=getbvalue(LiverImage.Canvas.Pixels[x+1, y+1]);

      RColor[7]:=getrvalue(LiverImage.Canvas.Pixels[x-1, y+1]);
      GColor[7]:=getgvalue(LiverImage.Canvas.Pixels[x-1, y+1]);
      bColor[7]:=getbvalue(LiverImage.Canvas.Pixels[x-1, y+1]);

      RColor[8]:=getrvalue(LiverImage.Canvas.Pixels[x-1, y-1]);
      GColor[8]:=getgvalue(LiverImage.Canvas.Pixels[x-1, y-1]);
      bColor[8]:=getbvalue(LiverImage.Canvas.Pixels[x-1, y-1]);


      hp:=0;lp:=0;sp:=0;
      for i:=0 to 8 do
      begin
        RGB_TO_HSL(RColor[i],GColor[i],BColor[i],  H[i],S[i],L[i]);
        hp:=hp+h[i];lp:=lp+l[i];sp:=sp+s[i];
      end;
      hp:=Round(hp/9); lp:=Round(lp/9); sp:=Round(sp/9);
      mark1:=false;
    end;

  if mark2=true  then
  begin
    canvas.Brush.Style:=bsclear;
    canvas.Rectangle(x-8,y-8,x+8,y+8) ;
    t:=0;
    for i:=-8 to 8 do
      for j:=-8 to 8 do
        begin
          RColor[t]:=getrvalue(LiverImage.Canvas.Pixels[x+i, y+j]);
          GColor[t]:=getgvalue(LiverImage.Canvas.Pixels[x+i, y+j]);
          bColor[t]:=getbvalue(LiverImage.Canvas.Pixels[x+i, y+j]);
          t:=t+1;
        end;
   hp:=0;lp:=0;sp:=0;
   for i:=0 to 120 do
   begin
     RGB_TO_HSL(RColor[i],GColor[i],BColor[i],  H[i],S[i],L[i]);
     hp:=hp+h[i];lp:=lp+l[i];sp:=sp+s[i];
   end;
   hp:=Round(hp/121); lp:=Round(lp/121); sp:=Round(sp/121);
   mark2:=false;
 end;
  HSLform.Show ;
  hslform.h.Caption :=inttostr(hp);
  hslform.s.Caption :=inttostr(sp);
  hslform.l.Caption :=inttostr(lp);
  LiverImage.cursor:=crDefault;
  TwoValue1.enabled:=true;
 end;
 mark4:=true;
 Canvas.Brush.Style:=bsclear;
 LiverImage.Canvas.Pen.Mode :=pmNotXor;
 LiverImage.Canvas.Pen.Color:=clRed;
 if mark5  then
   begin
   LiverImage.Canvas.Rectangle(startpoint.x,startpoint.Y,endpoint.X,endpoint.Y);
   LiverImage.Canvas.Rectangle(startpoint.x,startpoint.Y,endpoint.X,endpoint.Y);
   end;
 startpoint:=point(x,y);

 //LiverImage.Canvas.Brush.Style :=bsClear;
end;



procedure TMDIChild.LiverImageMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var RColor,GColor,BColor,H,L,S,gray:Integer;
begin
 mainform.StatusBar1.panels[0].Text :='位置:'+inttostr(x)+','+inttostr(y)+'';
 RColor:=getrvalue(LiverImage.Canvas.Pixels[x, y]);
 GColor:=getgvalue(LiverImage.Canvas.Pixels[x, y]);
 BColor:=getbvalue(LiverImage.Canvas.Pixels[x, y]);
if (rColor=gColor) then  gray:=rColor else
 Gray :=Round( RColor * 0.3 + GColor * 0.59 + BColor * 0.11);
 RGB_TO_HSL(RColor,GColor,BColor,H,S,L);
 mainform.StatusBar1 .panels[1].Text :='色度:'+inttostr(h)+' ,饱和度:'+inttostr(s)+' ,亮度:'+inttostr(l)+' ,灰度:'+inttostr(Gray);
 LiverImage.Hint:='色度:'+inttostr(h)+' ,饱和度:'+inttostr(s)+' ,亮度:'+inttostr(l) +' ,灰度:'+inttostr(Gray);
 if (ChoiceImageMark=true) and (mark4=true) then
     begin
  // LiverImage.Canvas.Brush.Style:=bsclear;
   LiverImage.Canvas.Pen.Mode:= pmnotXor;
   LiverImage.Canvas.Pen.Color:=clRed;
   //LiverImage.Canvas.Pen.Width:=1;
   self.LiverPartImage.Canvas.Rectangle(startpoint.x,startpoint.Y,X,Y);
   endpoint:=point(x,y);
   Self.LiverPartImage.Canvas.Rectangle(startpoint.x,startpoint.Y,endpoint.X,endpoint.Y);
     end;
end;

procedure TMDIChild.TwoValue1Click(Sender: TObject);
var
  Bmp : TBitmap ;
  p: PByteArray;
  x,y:Integer;
  HueMax1,HueMax2,HueMin1,HueMin2,
  SaturationMax,Saturationmin,LightnessMax,Lightnessmin:Integer;
begin
  withdrawBmp.Assign(LiverImage.Picture.Bitmap);
  withdraw.Enabled :=true;
  mainForm.ToolButton4.Enabled:=true;
  hf:=strtoint(HSLform.edit1.text);
  lf:=strtoint(HSLform.edit2.text);
  sf:=strtoint(HSLform.edit3.text);
  Bmp := TBitmap.Create;
  Bmp.Assign(Self.LiverImage.Picture.Bitmap); //24位图处理
  Bmp.PixelFormat := pf24Bit;
  SaturationMax:=min((sp+sf),100);  Saturationmin:=Max((sp-sf),0);
  LightnessMax:=min((lp+lf),100);   Lightnessmin:=Max((lp-lf),0);
      if (hp+hf)>=360 then begin
  HueMin1:=0;HueMax1:=(hp+hf)-360;
  HueMax2:=360;HueMin2:=hp-hf;
      end;
      if (hp-hf)<=0 then
  begin
  HueMin1:=0;   HueMax1:=hp+hf;
  HueMin2:=360+(hp-hf); HueMax2:=360;
      end;
  for y := 0 to Bmp.Height - 1 do
    begin
        p := Bmp.scanline[y];
        for x := 0 to Bmp.Width - 1 do
        begin
            //算出每一点的灰度值
         RGB_TO_HSL (p[x * 3 + 2],p[x * 3 + 1],p[x* 3],h1,s1,l1);
          if (((h1<=HueMax1)and(h1>=HueMin1))and((l1<=LightnessMax)and(l1>=Lightnessmin))
     and ((s1<=SaturationMax)and(s1>=Saturationmin)))      or
          (((h1<=HueMax2)and(h1>=HueMin2))and((l1<=LightnessMax)and(l1>=Lightnessmin))
          and ((s1<=SaturationMax)and(s1>=Saturationmin)))
            then  begin
                    p[x * 3 + 2]:=0;
                    p[x * 3 + 1]:=0;
                    p[x* 3]:=0;
                  end else
                  begin
           p[x * 3 + 2]:=255;p[x * 3 + 1]:=255; p[x* 3]:=255;
                  end;
        end;
    end;
    LiverImage.Picture .Bitmap .Assign(Bmp);
    Bmp.Free;
    removeimpurity.Enabled :=true;
end;

procedure edge(Bmp:Tbitmap);
var
   b0, b1: Tbitmap;
   i, j,k: Integer;
   p1, p2, p3, p: pbyteArray;
begin
   b0 :=Tbitmap.Create;
   b0.Assign (Bmp);
   b1 :=Tbitmap.Create ;
   b1.Assign (Bmp);
   b0.PixelFormat := pf24bit;
   b1.PixelFormat := pf24bit;
   for i := 1 to b0.Height - 2 do
   begin
      p1 := b0.ScanLine[i - 1];
      p2 := b0.ScanLine[i];
      p3 := b0.ScanLine[i + 1];
      p := b1.ScanLine[i];   //提取后的像素
      for j := 1 to  b0.Width - 2 do
      begin
         if(p2[3 * j] = 0) and (p2[3 * j + 2] = 0) and (p2[3 * j + 1] = 0)  then
         begin

            if ((p2[3 * (j - 1) + 2] = 255) and (p2[3 * (j - 1) + 1] = 255) and(p2[3 * (j - 1)] = 255))or

               ((p2[3 * (j + 1) + 2] = 255) and (p2[3 * (j + 1) + 1] = 255) and(p2[3 * (j + 1)] = 255)) or

               ((p1[3 * (j + 1) + 2] = 255) and (p1[3 * (j + 1) + 1] = 255) and(p1[3 * (j + 1)] = 255)) or

               ((p1[3 * (j) + 2] = 255) and (p1[3 * (j) + 1] = 255) and(p1[3 * (j)] = 255)) or

               ((p1[3 * (j - 1) + 2] = 255) and (p1[3 * (j - 1) + 1] = 255) and(p1[3 * (j - 1)] = 255)) or

               ((p3[3 * (j - 1) + 2] = 255) and (p3[3 * (j - 1) + 1] = 255) and(p3[3 * (j - 1)] = 255)) or

               ((p3[3 * (j) + 2] = 255) and (p3[3 * (j) + 1] = 255) and (p3[3 * (j)]= 255)) or

               ((p3[3 * (j + 1) + 2] = 255) and (p3[3 * (j + 1) + 1] = 255) and(p3[3 * (j + 1)] = 255) )then
            begin
               p[3 * j + 2] := 255; //置白色像素
               p[3 * j + 1] := 255;
               p[3 * j] := 255;
            end;
         end;
       end;
      end;

       p1:= b0.ScanLine[0];
       p2:= b0.scanline[1];
       p:= b1.ScanLine[0];
  for i:=1 to (b0.Width-2)  do
       begin
         if ((p1[3*i]=0) and (p1[3*i]=0) and (p1[3*i]=0))then
         begin
           if ((p1[3*(i-1)]=255) and (p1[3*(i-1)+1]=255) and (p1[3*(i-1)+2]=255))
           or ((p1[3*(i+1)]=255) and (p1[3*(i+1)+1]=255) and (p1[3*(i+1)+2]=255))
           or ((p2[3*(i-1)]=255) and (p2[3*(i-1)+1]=255) and (p2[3*(i-1)+2]=255))
           or ((p2[3*i]=255) and  (p2[3*i+1]=255) and (p2[3*i+2]=255))
           or ((p2[3*(i+1)]=255) and (p2[3*(i+1)+1]=255) and (p2[3*(i+1)+2]=255))
           then  begin
                  p[3*i]:=255;
                  p[3*i+1]:=255;
                  p[3*i+2]:=255;
                  end;
          end;
       end;
   for k := 1 to b0.Height - 2 do
   begin
      p1 := b0.ScanLine[k - 1];
      p2 := b0.ScanLine[k];
      p3 := b0.ScanLine[k + 1];
      p := b1.ScanLine[k];   //提取后的像素
      i:=0; j:=b0.Width -1;
      begin
         if ((p2[3 * i] = 0) and (p2[3 * i+1] = 0) and (p2[3 * i+2] = 0)) then
         begin
           if ((p1[3*i]=255) and (p1[3*i+1]=255) and (p1[3*i+2]=255))
           or ((p1[3*(i+1)]=255) and (p1[3*(i+1)+1]=255) and (p1[3*(i+1)+2]=255))
           or ((p2[3*(i+1)]=255) and (p2[3*(i+1)+1]=255) and (p2[3*(i+1)+2]=255))
           or ((p3[3*(i+1)]=255) and (p3[3*(i+1)+1]=255) and (p3[3*(i+1)+2]=255))
           or ((p3[3*i]=255) and (p3[3*i+1]=255) and (p3[3*i+2]=255))
           then  begin
                 p[3 * i] := 255 ;
                 p[3 * i+1] := 255;

⌨️ 快捷键说明

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