Delphi圖象處理效果集,主要是提取通道圖像,比如變換紅原色通道、變換綠原色和藍色通道,還有一些水彩效果、失真效果、明暗效果等,代碼中關鍵部分已標注了中文說明,對學習圖像效果的處理有幫助。
01
unit
Unit1;
02
interface
03
uses
04
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
05
Dialogs, StdCtrls, ExtCtrls, jpeg, ComCtrls;
06
type
07
TForm1 =
class
(TForm)
08
Image1: TImage;
09
Image2: TImage;
10
Button1: TButton;
11
ListBox1: TListBox;
12
Label1: TLabel;
13
Button2: TButton;
14
SaveDialog1: TSaveDialog;
15
procedure
Button1Click(Sender: TObject);
16
procedure
Button2Click(Sender: TObject);
17
private
18
{ Private declarations }
19
public
20
{ Public declarations }
21
end
;
22
var
23
Form1: TForm1;
24
implementation
25
{$R *.dfm}
26
procedure
TForm1
.
Button1Click(Sender: TObject);
27
Var
i,j,newR,newG,newB:
Integer
;
28
begin
29
For
i:=
0
To
Image1
.
Width
Do
30
For
j:=
0
To
Image1
.
Height
Do
31
Begin
32
//新的紅原色
33
newR:=Round(GetRValue(Image1
.
Canvas
.
Pixels[i-
1
,j-
1
])+GetRValue(Image1
.
Canvas
.
Pixels[i-
1
,j])+GetRValue(Image1
.
Canvas
.
Pixels[i-
1
,j+
1
])
34
+GetRValue(Image1
.
Canvas
.
Pixels[i,j-
1
])+GetRValue(Image1
.
Canvas
.
Pixels[i,j])+GetRValue(Image1
.
Canvas
.
Pixels[i,j+
1
])+
35
GetRValue(Image1
.
Canvas
.
Pixels[i+
1
,j-
1
])+GetRValue(Image1
.
Canvas
.
Pixels[i+
1
,j])+GetRValue(Image1
.
Canvas
.
Pixels[i+
1
,j+
1
])/
9
);
36
//新的綠原色
37
newG:=Round(GetGValue(Image1
.
Canvas
.
Pixels[i-
1
,j-
1
])+GetGValue(Image1
.
Canvas
.
Pixels[i-
1
,j])+GetGValue(Image1
.
Canvas
.
Pixels[i-
1
,j+
1
])
38
+GetGValue(Image1
.
Canvas
.
Pixels[i,j-
1
])+GetGValue(Image1
.
Canvas
.
Pixels[i,j])+GetGValue(Image1
.
Canvas
.
Pixels[i,j+
1
])+
39
GetGValue(Image1
.
Canvas
.
Pixels[i+
1
,j-
1
])+GetGValue(Image1
.
Canvas
.
Pixels[i+
1
,j])+GetGValue(Image1
.
Canvas
.
Pixels[i+
1
,j+
1
])/
9
);
40
//新的藍原色
41
newB:=Round(GetBValue(Image1
.
Canvas
.
Pixels[i-
1
,j-
1
])+GetBValue(Image1
.
Canvas
.
Pixels[i-
1
,j])+GetBValue(Image1
.
Canvas
.
Pixels[i-
1
,j+
1
])
42
+GetBValue(Image1
.
Canvas
.
Pixels[i,j-
1
])+GetBValue(Image1
.
Canvas
.
Pixels[i,j])+GetBValue(Image1
.
Canvas
.
Pixels[i,j+
1
])+
43
GetBValue(Image1
.
Canvas
.
Pixels[i+
1
,j-
1
])+GetBValue(Image1
.
Canvas
.
Pixels[i+
1
,j])+GetBValue(Image1
.
Canvas
.
Pixels[i+
1
,j+
1
])/
9
);
44
Case
ListBox1
.
ItemIndex
Of
45
0
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
46
255
,
//變換紅原色通道
47
GetGValue(Image1
.
Canvas
.
Pixels[i,j]),
48
GetBValue(Image1
.
Canvas
.
Pixels[i,j]));
49
1
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
50
GetRValue(Image1
.
Canvas
.
Pixels[i,j]),
51
255
,
//變換綠原色通道
52
GetBValue(Image1
.
Canvas
.
Pixels[i,j]));
53
2
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
54
GetRValue(Image1
.
Canvas
.
Pixels[i,j]),
55
GetGValue(Image1
.
Canvas
.
Pixels[i,j]),
56
255
);
//變換藍原色通道
57
3
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//水彩效果
58
GetRValue(newR),
59
GetGValue(Image1
.
Canvas
.
Pixels[i,j]),
60
GetBValue(Image1
.
Canvas
.
Pixels[i,j]));
61
4
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//紫色通道
62
GetRValue(Image1
.
Canvas
.
Pixels[i,j]),
63
GetGValue(newG),
64
GetBValue(Image1
.
Canvas
.
Pixels[i,j]));
65
5
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//黃色通道
66
GetRValue(Image1
.
Canvas
.
Pixels[i,j]),
67
GetGValue(Image1
.
Canvas
.
Pixels[i,j]),
68
GetBValue(newB));
69
6
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//失真效果
70
GetRValue(newR),
71
GetGValue(newG),
72
GetBValue(newB));
73
7
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//柔化效果
74
GetRValue(Image1
.
Canvas
.
Pixels[i-
2
,j-
2
]),
75
GetGValue(Image1
.
Canvas
.
Pixels[i,j]),
76
GetBValue(Image1
.
Canvas
.
Pixels[i+
2
,j+
2
]));
77
8
:Image2
.
Canvas
.
Pixels[i,j]:=RGB(
//明暗效果
78
Round(GetGValue(Image1
.
Canvas
.
Pixels[i,j])/
4
),
//變換紅原色通道
79
Round(GetGValue(Image1
.
Canvas
.
Pixels[i,j])/
4
),
80
Round(GetBValue(Image1
.
Canvas
.
Pixels[i,j])/
4
));
81
end
;
82
end
;
83
End
;
84
procedure
TForm1
.
Button2Click(Sender: TObject);
85
begin
86
If
SaveDialog1
.
Execute
Then
87
Image2
.
Picture
.
SaveToFile(SaveDialog1
.
FileName);
//保存效果圖
88
end
;
89
end
.
程序運行效果圖: