gpt4 book ai didi

delphi - 如何加快算法(二值化,积分图像)

转载 作者:行者123 更新时间:2023-12-03 18:25:20 24 4
gpt4 key购买 nike

我根据此网址中描述的积分图像算法编写了此程序

http://people.scs.carleton.ca/~roth/iit-publications-iti/docs/gerh-50002.pdf

有什么办法可以更快地执行此代码?

指针比动态数组快得多吗?

procedure TForm1.bBinarizationClick(Sender: TObject);
var
iX1, iY1,
iX2, iY2,
ii, jj,
s, s2,
iSum, iCount, index,
iHeight, iWidth : Integer;
iSize: Integer;

row : ^TRGBTriple;
black : TRGBTriple;
aIntegralIm: array of Integer;
aGrays : array of Byte;

startTime : Cardinal;

begin
iWidth := bBitmap.Width;
iHeight := bBitmap.Height;
iSize := iWidth * iHeight;

SetLength(aGrays, iSize);
SetLength(aIntegralIm, iSize);

black.rgbtRed := (clBlack and $0000FF);
black.rgbtGreen := (clBlack and $00FF00) shr 8;
black.rgbtBlue := (clBlack and $FF0000) shr 16;

bBitmap2.Canvas.Brush.Color := clWhite;
bBitmap2.Canvas.FillRect(Rect(0, 0, bBitmap2.Width, bBitmap2.Height));

s := Round(iWidth / TrackBar2.Position);
s2 := Round(s / 2);

startTime := GetTickCount();

index := 0;

for ii := 0 to iHeight - 1 do begin
row := bBitmap.ScanLine[ii];
for jj := 0 to iWidth - 1 do begin
aGrays[index] := ((row.rgbtRed * 77 + row.rgbtGreen * 150 + row.rgbtBlue * 29) shr 8);
inc(index);
inc(row);
end;
end;


for ii := 0 to iWidth - 1 do begin
iSum := 0;
for jj := 0 to iHeight - 1 do begin
index := jj*iWidth+ii;
iSum := iSum + aGrays[index];
if ii = 0 then aIntegralIm[index] := iSum
else aIntegralIm[index] := aIntegralIm[index - 1] + iSum;
end;
end;


for jj := 0 to iHeight - 1 do begin
row := bBitmap2.ScanLine[jj];
for ii := 0 to iWidth - 1 do begin

index := jj*iWidth+ii;

iX1 := ii-s2;
iX2 := ii+s2;
iY1 := jj-s2;
iY2 := jj+s2;

if (iX1 < 0) then iX1 := 0;
if (iX2 >= iWidth) then iX2 := iWidth-1;
if (iY1 < 0) then iY1 := 0;
if (iY2 >= iHeight) then iY2 := iHeight-1;

iCount := (iX2 - iX1) * (iY2 - iY1);

iSum := aIntegralIm[iY2*iWidth+iX2]
- aIntegralIm[iY1*iWidth+iX2]
- aIntegralIm[iY2*iWidth+iX1]
+ aIntegralIm[iY1*iWidth+iX1];

if (aGrays[index] * iCount) < (iSum * (100 - TrackBar1.Position) / 100) then row^ := black;

inc(row);

end;
end;

ePath.Text := 'Time: ' + inttostr(GetTickCount() - startTime) + ' ms';

imgOryginal.Picture.Bitmap.Assign(bBitmap2);

end;

最佳答案

您至少可以做一些简单的事情:


将(100-TrackBar1.Position)预计算为变量
代替除法:/ 100在另一侧使用* 100。您可能不需要任何浮点值。
使用查找表进行以下操作(请注意解释标识btw?):


码:

if (iX1 < 0) then iX1 := 0;
if (iX2 >= iWidth) then iX2 := iWidth-1;
if (iY1 < 0) then iY1 := 0;
if (iY2 >= iHeight) then iY2 := iHeight-1;



尝试保持索引和icremnet减量而不是乘法:index:= jj * iWidth + ii;

关于delphi - 如何加快算法(二值化,积分图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2280596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com