二值化:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ExtDlgs, StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Image1: TImage;Image2: TImage;Button1: TButton;Button2: TButton;OpenPictureDialog1: TOpenPictureDialog;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementationuses math;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);beginSelf.OpenPictureDialog1.Filter := '*.bmp|*.bmp';if self.OpenPictureDialog1.Execute thenbeginImage1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);end;end;procedure TForm1.Button2Click(Sender: TObject);varp: PByteArray;Gray, x, y: Integer;Bmp: TBitmap;beginBmp := TBitmap.Create;Bmp.Assign(Image1.Picture.Bitmap);//设置为24位真彩色Bmp.PixelFormat := pf24Bit;randomize;for y := 0 to Bmp.Height - 1 dobeginp := Bmp.scanline[y];for x := 0 to Bmp.Width - 1 dobegin//一个象素点三个字节Gray := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x* 3] * 0.11);if gray > 128 then //全局阀值128beginp[x * 3] := 255;p[x * 3 + 1] := 255;p[x * 3 + 2] := 255;endelsebeginp[x * 3] := 0;p[x * 3 + 1] := 0;p[x * 3 + 2] := 0;end;end;end;Image2.Picture.Bitmap.Assign(Bmp);Bmp.Free;end;end.锐化:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ExtDlgs;
type
TForm1 = class(TForm)
Image1: TImage;
Button2: TButton;
Image2: TImage;
Button1: TButton;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses math;
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var
bmp1, bmp2: Tbitmap;
p1, p2, p3, p4: pbytearray;
//定义四个pbytearray类型变量
i, j, z: integer;
y: array[0..8] of integer;
begin
y[0] := 0; y[1] := -1; y[2] := 0;
y[3] := -1; y[4] := 5; y[5] := -1;
y[6] := 0; y[5] := -1; y[8] := 0;
//卷积矩阵
z := 1;
//卷积核
bmp1 := Tbitmap.Create;
bmp2 := Tbitmap.Create;
bmp1.Assign(image1.Picture.Bitmap);
bmp1.PixelFormat := pf24bit;
//24为格式便于处理
bmp1.Width := image1.Picture.Graphic.Width;
bmp1.Height := image1.Picture.Graphic.Height;
bmp2.Assign(bmp1);
//备用的位图
bmp2.PixelFormat := pf24bit;
for j := 1 to bmp1.Height - 2 do
begin
p1 := bmp1.ScanLine[j];
//第一条扫描线
p2 := bmp2.ScanLine[j - 1];
//第二条扫描线,为了防止数据变化,在备用位图上操作
p3 := bmp2.ScanLine[j];
p4 := bmp2.ScanLine[j + 1];
//第三条扫描线
//三条相邻的扫描线
for i := 1 to bmp1.Width - 2 do
begin
//进行卷积操作获取新的象素值
p1[3 * i + 2] := min(255, max(0, ((y[0] * p2[3 * (i - 1) + 2]
+
y[1] * p2[3 * i + 2] + y[2] * p2[3 * (i + 1) + 2] + y[3]
* p3[3
* (i - 1)
+ 2] + y[4] * p3[3 * i + 2] + y[5] * p3[3 * (i + 1) +
2] +
y[6]
* p4[3
* (i - 1) + 2] + y[5] * p4[3 * i + 2] + y[8] * p4[3 * (i
+
1) + 2]))
div
z));
//重新算出红色分量
p1[3 * i + 1] := min(255, max(0, ((y[0] * p2[3 * (i - 1) + 1]
+
y[1] * p2[3 * i + 1] + y[2] * p2[3 * (i + 1) + 1] + y[3]
* p3[3
* (i - 1)
+ 1] + y[4] * p3[3 * i + 1] + y[5] * p3[3 * (i + 1) +
1] +
y[6]
* p4[3
* (i - 1) + 1] + y[5] * p4[3 * i + 1] + y[8] * p4[3 * (i
+
1) + 1]))
div
z));
//重新算出蓝色分量
p1[3 * i] := min(255, max(0, ((y[0] * p2[3 * (i - 1)] + y[1]
*
p2[3 * i] + y[2] * p2[3 * (i + 1)] + y[3] * p3[3 * (i -
1)] +
y[4] * p3[3
* i] + y[5] * p3[3 * (i + 1)] + y[6] * p4[3 * (i - 1)] +
y[5]
* p4[3 * i]
+ y[8] * p4[3 * (i + 1)])) div z));
//重新算出绿色分量
end;
end;
Image2.Picture.Bitmap.Assign(Bmp1);
//重新显示
Image2.Invalidate;
Bmp1.Free;
bmp2.Free;
//释放资源
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
if OpenPictureDialog1.Execute then
begin
Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;
end;
end.
中值滤波:unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ExtCtrls, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Image1: TImage;Image2: TImage;procedure Button1Click(Sender: TObject);procedure SelectionSort(var a: array of integer);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;RvalueArray, GvalueArray, BvalueArray: array[0..8] of integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);varbmp1, bmp2: Tbitmap;p1, p2, p3, p4: pbytearray;i, j: integer;begin//设置双缓冲self.DoubleBuffered := true;//创建两个位图实例bmp1 := Tbitmap.Create;bmp2 := Tbitmap.Create;//加在位图bmp1.Assign(image1.Picture.Bitmap);//设置位图的象素格式bmp1.PixelFormat := pf24bit;//位图的大小bmp1.Width := image1.Picture.Graphic.Width;bmp1.Height := image1.Picture.Graphic.Height;//加载备份的位图bmp2.Assign(image1.Picture.Bitmap);bmp2.PixelFormat := pf24bit;for j := 1 to bmp1.Height - 2 dobegin//三条扫描线p1 := bmp1.ScanLine[j];p2 := bmp2.ScanLine[j - 1];p3 := bmp2.ScanLine[j];p4 := bmp2.ScanLine[j + 1];for i := 1 to bmp1.Width - 2 dobegin//对存储9个R分量的数组进行赋值RvalueArray[0] := p2[3 * (i - 1) + 2];RvalueArray[1] := p2[3 * i + 2];RvalueArray[2] := p2[3 * (i + 1) + 2];RvalueArray[3] := p3[3 * (i - 1) + 2];RvalueArray[4] := p3[3 * i + 2];RvalueArray[5] := p3[3 * (i + 1) + 2];RvalueArray[6] := p4[3 * (i - 1) + 2];RvalueArray[7] := p4[3 * i + 2];RvalueArray[8] := p4[3 * (i + 1) + 2];//调用排序过程SelectionSort(RvalueArray);//获取R分量的中间值p1[3 * i + 2] := RvalueArray[4];//对存储9个G分量的数组进行赋值GvalueArray[0] := p2[3 * (i - 1) + 1];GvalueArray[1] := p2[3 * i + 1];GvalueArray[2] := p2[3 * (i + 1) + 1];GvalueArray[3] := p3[3 * (i - 1) + 1];GvalueArray[4] := p3[3 * i + 1];GvalueArray[5] := p3[3 * (i + 1) + 1];GvalueArray[6] := p4[3 * (i - 1) + 1];GvalueArray[7] := p4[3 * i + 1];GvalueArray[8] := p4[3 * (i + 1) + 1];//调用选择排序SelectionSort(RvalueArray);//获取G分量的中间值p1[3 * i + 1] := RvalueArray[4];//对存储9个B分量的数组进行赋值BvalueArray[0] := p2[3 * (i - 1)];BvalueArray[1] := p2[3 * i];BvalueArray[2] := p2[3 * (i + 1)];BvalueArray[3] := p3[3 * (i - 1)];BvalueArray[4] := p3[3 * i];BvalueArray[5] := p3[3 * (i + 1)];BvalueArray[6] := p4[3 * (i - 1)];BvalueArray[7] := p4[3 * i];BvalueArray[8] := p4[3 * (i + 1)];//调用选择排序过程SelectionSort(RvalueArray);//获取G分量的中间值p1[3 * i] := RvalueArray[4];end;end;Image2.Picture.Bitmap.Assign(Bmp1);Bmp1.Free;bmp2.Free;end;//选择排序过程procedure TForm1.SelectionSort(var a: array of integer);vari, j, t: integer;beginfor i := low(a) to high(a) - 1 dofor j := high(a) downto i + 1 doif a > a[j] then
begin
//交换值(a, a[j], i, j);
t := a;
a := a[j];
a[j] := t;
end;
end;
end.
伪彩色增强:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ExtDlgs;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
OpenPictureDialog1: TOpenPictureDialog;
Image2: TImage;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Img: array of array of integer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
bmp: Tbitmap;
//位图对象
Gray, maxgray: integer;
i, j: integer;
p1, p2: pbytearray;
const
ColorTable: array[0..15] of integer = ($00000000, $00550000, $00005500,
$00000055, $003F3F3F, $00550055, $00FF0000, $00005555, $0000FF00,
$000000FF,
$00808080, $00FFFF00, $0000FFFF, $00FFFFFF, $00555500, $00FF00FF);
//16种颜色得颜色对照表
begin
bmp := Tbitmap.Create;
//创建位图实例
bmp.Assign(Image1.Picture.Bitmap);
bmp.PixelFormat := pf24bit;
//设为24位
Setlength(Img, bmp.Height, bmp.Width);
//设置动二维态数组得维数
for i := 0 to bmp.Height - 1 do
begin
p1 := bmp.ScanLine;
//每一行扫描线
for j := 0 to bmp.Width - 1 do
begin
//算出该象素的灰度
Img[j] := Round(0.3 * p1[3 * j + 2] + 0.59 *
p1[3 * j + 1] + 0.11 * p1[3 * j]);
end;
end;
maxGray := Img[0][0];
//初始化maxGray
for i := 0 to high(Img) do
begin
for j := 0 to high(Img[0]) do
begin
if maxgray < Img[j] then
begin
maxgray := Img[j];
//算出最大灰度值
end;
end;
end;
//转为16级灰度
for i := 0 to bmp.Height - 1 do
begin
p2 := bmp.ScanLine;
for j := 0 to bmp.Width - 1 do
begin
Gray := 16 * Img[j] div maxgray;
//灰度级的转化
p2[3 * j + 2] := GetRvalue(ColorTable[Gray]);
p2[3 * j + 1] := GetGvalue(ColorTable[Gray]);
p2[3 * j] := GetBvalue(ColorTable[Gray]);
//对象素点重新进行赋值
end;
end;
Image2.Picture.Bitmap.Assign(bmp);
//显示效果
bmp.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
OpenPicturedialog1.Filter := '*.bmp|*.bmp'; //限制为位图格式
if OpenPicturedialog1.Execute then
begin
Image1.Picture.Bitmap.LoadFromFile(OpenPicturedialog1.FileName);
end;
end;
end.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。