{功能:两灰阶图像逐点相减(减影),得到减影图像的处理函数
参数: Pic,Pic 两灰阶图像
picdev 得到的减影图像}
procedure DivPic(const Pic1,Pic2:TImage;var picdev:TImage;CutRect:TRect);
var
p1,p2,pdevByteArray;
x,y,x_s,y_s:integer;
byteValue:byte;
bmp1,bmp2,bmpdev:TBitmap;
MyRect:TRect;
begin
bmp1:=TBitmap.Create; //两灰阶图像与目标减影图像载入暂存图像
bmp2:=TBitmap.Create;
bmpdev:=TBitmap.Create;
bmp1.Assign(pic1.Picture.Bitmap);
bmp2.Assign(pic2.Picture.Bitmap );
bmpdev.Assign(pic1.Picture.Bitmap );
x_s:=0; //调整目标减影图像图像的大小,取小舍大
y_s:=0;
if bmp1.Width >x_s then x_s:=bmp1.Width;
if bmp2.Width <x_s then x_s:=bmp2.Width;
bmpdev.Width:=x_s;
// if bmpdev.Width <x_s then x_s:=bmpdev.Width;
if bmp1.Height >y_s then y_s:=bmp1.Height;
if bmp2.Height <y_s then y_s:=bmp2.Height;
bmpdev.Height:=y_s;
// if bmpdev.Height <y_s then y_s:=bmpdev.Height;
if (x_s<=0) or (y_s<=0) then exit; //灰阶图像不存在,则退出程序
for y:=0 to y_s-1 do //两图像逐点相减处理,存入目标图像
begin
p1:=bmp1.ScanLine[y];
p2:=bmp2.ScanLine[y];
pdev:=bmpdev.ScanLine[y];
for x:=0 to x_s-1 do
begin
if p2[x*3]>=p1[x*3] then
byteValue:=0 //p2[x*3]-p1[x*3]
else
byteValue:=p1[x*3]-p2[x*3];
pdev[x*3]:=byteValue;
pdev[x*3+1]:=byteValue;//p2[x*3+1]-p1[x*3+1];
pdev[x*3+2]:=byteValue;//p2[x*3+2]-p1[x*3+2];
end;
end;
picdev.Picture.Bitmap.Width:=CutRect.Right-CutRect.Left;
picdev.Picture.Bitmap.Height:=CutRect.Bottom-CutRect.Top;
MyRect:=rect(0,0,picdev.Picture.Bitmap.Width,
picdev.Picture.Bitmap.Height);
picdev.Picture.Bitmap.Canvas.CopyRect(MyRect,bmpdev.Canvas,CutRect);
// bmpdev.Canvas.CopyRect(fCutRect);
// picdev.Picture.Bitmap.Assign(bmpdev);
bmp1.Free;
bmp2.Free;
bmpdev.Free;
end;