颜色选取 - 权当给 supperment 的回复吧, 你的要求要用到"种子算法", 我暂时还没算明白.

颜色选取 - 权当给 supperment 的回复吧, 你的要求要用到"种子算法", 我暂时还没算明白.

本例效果图:

颜色选取 - 权当给 supperment 的回复吧, 你的要求要用到"种子算法", 我暂时还没算明白.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  TargetColor: TColor = clRed;
var
  SeedColor: TColor;
  x1,y1: Integer;
  pic: TPicture;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Image1.AutoSize := True;
  Image1.Picture.LoadFromFile('c:\temp\test.bmp');
  x1 := Image1.Left;
  y1 := Image1.Top;
  pic := Image1.Picture;
  Button1.Caption := '恢复';
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  w,h: Integer;
  ImgHdc: HDC;
begin
  ImgHdc := Image1.Canvas.Handle;
  SeedColor := GetPixel(ImgHdc, X,Y);

  for w := 0 to Image1.Width-1 do
  for h := 0 to Image1.Height-1 do
    if GetPixel(ImgHdc, w,h)=SeedColor then
      SetPixelV(Canvas.Handle, x1+w, y1+h, TargetColor);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Image1.Picture := pic;
end;

end.
posted on 2008-03-21 15:47  万一  阅读(2567)  评论(3)  编辑  收藏
上一篇:如何把几张相同大小的图片拼合在 TImage 中 - 回复 "jxjjljf" 的问题


下一篇:opencv-python处理图片的一些列操作