分享

求含有锐化(最好还有二值化、滤波、增强)等功能的delphi图像处理的原代码(100分) | Delphi论坛 | Delphi Forum

 delphi_笔记 2018-09-21
二值化:

unit Unit1;

interface

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

type
TForm1 = 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;

var
Form1: TForm1;

implementation
uses math;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
if self.OpenPictureDialog1.Execute then
begin
Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
p: PByteArray;
Gray, x, y: Integer;
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Image1.Picture.Bitmap);
//设置为24位真彩色
Bmp.PixelFormat := pf24Bit;
randomize;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
//一个象素点三个字节
Gray := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
* 3] * 0.11);
if gray > 128 then //全局阀值128
begin
p[x * 3] := 255;
p[x * 3 + 1] := 255;
p[x * 3 + 2] := 255;
end
else
begin
p[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;

interface

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

type
TForm1 = 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;

var
Form1: TForm1;
RvalueArray, GvalueArray, BvalueArray: array[0..8] of integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
bmp1, 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 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
//对存储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);
var
i, j, t: integer;
begin
for i := low(a) to high(a) - 1 do
for j := high(a) downto i + 1 do
if 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.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多