码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

----------开发环境D7

------效果图
image

---Image2的图片请自行加载,加载的图片为bmp格式,最好是颜色丰富的图片!
----Image2.Pictureimage


---Unit开始--
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
Image3: TImage;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

var
GrayLevelArray:array[0..255] of Integer;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
vP:PByteArray;
i,j,x,y:Integer;
vBmp,vBmp2:TBitmap;
vGray:Byte;
vColor:TColor;
vMaxValue:Integer;
begin
vBmp:=TBitmap.Create;
vBmp.Assign(Image2.Picture.Bitmap);
vBmp.PixelFormat:=pf24bit;//24位的位图
for y:=0 to vBmp.Height-1 do
begin
vP:=vBmp.ScanLine[y];
for x:=0 to vBmp.Width-1 do
begin
vGray:=(77 * vp[3x+2] + 149 * vp[3x+1] + 29 * vp[3x]) shr 8 ;//这个的解释在这篇delphi图像处理之灰度处理中https://www.cnblogs.com/dmqhjp/p/15136969.html
GrayLevelArray[vGray]:=GrayLevelArray[vGray]+1;//统计每个灰度级上的像素点的个数
end;
end;
vBmp.Free;
vMaxValue:=GrayLevelArray[0]; //初始化最大值
Image1.Canvas.Brush.Color:=clSkyBlue ;//clSkyBlue
image1.Canvas.FillRect(Rect(0,0,Image1.Width,Image1.Height));
Image1.Canvas.Pen.Color:=clYellow;
for i:=1 to 255 do
begin
if vMaxValue<GrayLevelArray[i] then
begin
vMaxValue:=GrayLevelArray[i]
end;
end;
//绘制灰度级像素个数分布图
for i:=0 to 255 do
begin
Image1.Canvas.Pen.Color:=RGB(i,i,i); //画笔颜色渐变
Image1.Canvas.MoveTo(i,300);
//Image1.Canvas.LineTo(i,300-Graylevelarray[i]); //没用对数,个数太多的图像显示效果
//用对数降级,怕某个灰度级的像素个数太多,看不出来
Image1.Canvas.LineTo(i,300-round(60
(Log10(Graylevelarray[i]+1))));
end;
vbmp2:=TBitmap.Create;
vBmp2.Width:=Image3.Width;
vBmp2.Height:=Image3.Height;
//在Image3上绘制256级灰度分布图
for i:=0 to vBmp2.Width-1 do
begin
vColor:=RGB(i,i,i);
for j:=0 to vBmp2.Height-1 do
begin
vBmp2.Canvas.Pixels[i,j]:=vColor;
end;
end;
Image3.Picture.Bitmap.Assign(vBmp2);
vBmp2.Free;
Label3.Caption:='各灰度级上最大像素点数是:'+inttostr(vMaxValue);
end;

end.

----Unit结束------

----Form开始
object Form1: TForm1
Left = 156
Top = 217
Width = 1019
Height = 675
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 8
Top = 16
Width = 256
Height = 329
end
object Image2: TImage
Left = 352
Top = 16
Width = 625
Height = 465
Center = True
Proportional = True
Stretch = True
end
object Image3: TImage
Left = 8
Top = 416
Width = 256
Height = 33
end
object Label1: TLabel
Left = 8
Top = 352
Width = 32
Height = 13
AutoSize = False
Caption = '0'
end
object Label2: TLabel
Left = 240
Top = 348
Width = 32
Height = 13
AutoSize = False
Caption = '255'
end
object Label3: TLabel
Left = 200
Top = 544
Width = 473
Height = 25
AutoSize = False
end
object Button1: TButton
Left = 104
Top = 544
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end

----Form结束------

posted on 2021-08-13 17:43  码农的笔记  阅读(185)  评论(0编辑  收藏  举报