สอนแลป Delphi สดๆ

โดย มิสเตอร์สะตอฯ เมื่อ 25 มกราคม 2009 เวลา 3:00 (เย็น) ในหมวดหมู่ การศึกษา #
อ่าน: 3950

สวัสดีครับทุกท่าน

ทดลองสอนแลปเดลไฟผ่านเว็บลานปัญญานะครับ

สิ่งที่จะเห็นต่อไปนี้เป็นเพียงโคดภาษาเดลไฟนะครับ…..

unit Unit1;

interface

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

type
//------ Fast Declaration of Scanline -----//
  TRGB32 = packed record
     B, G, R, A : Byte;
  //     R,G,B,Y,A : Byte;
  end;
  TRGB32Array = packed array[0..MaxInt div SizeOf(TRGB32)-1] of TRGB32;
  PRGB32Array = ^TRGB32Array;
//—————————————–//

  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    OpenPictureDialog1: TOpenPictureDialog;
    Label1: TLabel;
    Image2: TImage;
    Button2: TButton;
    SpinEdit1: TSpinEdit;
    Label2: TLabel;
    Label3: TLabel;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Button2Click(Sender: TObject);
    procedure SpinEdit1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
     If OpenPictureDialog1.Execute Then
        Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
Var
    RR, GG, BB, YY : Byte;
begin
     RR := GetRValue(Image1.Canvas.Pixels[X,Y]);
     GG := GetGValue(Image1.Canvas.Pixels[X,Y]);
     BB := GetBValue(Image1.Canvas.Pixels[X,Y]);
     YY := Round(0.299*RR + 0.587*GG + 0.114*BB);  // Gray-Scale
     Label1.Caption := ‘Position(’+IntToStr(X)+’,'+IntToStr(Y)+’) = (’
          + IntToStr(RR) + ‘,’ + InttoStr(GG)+’,'+IntToStr(BB) + ‘,’ +
            InttoStr(YY) + ‘)’;
     Label1.Refresh;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
     i,j : integer;
     RR, GG, BB, YY, CutValue : Byte;
     CountLeafPixel : Integer;
begin
     Image2.Width := Image1.Width;
     Image2.Height:= Image1.Height;
     CountLeafPixel := 0;
     CutValue := SpinEdit1.Value;
     Image2.Canvas.Brush.Color := clWhite;
     Image2.Canvas.Rectangle(0,0,Image2.Width,Image2.Height);
     For J:=1 to Image1.Height Do
       For I:= 1 to Image1.Width Do
       Begin
         RR := GetRValue(Image1.Canvas.Pixels[i,j]);
         GG := GetGValue(Image1.Canvas.Pixels[i,j]);
         BB := GetBValue(Image1.Canvas.Pixels[i,j]);
         YY := Round(0.299*RR + 0.587*GG + 0.114*BB);  // Gray-Scale
         If YY < CutValue then
         begin
            CountLeafPixel := CountLeafPixel + 1;
            Image2.Canvas.Pixels[i,j] := RGB(0,255,0);//  RGB(YY,YY,YY);
         end;
       End;
     //ShowMessage(’Amount of Leaf Pixels = ‘ + IntToStr(CountLeafPixel));
     Label3.Caption := ‘Amount of Leaf Pixels = ‘ + IntToStr(CountLeafPixel);
     Label3.Refresh;
     Image2.Refresh;
end;

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
    //  Button2Click(Sender);
        Button3Click(Sender);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
     i,j : integer;
     RR, GG, BB, YY, CutValue : Byte;
     CountLeafPixel : Integer;
     Line1, Line2 : PRGB32Array; // For Scanline Uses - Fast Computation;
begin
     CountLeafPixel := 0;
     CutValue := SpinEdit1.Value;
   //—– Scanline Initialization ——-//
     with Image1.Picture.Bitmap do
     begin
          PixelFormat := pf32bit;
          Width := Image1.Width;
          Height:= Image1.Height;
     end;
     with Image2.Picture.Bitmap do
     begin
          PixelFormat := pf32bit;
          Width := Image1.Width;
          Height:= Image1.Height;
     end;
   //————————————-//
     Image2.Width := Image1.Width;
     Image2.Height:= Image1.Height;
     Image2.Canvas.Brush.Color := clWhite;
     Image2.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);

     For J:=0 to Image2.Height-1 Do
     Begin
       Line1 := Image1.Picture.Bitmap.ScanLine[J];
       Line2 := Image2.Picture.Bitmap.ScanLine[J];
       For I:= 0 to Image2.Width-1 Do
       Begin
       {
         RR := GetRValue(Image1.Canvas.Pixels[i,j]);
         GG := GetGValue(Image1.Canvas.Pixels[i,j]);
         BB := GetBValue(Image1.Canvas.Pixels[i,j]);
       }

         RR := Line1[i].B;
         GG := Line1[i].G;
         BB := Line1[i].R;
         YY := Line1[i].A;

       //  YY := Round(0.299*Line1[i].R + 0.587*Line1[i].G + 0.114*Line1[i].B);  // Gray-Scale
         YY := Round(0.299*RR + 0.587*GG + 0.114*BB);  // Gray-Scale
         If YY < CutValue then
         begin
            CountLeafPixel := CountLeafPixel + 1;
         //   Image2.Canvas.Pixels[i,j] := RGB(0,255,0);//  RGB(YY,YY,YY);
            Line2[I].B := 0;
            Line2[I].G := 255;
            Line2[I].R := 0;
            Line2[I].A := 255;
         end; // if YY
       End; // For I
     End; // For J
     //ShowMessage(’Amount of Leaf Pixels = ‘ + IntToStr(CountLeafPixel));
     Label3.Caption := ‘Amount of Leaf Pixels = ‘ + IntToStr(CountLeafPixel);
     Label3.Refresh;
//     Image2.Refresh;
     Image2.Invalidate;

//     Showmessage(’Image1 W = ‘ + inttostr(Image1.Width) +’, H = ‘ + inttostr(Image1.Height)
//      +#13#10 +’Image2 W = ‘ + inttostr(Image2.Width) +’, H = ‘ + inttostr(Image2.Height));
end;

End.

« « Prev : เขื่อนแตก น้ำท่วม (Water flooding)

Next : พืชวัตถุอันตราย…พืชโดนจำคุก…ความผิดของพืชสมุนไพรไทยหรือ? » »


ผู้ใช้ Facebook สามารถให้ความเห็นที่นี่ได้ โดยกด Like เพื่อแสดงตัว

9 ความคิดเห็น


แสดงความคิดเห็น

ท่านอยากจะเข้าระบบหรือไม่


*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Main: 0.13059306144714 sec
Sidebar: 0.050740957260132 sec