📄 childwin.~pas
字号:
end;
procedure TMDIChild.GrayClick(Sender: TObject);
var
p: pbyteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: Integer;
begin
Bmp := TBitmap.Create;
Bmp.Assign(BloodImage.Picture.Bitmap);
Bmp.PixelFormat := pf24bit; //转为24位真彩色处理
for y := 0 to Bmp.Height - 1 do
begin
//获取每一行像素信息
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
//这里采用方法三
//即 Y=0.3R+059G+0.11B
Gray := Round(p[3 * x + 2] * 0.3 + p[3 * x + 1] * 0.59
+ p[3 * x] * 0.11);
//由于是24位真彩色,故一个像素点为三个字节
p[3 * x + 2] := byte(Gray);
p[3 * x + 1] := byte(Gray);
p[3 * x] := byte(Gray);
end;
end;
BloodImage.Picture.Bitmap := Bmp;
Bmp.Free;
end;
procedure TMDIChild.N6Click(Sender: TObject);
var
Bmp: Tbitmap; // 建立临时位图
p: pByteArray;
w,h,c,x,y: Integer;
begin
Bmp := Tbitmap.Create;
Bmp.Assign(BloodImage.Picture.Bitmap);
w:= Bmp.Width ;
H:= Bmp.Height ;
withdrawBmp.Assign(BloodImage.Picture.Bitmap);
withdraw.Enabled :=true;
Bmp.PixelFormat := pf24bit; //设置位图格式
for y := 0 to Bmp.Height - 1 do t[y]:=Bmp.ScanLine[y];
c:=0;
for y := 0 to Bmp.Height -1 do
begin
p:= Bmp.ScanLine[y];
for x := 0 to Bmp.Width -1 do
begin
if (p[3*x]=255) and (p[3*x+1]=255) and (p[3*x+2]=255)then //clwhite;
begin
connect(w,h,c,x,y);
c:=c+1;
end;
end;
end;
for y := 0 to Bmp.Height - 1 do
begin
p:= Bmp.ScanLine[y];
for x := 0 to Bmp.Width - 1 do
begin
if ((p[3*x]=0) and (p[3*x+1]=0) and (p[3*x+2]=255))then
begin
p[3*x]:=255 ; p[3*x+1]:=255; p[3*x+2]:=255;
end;
end;
end;
BloodImage.Picture.Bitmap.Assign(Bmp) ;
Bmp.Free;
withdraw.Enabled :=true;
end;
procedure TMDIChild.FilterClick(Sender: TObject);
var
Bmp1, Bmp2: Tbitmap;
p1, p2, p3, p4: pbytearray;
i, j,num: Integer;
begin
self.DoubleBuffered := true;//设置双缓冲
Bmp1 := Tbitmap.Create;
Bmp2 := Tbitmap.Create;
Bmp1.Assign(BloodImage.Picture.Bitmap); //加载位图
Bmp1.PixelFormat := pf24bit; //设置位图的像素格式
//位图的大小
Bmp1.Width := BloodImage.Picture.Graphic.Width;
Bmp1.Height := BloodImage.Picture.Graphic.Height;
//加载备份的位图
Bmp2.Assign(BloodImage.Picture.Bitmap);
Bmp2.PixelFormat := pf24bit;
for num:=1 to 3 do
begin
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(GvalueArray);
//获取G分量的中间值
p1[3 * i + 1] := GvalueArray[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(BvalueArray);
//获取G分量的中间值
p1[3 * i] := BvalueArray[4];
end;
end;
end;
BloodImage.Picture.Bitmap := Bmp1;
Bmp1.Free;
Bmp2.Free;
end;
procedure TMDIChild.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[i] > a[j] then
begin //交换值(a[i], a[j], i, j);
t := a[i];
a[i] := a[j];
a[j] := t;
end;
end;
procedure TMDIChild.GrayStrechClick(Sender: TObject);
var
p: PByteArray;
x, y: Integer;
Bmp1: TBitmap;
Gray: byte;
ScaleFactor: real;
scanlinebytes: Integer;
begin
Bmp1 := TBitmap.Create;
Bmp1.Assign(BloodImage.Picture.Bitmap);
Bmp1.PixelFormat := pf24bit;
GetParam(Bmp1);
p := Bmp1.scanline[0];
ScaleFactor := 255/(OriginalRangeRight - OriginalRangeLeft); //拉伸比例
scanlinebytes := Integer(Bmp1.scanline[1]) - Integer(Bmp1.scanline[0]);
//扫描线间距
for y := 0 to Bmp1.Height - 1 do
begin
for x := 0 to Bmp1.Width - 1 do
begin
Gray := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x
* 3] * 0.11);
Gray := Round(ScaleFactor * (Gray - OriginalRangeLeft));
//进行灰度拉伸
p[x * 3 + 2] := Gray; p[x * 3 + 1] := Gray; p[x * 3] := Gray;
//重新赋值
end;
inc(Integer(p), scanlinebytes);
end;
BloodImage.Picture.Bitmap := Bmp1;
Bmp1.Free;
end;
procedure TMDIChild.GetParam(Bmp:TBitmap);
var
p: PByteArray;
x, y, i, j: Integer;
Bmp1: TBitmap;
Gray: byte;
scanlinebytes: Integer; //扫描线间距
begin
Bmp1 := TBitmap.Create; //创建实例
Bmp1.Assign(Bmp);
Bmp1.PixelFormat := pf24Bit; //24bit位图
p := Bmp1.scanline[0]; //首行扫描线信息
for i := 0 to 255 do
begin
GrayClass[i] := 0;
//初始化数组为0
end;
scanlinebytes := Integer(Bmp1.scanline[1]) - Integer(Bmp1.scanline[0]);
for y := 0 to Bmp1.Height - 1 do
begin
//注意边界,不能越界
for x := 0 to Bmp1.Width - 1 do
begin
Gray := Round(p[x * 3 + 2] * 0.3 + p[x * 3 + 1] * 0.59 + p[x* 3]* 0.11);
//求取灰度值
for i := 0 to 255 do
begin
if Gray = i then
begin
GrayClass[i] := GrayClass[i] + 1; //每级灰度像素点数
end;
end;
end; //指针增加,增加得其实是一个负值
inc(Integer(p), scanlinebytes);
end;
Bmp1.Free;
//释放资源
for i := 0 to 255 do
begin
if GrayClass[i] <> 0 then
begin
OriginalRangeLeft := i;
break;
//获取最大灰度级
end;
end;
for j := 255 downto 0 do
begin
if GrayClass[j] <> 0 then
begin
OriginalRangeRight := j;
break;
//获取最小灰度级
end;
end;
end;
procedure TMDIChild.ContrastClick(Sender: TObject);
var
p: PByteArray;
x, y: Integer;
Bmp1: TBitmap;
begin
Bmp1 := TBitmap.Create;
Bmp1.Assign(BloodImage.Picture.Bitmap);
Bmp1.PixelFormat := pf24bit;
for y := 0 to Bmp1.Height - 1 do
begin
p := Bmp1.scanline[y];
for x := 0 to Bmp1.Width - 1 do
begin //确定阀值为200
if (p[x * 3] > 200) and (p[x * 3] < 236)
and (p[x * 3 + 1] > 200) and (p[x * 3 + 1] < 236)
and (p[x * 3 + 2] > 200) and (p[x * 3 + 2] < 236) then
begin
p[x * 3] := min(255,(p[x * 3] + 20));
p[x * 3 + 1] :=min(255,(p[x * 3 + 1] + 20));
p[x * 3 + 2] :=min(255, (p[x * 3 + 2] + 20));
end;
if (p[x * 3] > 20) and (p[x * 3] < 200)
and (p[x * 3 + 1] > 20) and (p[x *3 + 1] <200)
and (p[x * 3 + 2] > 20) and (p[x * 3 + 2] < 200) then
begin
p[x * 3] := max(0,(p[x * 3] - 20));
p[x * 3 + 1] :=max(0, (p[x * 3 + 1] - 20));
p[x * 3 + 2] := max(0,(p[x * 3 + 2] - 20));
end;
end;
end;
BloodImage.Picture.Bitmap := Bmp1;
Bmp1.Free;
end;
procedure TMDIChild.StrengthSaturationClick(Sender: TObject);
var
x, y, ScanlineBytes: Integer;
p: prgbtriplearray;
RVALUE, bvalue, gvalue: Integer;
hVALUE, sVALUE, lVALUE: Integer;
Bmp1: TBitmap;
begin
self.DoubleBuffered := true; //设置双缓冲
Bmp1 := TBitMap.Create;
Bmp1.Assign(BloodImage.Picture.Bitmap);
Bmp1.PixelFormat := pf24bit;//指定为24位位图
p := Bmp1.ScanLine[0];
ScanlineBytes := Integer(Bmp1.ScanLine[1]) - Integer(Bmp1.ScanLine[0]);
for y := 0 to Bmp1.Height - 1 do
begin
for x := 0 to Bmp1.Width - 1 do
begin //获取RGB的三个分量值,并进行赋值
RVALUE := p[x].rgbtRed;
GVALUE := p[x].rgbtGreen;
BVALUE := p[x].rgbtBlue;
// 调用RGB转HSL过程,获取HSL三个分量值
RGB_to_HSL(RVALUE, gVALUE, bVALUE, hVALUE, sVALUE, lVALUE);
//饱和度值加20
Svalue:=Svalue+20;
Svalue := Min(100, Svalue);
// 调用HSL转RGB程,获取RGB三个分量值
HSL_to_RGB(hVALUE, sVALUE, lVALUE, rVALUE, gVALUE, bVALUE);
p[x].rgbtRed := RVALUE;
p[x].rgbtGreen := GVALUE;
p[x].rgbtBlue := BVALUE;
end;
inc(Integer(p), ScanlineBytes); //指针递增
end;
BloodImage.Picture.Bitmap := Bmp1;
Bmp1.Free;
end;
////////////////////////////////////////////////////////////////////////
procedure TMDIChild.HSL_to_RGB(H, S, L: Integer; var R, G, B: Integer);
var
Sat, Lum: Double;
begin
R := 0; G := 0; B := 0;
if (H < 360) and (H >= 0) and (S <= 100) and
(S >= 0) and (L <= 100) and (L>=0) then
begin
if H <= 60 then
begin
R := 255;
G := Round((255 / 60) * H);
B := 0;
end
else if H <= 120 then
begin
R := Round(255 - (255 / 60) * (H - 60));
G := 255;
B := 0;
end
else if H <= 180 then
begin
R := 0;
G := 255;
B := Round((255 / 60) * (H - 120));
end
else if H <= 240 then
begin
R := 0;
G := Round(255 - (255 / 60) * (H - 180));
B := 255;
end
else if H <= 300 then
begin
R := Round((255 / 60) * (H - 240));
G := 0;
B := 255;
end
else if H < 360 then
begin
R := 255;
G := 0;
B := Round(255 - (255 / 60) * (H - 300));
end;
Sat := Abs((S - 100) / 100);
R := Round(R - ((R - 128) * Sat));
G := Round(G - ((G - 128) * Sat));
B := Round(B - ((B - 128) * Sat));
Lum := (L - 50) / 50;
if Lum > 0 then
begin
R := Round(R + ((255 - R) * Lum));
G := Round(G + ((255 - G) * Lum));
B := Round(B + ((255 - B) * Lum));
end
else if Lum < 0 then
begin
R := Round(R + (R * Lum));
G := Round(G + (G * Lum));
B := Round(B + (B * Lum));
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -