伪彩色增强:
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.