📄 screenhistostretchgrays.pas
字号:
bmp1.PixelFormat := pf24bit;
//设置位图格式
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
r := min(255, max(0, ((-p2[3 * (i - 1) + 2] - 2 * p2[3 * i +
2] -p2[3 * (i+1) + 2] - 0 * p3[3 * (i - 1) + 2] +
0 * p3[3 * i + 2]- 0 * p3[3 * (i + 1)
+ 2] + p4[3 * (i - 1) + 2] + 2 * p4[3 * i + 2] +
p4[3 * (i+ 1)+ 2]))));
g := min(255, max(0, ((-p2[3 * (i - 1) + 1] - 2 * p2[3 * i +
1] - p2[3 * (i+ 1) + 1] - 0 * p3[3 * (i - 1) + 1] + 0 *
p3[3 * i + 1]- 0 * p3[3 * (i + 1)+ 1] + p4[3 * (i - 1) + 1]
+ 2 * p4[3 * i + 1] + p4[3 * (i+ 1)+ 1]))));
b := min(255, max(0, ((-p2[3 * (i - 1)] - 2 * p2[3 * i] - p2[3
* (i + 1)]- 0 * p3[3 * (i - 1)] + 0 * p3[3 * i] -
0 * p3[3 * (i + 1)] + p4[3 * (i - 1)]
+ 2 * p4[3 * i + 2] + p4[3 * (i + 1)]))));
p1[3 * i + 2] :=r;p1[3 * i + 1] :=g;p1[3 * i] :=b;
end;
end;
ImageHistoStretched.Picture.Bitmap.Assign(bmp1);// 读取已经利用水平sobel进行检测后的位图
bmp2.Free;
Bmp1.Free; //释放资源
end;
procedure TFormHistoStretchGrays.VMargeClick(Sender: TObject);
var
bmp1, bmp2: Tbitmap;
p1, p2: pbytearray;
//定义四个pbytearray类型变量
i, j: integer;
begin
bmp1 := Tbitmap.Create;
bmp2 := Tbitmap.Create;
bmp1.Assign(ImageHistoStretched.Picture.Bitmap);
bmp1.PixelFormat := pf24bit;
//24为格式便于处理
bmp1.Width := ImageHistoStretched.Picture.Graphic.Width;
bmp1.Height := ImageHistoStretched.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];
for i := 1 to bmp1.Width - 3 do
begin
p1[3*i]:=min(255,abs(p2[3*i]-p2[3*(i-1)])+abs(p2[3*(i+1)]-p2[3*i]));
p1[3*i+1]:=p1[3*i];
p1[3*i+2]:=p1[3*i]
end;
end;
ImageHistoStretched.Picture.Bitmap.Assign(Bmp1);
//重新显示
ImageHistoStretched.Invalidate;
Bmp1.Free;
bmp2.Free;
//释放资源
end;
procedure TFormHistoStretchGrays.FrameCloseClick(Sender: TObject);
begin
PictureTwoValue(ImageHistoStretched.Picture.Bitmap,TwoValueTrackBar.Position+128);
if (BitmapDilate(self.ImageHistoStretched.Picture.Bitmap, False)) then
begin
ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
end
else
showmessage('膨胀失败');
if (BitmapErose(self.ImageHistoStretched.Picture.Bitmap, True)) then
begin
ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
end
else
showmessage('腐蚀失败');
end;
procedure TFormHistoStretchGrays.CopyMe(Tobmp: TBitmap; Frombmp: TBitmap);
begin
Tobmp.Width := Frombmp.Width;
Tobmp.Height := Frombmp.Height;
// 原位图的高度和宽度赋值给新位图
Tobmp.PixelFormat := pf24bit;
// 设置位图象格式为pf24bit
Tobmp.Assign(frombmp);
end; //Copy any TGraphic to a 24-bit TBitmap
procedure TFormHistoStretchGrays.HoughClick(Sender: TObject);
var
ray: array[0..8] of integer; // Filter setting array
z: word;
OrigBMP: TBitmap;
begin
OrigBMP := TBitmap.Create;
// 动态创建位图
CopyMe(OrigBMP, ImageHistoStretched.Picture.Bitmap);
// 复制位图的函数
ray[0] := -1;
ray[1] := 0;
ray[2] := 1;
ray[3] := -1;
ray[4] := 0;
ray[5] := 1;
ray[6] := -1;
ray[7] := 0;
ray[8] := 1;
z := 1;
// 模板参数
Convolve(ray, z, OrigBMP);
//通用处理函数
ImageHistoStretched.Picture.Bitmap.Assign(OrigBMP);
OrigBMP.Free;
// 释放位图象
end;
procedure TFormHistoStretchGrays.Convolve(ray: array of integer; z: word; aBmp: TBitmap);
var
O, T, C, B: pRGBArray; //scanlines
x, y: integer;
tBufr: TBitmap;
begin
tBufr := TBitmap.Create;
// 创建临时位图象
tBufr.Assign(aBmp);
// 拷贝图象
for x := 1 to aBmp.Height - 2 do
begin
O := aBmp.ScanLine[x]; //New Target(Original)
T := tBufr.ScanLine[x - 1]; //Old x-1 (Top)
C := tBufr.ScanLine[x]; //old x (Center)
B := tBufr.ScanLine[x + 1]; //old x+1 (Buttom)
for y := 1 to (tBufr.Width - 2) do //Walk pixels
begin
O[y].rgbtRed := max(0, min(255, ((T[y - 1].rgbtRed * ray[0]) +
(T[y].rgbtRed * ray[1]) +
(T[y + 1].rgbtRed * ray[2]) + (C[y - 1].rgbtRed * ray[3]) +
(C[y].rgbtRed * ray[4]) +
(C[y + 1].rgbtRed * ray[5]) + (B[y - 1].rgbtRed * ray[6]) +
(B[y].rgbtRed * ray[7]) +
(B[y + 1].rgbtRed * ray[8])) div z));
O[y].rgbtBlue := max(0, min(255, ((T[y - 1].rgbtBlue * ray[0]) +
(T[y].rgbtBlue * ray[1]) +
(T[y + 1].rgbtBlue * ray[2]) + (C[y - 1].rgbtBlue * ray[3]) +
(C[y].rgbtBlue * ray[4]) +
(C[y + 1].rgbtBlue * ray[5]) + (B[y - 1].rgbtRed * ray[6]) +
(B[y].rgbtBlue * ray[7]) +
(B[y + 1].rgbtBlue * ray[8])) div z));
O[y].rgbtGreen := max(0, min(255, ((T[y - 1].rgbtGreen * ray[0])
+ (T[y].rgbtGreen * ray[1]) +
(T[y + 1].rgbtGreen * ray[2]) + (C[y - 1].rgbtGreen * ray[3])
+ (C[y].rgbtGreen * ray[4]) +
(C[y + 1].rgbtGreen * ray[5]) + (B[y - 1].rgbtGreen * ray[6])
+ (B[y].rgbtGreen * ray[7]) +
(B[y + 1].rgbtGreen * ray[8])) div z));
end;
end;
tBufr.Free;
// 释放位图
end;
procedure TFormHistoStretchGrays.StretchClick(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(ImageHistoStretched.Picture.Bitmap);
bmp1.PixelFormat := pf24bit;
//24为格式便于处理
bmp1.Width := ImageHistoStretched.Picture.Graphic.Width;
bmp1.Height := ImageHistoStretched.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]:= p1[3 * i + 2];
p1[3*i]:=p1[3 * i + 2]
end;
end;
ImageHistoStretched.Picture.Bitmap.Assign(Bmp1);
//重新显示
ImageHistoStretched.Invalidate;
Bmp1.Free;
bmp2.Free;
//释放资源
end;
procedure TFormHistoStretchGrays.OpenPictureClick(Sender: TObject);
VAR
ColorCount: INTEGER;
Converted : BOOLEAN;
GrayCount : INTEGER;
i : INTEGER;
Intensity : INTEGER;
j : INTEGER;
Picture : TPicture;
row : pRGBTripleARray;
extension : string;
jpg:tjpegimage;
bitmap:tbitmap;
begin
plateImg.Picture.Assign(nil);
IF OpenPictureDialog.Execute
THEN BEGIN
Screen.Cursor := crHourGlass;
if fileexists(OpenPictureDialog.Filename) then
extension:=uppercase(copy( OpenPictureDialog.Filename,length(OpenPictureDialog.Filename)-2,3));
if(extension='BMP') THEN
OroginalColorImage.Picture.LoadFromFile(OpenPictureDialog.Filename);
if(extension='JPG')OR(extension='JPEG') then
TRY
jpg:=tjpegimage.Create;
jpg.LoadFromFile(OpenPictureDialog.Filename);
Bitmap := TBitmap.Create;
Bitmap.Width := jpg.Width;
Bitmap.Height := jpg.Height;
Bitmap.PixelFormat := pf24Bit;
Bitmap.Canvas.Draw(0,0, jpg);
OroginalColorImage.Picture.Graphic := Bitmap;
finally
jpg.Free;
bitmap.Free
end;
TRY
IF Assigned(OriginalBitmap)
THEN OriginalBitmap.Free;
OriginalBitmap := TBitmap.Create;
// Use polymorphic TPicture to load any registered file type
Picture := TPicture.Create;
TRY
Picture.LoadFromFile(OpenPictureDialog.Filename);
// OroginalColorImage.Picture.LoadFromFile(OpenPictureDialog.Filename);
// Try converting into bitmap
TRY
// OriginalBitmap.Assign(plateimg.Picture.Graphic);
OriginalBitmap.Assign(Picture.Graphic);
EXCEPT
OriginalBitmap.Width := Picture.Graphic.Width;
OriginalBitmap.Height := Picture.Graphic.Height;
OriginalBitmap.PixelFormat := pf24bit;
OriginalBitmap.Canvas.Draw(0,0, Picture.Graphic)
END;
FINALLY
Picture.Free
END;
OriginalBitmap.PixelFormat := pf24bit;
FormHistoStretchGrays.Caption := '车牌识别系统: ' +
OpenPictureDialog.Filename;
// Convert any/all "color" pixels to intensity values-- but if original
// image already in shades of gray, no conversion will occur.
Converted := FALSE;
FOR j := 0 TO OriginalBitmap.Height-1 DO
BEGIN
row := OriginalBitmap.Scanline[j];
FOR i := 0 TO OriginalBitmap.Width-1 DO
BEGIN
WITH row[i] DO
BEGIN // Force R = G = B
IF (rgbtRed <> rgbtGreen) OR (rgbtRed <> rgbtBlue)
THEN BEGIN
Converted := TRUE;
Intensity := RGBTripleToY(row[i]);
rgbtRed := Intensity;
rgbtGreen := Intensity;
rgbtBlue := Intensity
END
END
END
END;
GrayCount := CountColors(OriginalBitmap);
// OK to show image now
ImageOriginal.Picture.Graphic := OriginalBitmap;
ImageHistoStretched.Picture.Graphic := OriginalBitmap;
CarGrayStreenchClick(Sender);
FINALLY
Screen.Cursor := crDefault
END;
END
end;
procedure TFormHistoStretchGrays.ExpandClick(Sender: TObject);
begin
self.DoubleBuffered := True;
// 设立双缓冲模式
PictureTwoValue(ImageHistoStretched.Picture.Bitmap,TwoValueTrackBar.Position+128);
// 调用位图二值化的过程
if (BitmapErose(self.ImageHistoStretched.Picture.Bitmap, True)) then
begin
ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
end
else
showmessage('腐蚀失败');
// 调用图象腐蚀函数
if (BitmapDilate(self.ImageHistoStretched.Picture.Bitmap, False)) then
begin
ImageHistoStretched.Picture.Assign(ImageHistoStretched.Picture.Bitmap);
end
else
showmessage('膨胀失败');
end;
procedure TFormHistoStretchGrays.GrayStretch(bitmap:TBITMAP);
var
p: PByteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: integer;
scanlinebytes: integer;
begin
// GrayRangeRight:=200;
// GrayRangeLeft:=0;
// GetParam;
Bmp := TBitmap.Create;
//创建位图实例
Bmp.Assign(Bitmap);
Bmp.PixelFormat := pf24Bit;
// GrayRangeRight:=200;
// GrayRangeLeft:=10;
p := Bmp.scanline[0];
ScaleFactor := 255 /(GrayRangeRight - GrayRangeLeft);;
//拉伸比例
scanlinebytes := integer(Bmp.scanline[1]) - integer(Bmp.scanline[0]);
//扫描线间距
for y := 0 to Bmp.Height - 1 do
begin
for x := 0 to Bmp.Width - 1 do
begin
Gray :=p[x* 3];
Gray := min(255,Round(ScaleFactor * (Gray - grayRangeLeft)));
//进行灰度拉伸
p[x * 3 + 2] := byte(Gray);
p[x * 3 + 1] := byte(Gray);
p[x * 3] := byte(Gray);
//重新赋值
end;
inc(integer(p), scanlinebytes);
end;
Bitmap.Assign(bmp);
//显示拉伸后的位图
//bitmap.Invalidate;
bmp.Free;
end;
procedure TFormHistoStretchGrays.ImageGray(Bitmap: TBitmap);
var
p: pbyteArray;
x, y: Integer;
Bmp: TBitmap;
Gray: integer;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Bitmap);
Bmp.PixelFormat := pf24bit;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -