📄 childwin.~pas
字号:
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;
BeanImage.Picture.Bitmap:=Bmp2;
Bmp1.Free;
Bmp2.Free;
end;
procedure TMDIChild.TotalThresholdInColorClick(Sender: TObject);
var
p: PByteArray;
Gray, x, y: Integer;
Bmp: TBitmap;
Child:TMDIChild;
begin
Bmp := TBitmap.Create;
Bmp.Assign(datamodule.LoadedBmp);
Bmp.PixelFormat := pf24Bit; //设置为24位真彩色
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 >162 then //全局阀值162
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;
Child:=TMDIChild.Create(Application);
Child.Caption:= '全值阀值法(彩色图像)';
TMDIChild(Child).BeanImage.picture.bitmap:=Bmp;
Child.Width :=Child.BeanImage.picture.width+1;
Child.height:=Child.BeanImage.picture.height+1;
Bmp.Free;
Child.ErodeProcess.Enabled:=true;
Child.expand.Enabled:=true;
end;
procedure TMDIChild.HSL1Click(Sender: TObject);
begin
if application.MessageBox('请点击左键选取黄豆的一块颜色','系统提示',
MB_YesNo)=IDYes THEN
begin
mark2:=true ; BeanImage.Cursor :=crcross;
ProcessedBmp := TBitmap.Create;
ProcessedBmp.Assign(BeanImage.Picture.Bitmap);
end
else mark2:=false;
end;
procedure TMDIChild.GrayClick(Sender: TObject);
var
p: pbyteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: Integer;
begin
Bmp := TBitmap.Create;
Bmp.Assign(BeanImage.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;
BeanImage.Picture.Bitmap := Bmp;
Bmp.Free;
end;
procedure TMDIChild.FillHoleClick(Sender: TObject);
var
Bmp: Tbitmap; // 建立临时位图
p: pByteArray;
w,h,c,x,y: Integer;
begin
Bmp := Tbitmap.Create;
Bmp.Assign(BeanImage.Picture.Bitmap);
w:= Bmp.Width ;
H:= Bmp.Height ;
withdrawBmp.Assign(BeanImage.Picture.Bitmap);
withdraw.Enabled :=true;
Bmp.PixelFormat := pf24bit; //设置位图格式
for y := 0 to Bmp.Height - 1 do TempBmpPtr[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;
BeanImage.Picture.Bitmap.Assign(Bmp) ;
Bmp.Free;
withdraw.Enabled :=true;
end;
procedure TMDIChild.MiddleValueFilterClick(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(BeanImage.Picture.Bitmap); //加载位图
Bmp1.PixelFormat := pf24bit; //设置位图的像素格式
//位图的大小
Bmp1.Width := BeanImage.Picture.Graphic.Width;
Bmp1.Height := BeanImage.Picture.Graphic.Height;
//加载备份的位图
Bmp2.Assign(BeanImage.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;
BeanImage.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(BeanImage.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;
BeanImage.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(BeanImage.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;
BeanImage.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(BeanImage.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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -