图像干扰处理1

来源:互联网 发布:蘑菇中毒知乎 编辑:程序博客网 时间:2024/04/30 07:55

图像识别是一个复杂的过程,分别要经过图像处理、图像分割、图像校正、图像识别等过程
其中第一步就是去掉干扰
以下是itpub.net中的图灵    经过处理后的样子
                
代码如下
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Bmp: TBitmap;
  Color: TColor;
  x, y: Integer;
begin
  Bmp := TBitmap.Create;
  Bmp.Assign(Image1.Picture.Graphic);
  Bmp.PixelFormat := pf4bit;
  Color := BTColorIndex(Bmp, 2);
  for x := 0 to Bmp.Width - 1 do for y := 0 to Bmp.Height - 1 do
  begin
    if Bmp.Canvas.Pixels[x, y] = Color then Bmp.Canvas.Pixels[x, y] := ClBlack else Bmp.Canvas.Pixels[x, y] := ClWhite;
  end;
  Image2.Picture.Bitmap := Bmp;
end;

function BTColorIndex(Bmp: TBitmap; Index: Integer = 0): Integer;
  type
    TColorInfo = record
      Color: TColor;
      Count: Integer;
    end;
    pColorInfo = ^TColorInfo;
  function GetColorIndex(List: TList; Color: TColor): pColorInfo;
  var
    i: Integer;
  begin
    for i := 0 to List.Count - 1 do begin Result := List[i]; if Result.Color <> Color then Continue; Exit; end;
    Result := nil;
  end;
  function Compare(Item1, Item2: pColorInfo): Integer;
  begin
    Result := Item2.Count - Item1.Count;
  end;
var
  List: TList;
  x, y: Integer;
  pCI: pColorInfo;
  Color: TColor;
begin
  Result := -1;
  List := TList.Create;
  try
    Bmp.PixelFormat := pf24bit;
    for x := 0 to Bmp.Width - 1 do for y := 0 to Bmp.Height - 1 do
    begin
      Color := Bmp.Canvas.Pixels[x, y];
      pCI := GetColorIndex(List, Color);
      if pCI = nil then begin New(pCI); pCI.Color := Color; pCI.Count := 0; List.Add(pCI); end;
      Inc(pCI.Count);
    end;
    List.Sort(@Compare);
    if List.Count < Index + 1 then Exit;
    pCI := List[Index];
    Result := pCI.Color;
  finally
    while List.Count <> 0 do begin pCI := List[0]; Dispose(pCI); List.Delete(0); end;
    FreeAndNil(List);
  end;
end;

 

原创粉丝点击