关于2值化的问题 ( 积分: 100 )

  • 主题发起人 主题发起人 martinsino
  • 开始时间 开始时间
M

martinsino

Unregistered / Unconfirmed
GUEST, unregistred user!
var
Form1: TForm1;
pHeight,pWidth:integer;
pLine:pByteArray;
p:array of integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
if self.OpenPictureDialog1.Execute then
begin
Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i,j:integer;
Gray:integer;
begin
Image1.Picture.Bitmap.HandleType:=bmDIB;
Image1.Picture.Bitmap.pixelFormat:=pf24bit;
pHeight:=Image1.Picture.Bitmap.Height;
pWidth:=Image1.Picture.Bitmap.Width;

//图像灰度化
for i:=0 to pHeight-1 do
begin
pLine:=Image1.Picture.Bitmap.ScanLine;
for j:=0 to pWidth-1 do
begin
Gray:=round(0.11*pLine[3*j]+0.59*pLine[3*j+1]+0.3*pLine[3*j+2]);
p[i*pWidth+j]:=Gray;
pLine[3*j]:=byte(Gray);
pLine[3*j+1]:=byte(Gray);
pLine[3*j+2]:=byte(Gray);
//p[i*pWidth+j]:=Gray; 记录每个点的灰度值
end;
end;
Image1.Invalidate;
end;
end.
这段程序在不加屏蔽掉的那一行,可以正常运行(灰度化).但是家了记录各点灰度值的语句(也就是屏蔽的那行),程序运行就出现内存错误,是什么原因啊!小弟下面的工作(2值化)需要用到这个参数.不知道各位大侠有没有什么好的解决方法```
谢谢
 
var
Form1: TForm1;
pHeight,pWidth:integer;
pLine:pByteArray;
p:array of integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Self.OpenPictureDialog1.Filter := '*.bmp|*.bmp';
if self.OpenPictureDialog1.Execute then
begin
Image1.Picture.Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i,j:integer;
Gray:integer;
begin
Image1.Picture.Bitmap.HandleType:=bmDIB;
Image1.Picture.Bitmap.pixelFormat:=pf24bit;
pHeight:=Image1.Picture.Bitmap.Height;
pWidth:=Image1.Picture.Bitmap.Width;

//图像灰度化
for i:=0 to pHeight-1 do
begin
pLine:=Image1.Picture.Bitmap.ScanLine;
for j:=0 to pWidth-1 do
begin
Gray:=round(0.11*pLine[3*j]+0.59*pLine[3*j+1]+0.3*pLine[3*j+2]);
p[i*pWidth+j]:=Gray;
pLine[3*j]:=byte(Gray);
pLine[3*j+1]:=byte(Gray);
pLine[3*j+2]:=byte(Gray);
//p[i*pWidth+j]:=Gray; 记录每个点的灰度值
end;
end;
Image1.Invalidate;
end;
end.
这段程序在不加屏蔽掉的那一行,可以正常运行(灰度化).但是家了记录各点灰度值的语句(也就是屏蔽的那行),程序运行就出现内存错误,是什么原因啊!小弟下面的工作(2值化)需要用到这个参数.不知道各位大侠有没有什么好的解决方法```
谢谢
 
p[i*pWidth+j]:=Gray;
...
//p[i*pWidth+j]:=Gray; 记录每个点的灰度值
怎么上面的没有屏蔽就不出现错误?
另外,出现内存错误,那肯定就是这个p数组的错误了,p是动态数组,还没分配内存就访问,当然会出错
循环前加上这句:
SetLength(p,pHeight *pWidth);
 
接受答案了.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
930
SUNSTONE的Delphi笔记
S
I
回复
0
查看
600
import
I
I
回复
0
查看
510
import
I
后退
顶部