分享
 
 
 

实现Lucas-Kanade光流计算的Delphi类

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

{

作者:刘留

参考文献为:

Jean-Yves Bouguet "Pyramidal Implementation of the Lucas Kanade Feature Tracker Description of the algorithm"

http://www.aivisoft.net/

Geo.Cra[at]gmail[dot]com

}

unit OpticalFlowLK;

interface

uses

Math, Windows, SysUtils, Variants, Classes, Graphics, unitypes, ColorConv;

type

TOpticalFlowLK = class

private

ImageOld, ImageNew: TTripleLongintArray;

ImageGray, dx, dy, dxy: TDoubleLongintArray;

Eigenvalues: TDoubleExtendedArray;

WBPoint: TDoubleBooleanArray;

Height, Width, L, RadioX, RadioY: longint;

procedure CornerDetect(sWidth, sHeight: longint; Quality: extended);

procedure MakePyramid(var Images: TTripleLongintArray; sWidth, sHeight, sL: longint);

public

Frame: TBitmap;

Features: TSinglePointInfoArray;

FeatureCount, SupportCount: longint;

Speed, Radio: extended;

procedure Init(sWidth, sHeight, sL: longint);

procedure InitFeatures(Frames: TBitmap);

procedure CalOpticalFlowLK(Frames: TBitmap);

destructor Destroy; override;

end;

implementation

procedure TOpticalFlowLK.CornerDetect(sWidth, sHeight: longint; Quality: extended);

var

i, j, fi, fj: longint;

a, b, c, sum, MinAccept, MaxEigenvalue: extended;

begin

FeatureCount := 0;

{

下面采用Good Feature To Track介绍的方法

J. Shi and C. Tomasi "Good Features to Track", CVPR 94

}

for i := 1 to sWidth - 2 do

for j := 1 to sHeight - 2 do begin

dx[i, j] := ImageGray[i - 1, j - 1] + 2 * ImageGray[i - 1, j] + ImageGray[i - 1, j + 1]

- (ImageGray[i + 1, j - 1] + 2 * ImageGray[i + 1, j] + ImageGray[i + 1, j + 1]);

dy[i, j] := ImageGray[i - 1, j + 1] + 2 * ImageGray[i, j + 1] + ImageGray[i + 1, j + 1]

- (ImageGray[i - 1, j - 1] + 2 * ImageGray[i, j - 1] + ImageGray[i + 1, j - 1]);

dxy[i, j] := ImageGray[i + 1, j - 1] + ImageGray[i - 1, j + 1]

- (ImageGray[i - 1, j - 1] + ImageGray[i + 1, j + 1]);

end;

{求取Sobel算子的Dx, Dy, Dxy

Dx:

|1 0 -1|

|2 0 -2|

|1 0 -1|

Dy:

|-1 -2 -1|

| 0 0 0|

| 1 2 1|

Dxy

|-1 0 1|

| 0 0 0|

| 1 0 -1|}

MaxEigenvalue := 0;

for i := 2 to sWidth - 3 do

for j := 2 to sHeight - 3 do begin

a := 0; b := 0; c := 0;

for fi := i - 1 to i + 1 do

for fj := j - 1 to j + 1 do begin

a := a + sqr(dx[fi, fj]);

b := b + dxy[fi, fj];

c := c + sqr(dy[fi, fj]);

end;

a := a / 2; c := c / 2;

Eigenvalues[i, j] := (a + c - sqrt((a - c) * (a - c) + b * b));

if Eigenvalues[i, j] > MaxEigenvalue then MaxEigenvalue := Eigenvalues[i, j];

end;

{求取矩阵

|∑Dx*Dx ∑Dxy|

M=| |

|∑Dxy ∑Dy*Dy|

的特征值

λ= ∑Dx*Dx + ∑Dy*Dy - ((∑Dx*Dx+∑Dy*Dy)^2-4*(∑Dx*Dx * ∑Dy*Dy - ∑Dxy * ∑Dxy))^1/2}

MinAccept := MaxEigenvalue * Quality;

{设置最小允许阀值}

for i := 8 to sWidth - 9 do

for j := 8 to sHeight - 9 do

if Eigenvalues[i, j] > MinAccept then begin

WBPoint[i, j] := true;

Inc(FeatureCount);

end else

WBPoint[i, j] := false;

for i := 8 to sWidth - 9 do

for j := 8 to sHeight - 9 do

if WBPoint[i, j] then begin

sum := Eigenvalues[i, j];

for fi := i - 8 to i + 8 do begin

for fj := j - 8 to j + 8 do

if sqr(fi - i) + sqr(fj - j) <= 64 then

if (Eigenvalues[fi, fj] >= sum) and ((fi <> i) or (fj <> j)) and (WBPoint[fi, fj]) then begin

WBPoint[i, j] := false;

Dec(FeatureCount);

break;

end;

if not WBPoint[i, j] then break;

end;

end;

{用非最大化抑制来抑制假角点}

setlength(Features, FeatureCount); fi := 0;

for i := 8 to sWidth - 9 do

for j := 8 to sHeight - 9 do

if WBPoint[i, j] then begin

Features[fi].Info.X := i;

Features[fi].Info.Y := j;

Features[fi].Index := 0;

Inc(fi);

end;

{输出最终的点序列}

end;

procedure TOpticalFlowLK.Init(sWidth, sHeight, sL: longint);

begin

Width := sWidth; Height := sHeight; L := sL;

setlength(ImageOld, Width, Height, L);

setlength(ImageNew, Width, Height, L);

Frame := TBitmap.Create;

Frame.Width := Width; Frame.Height := Height;

Frame.PixelFormat := pf24bit;

setlength(ImageGray, Width, Height);

setlength(Eigenvalues, Width, Height);

setlength(dx, Width, Height);

setlength(dy, Width, Height);

setlength(dxy, Width, Height);

setlength(WBPoint, Width, Height);

FeatureCount := 0;

end;

procedure TOpticalFlowLK.MakePyramid(var Images: TTripleLongintArray; sWidth, sHeight, sL: longint);

var

i, j, k, ii, jj, nWidth, nHeight, oWidth, oHeight: longint;

begin

{生成金字塔图像}

oWidth := sWidth; oHeight := sHeight;

for k := 1 to sL - 1 do begin

nWidth := (oWidth + 1) shr 1; nHeight := (oHeight + 1) shr 1;

for i := 1 to nWidth - 2 do begin

ii := i shl 1;

for j := 1 to nHeight - 2 do begin

jj := j shl 1;

Images[i, j, k] := (Images[ii, jj, k - 1] shl 2 +

Images[ii - 1, jj, k - 1] shl 1 + Images[ii + 1, jj, k - 1] shl 1 + Images[ii, jj - 1, k - 1] shl 1 + Images[ii, jj + 1, k - 1] shl 1 +

Images[ii - 1, jj - 1, k - 1] + Images[ii + 1, jj - 1, k - 1] + Images[ii - 1, jj + 1, k - 1] + Images[ii + 1, jj + 1, k - 1]) shr 4;

{高斯原则,shl右移位,shr左移位}

end;

end;

for i := 1 to nWidth - 2 do begin

ii := i shl 1;

Images[i, 0, k] := (Images[ii, 0, k - 1] shl 2 +

Images[ii - 1, 0, k - 1] shl 1 + Images[ii + 1, 0, k - 1] shl 1 + Images[ii, 0, k - 1] shl 1 + Images[ii, 1, k - 1] shl 1 +

Images[ii - 1, 0, k - 1] + Images[ii + 1, 0, k - 1] + Images[ii - 1, 1, k - 1] + Images[ii + 1, 1, k - 1]) shr 4;

Images[i, nHeight - 1, k] := (Images[ii, oHeight - 1, k - 1] shl 2 +

Images[ii - 1, oHeight - 1, k - 1] shl 1 + Images[ii + 1, oHeight - 1, k - 1] shl 1 + Images[ii, oHeight - 2, k - 1] shl 1 + Images[ii, oHeight - 1, k - 1] shl 1 +

Images[ii - 1, oHeight - 2, k - 1] + Images[ii + 1, oHeight - 2, k - 1] + Images[ii - 1, oHeight - 1, k - 1] + Images[ii + 1, oHeight - 1, k - 1]) shr 4;

end;

{处理上下边}

for j := 1 to nHeight - 2 do begin

jj := j shl 1;

Images[0, j, k] := (Images[0, jj, k - 1] shl 2 +

Images[0, jj, k - 1] shl 1 + Images[1, jj, k - 1] shl 1 + Images[0, jj - 1, k - 1] shl 1 + Images[0, jj + 1, k - 1] shl 1 +

Images[0, jj - 1, k - 1] + Images[1, jj - 1, k - 1] + Images[0, jj + 1, k - 1] + Images[1, jj + 1, k - 1]) shr 4;

Images[nWidth - 1, j, k] := (Images[oWidth - 1, jj, k - 1] shl 2 +

Images[oWidth - 2, jj, k - 1] shl 1 + Images[oWidth - 1, jj, k - 1] shl 1 + Images[oWidth - 1, jj - 1, k - 1] shl 1 + Images[oWidth - 1, jj + 1, k - 1] shl 1 +

Images[oWidth - 2, jj - 1, k - 1] + Images[oWidth - 1, jj - 1, k - 1] + Images[oWidth - 2, jj + 1, k - 1] + Images[oWidth - 1, jj + 1, k - 1]) shr 4;

end;

{处理左右边}

Images[0, 0, k] := (Images[0, 0, k - 1] shl 2 +

Images[0, 0, k - 1] shl 1 + Images[1, 0, k - 1] shl 1 + Images[0, 0, k - 1] shl 1 + Images[0, 1, k - 1] shl 1 +

Images[0, 0, k - 1] + Images[1, 0, k - 1] + Images[0, 1, k - 1] + Images[1, 1, k - 1]) shr 4;

{处理左上点}

Images[0, nHeight - 1, k] := (Images[0, oHeight - 1, k - 1] shl 2 +

Images[0, oHeight - 1, k - 1] shl 1 + Images[1, oHeight - 1, k - 1] shl 1 + Images[0, oHeight - 2, k - 1] shl 1 + Images[0, oHeight - 1, k - 1] shl 1 +

Images[0, oHeight - 2, k - 1] + Images[1, oHeight - 2, k - 1] + Images[0, oHeight - 1, k - 1] + Images[1, oHeight - 1, k - 1]) shr 4;

{处理左下点}

Images[nWidth - 1, 0, k] := (Images[oWidth - 1, 0, k - 1] shl 2 +

Images[oWidth - 2, oHeight - 1, k - 1] shl 1 + Images[oWidth - 1, oHeight - 1, k - 1] shl 1 + Images[oWidth - 1, oHeight - 1, k - 1] shl 1 + Images[oWidth - 1, oHeight - 1, k - 1] shl 1 +

Images[oWidth - 2, oHeight - 1, k - 1] + Images[oWidth - 1, oHeight - 1, k - 1] + Images[oWidth - 2, oHeight - 1, k - 1] + Images[oWidth - 1, oHeight - 1, k - 1]) shr 4;

{处理右上点}

Images[nWidth - 1, nHeight - 1, k] := (Images[oWidth - 1, oHeight - 1, k - 1] shl 2 +

Images[oWidth - 2, oHeight - 1, k - 1] shl 1 + Images[oWidth - 1, oHeight - 1, k - 1] shl 1 + Images[oWidth - 1, oHeight - 2, k - 1] shl 1 + Images[oWidth - 1, oHeight - 1, k - 1] shl 1 +

Images[oWidth - 2, oHeight - 2, k - 1] + Images[oWidth - 1, oHeight - 2, k - 1] + Images[oWidth - 2, oHeight - 1, k - 1] + Images[oWidth - 1, oHeight - 1, k - 1]) shr 4;

{处理右下点}

end;

end;

procedure TOpticalFlowLK.InitFeatures(Frames: TBitmap);

var

i, j: longint;

Line: pRGBTriple;

begin

SetStretchBltMode(Frame.Canvas.Handle, Stretch_Halftone);

StretchBlt(Frame.Canvas.Handle, 0, 0, Width, Height, Frames.Canvas.Handle, 0, 0, Frames.Width, Frames.Height, SrcCopy);

for i := 0 to Height - 1 do begin

Line := Frame.ScanLine[i];

for j := 0 to Width - 1 do begin

ImageOld[j, i, 0] := (Line^.rgbtBlue * 11 + Line^.rgbtGreen * 59 + Line^.rgbtRed * 30) div 100;

ImageGray[j, i] := ImageOld[j, i, 0];

Inc(Line);

end;

end;

{初始化金字塔图像第一层ImageOld[x,y,0]}

MakePyramid(ImageOld, Width, Height, L);

{生成金字塔图像}

CornerDetect(Width, Height, 0.01);

{进行强角点检测}

end;

procedure TOpticalFlowLK.CalOpticalFlowLK(Frames: TBitmap);

var

i, j, fi, fj, k, ll, m, dx, dy, gx, gy, px, py, kx, ky, ed, edc, nWidth, nHeight: longint;

nx, ny, vx, vy, A, B, C, D, E, F, Ik: extended;

Ix, Iy: TDoubleExtendedArray;

Line: pRGBTriple;

Change: boolean;

begin

SetStretchBltMode(Frame.Canvas.Handle, Stretch_Halftone);

StretchBlt(Frame.Canvas.Handle, 0, 0, Width, Height, Frames.Canvas.Handle, 0, 0, Frames.Width, Frames.Height, SrcCopy);

for i := 0 to Height - 1 do begin

Line := Frame.ScanLine[i];

for j := 0 to Width - 1 do begin

ImageNew[j, i, 0] := (Line^.rgbtBlue * 11 + Line^.rgbtGreen * 59 + Line^.rgbtRed * 30) div 100;

Inc(Line);

end;

end;

{初始化金字塔图像第一层ImageNew[x,y,0]}

MakePyramid(ImageNew, Width, Height, L);

{生成金字塔图像}

setlength(Ix, 15, 15); setlength(Iy, 15, 15);

{申请差分图像临时数组}

for m := 0 to FeatureCount - 1 do begin

{算法细节见:

Jean-Yves Bouguet "Pyramidal Implementation of the Lucas Kanade Feature Tracker Description of the algorithm"}

gx := 0; gy := 0;

for ll := L - 1 downto 0 do begin

px := Features[m].Info.X shr ll;

py := Features[m].Info.Y shr ll;

{对应当前金字塔图像的u点:u[L]:=u/2^L}

nWidth := Width shr ll; nHeight := Height shr ll;

A := 0; B := 0; C := 0;

for i := max(px - 7, 1) to min(px + 7, nWidth - 2) do

for j := max(py - 7, 1) to min(py + 7, nHeight - 2) do begin

fi := i - px + 7; fj := j - py + 7;

Ix[fi, fj] := (ImageOld[i + 1, j, ll] - ImageOld[i - 1, j, ll]) / 2;

Iy[fi, fj] := (ImageOld[i, j + 1, ll] - ImageOld[i, j - 1, ll]) / 2;

A := A + Ix[fi, fj] * Ix[fi, fj]; B := B + Ix[fi, fj] * Iy[fi, fj];

C := C + Iy[fi, fj] * Iy[fi, fj];

end;

{计算2阶矩阵G:

|Ix(x,y)*Ix(x,y) Ix(x,y)*Iy(x,y)|

∑|Ix(x,y)*Iy(x,y) Iy(x,y)*Iy(x,y)|}

D := A * C - B * B;

vx := 0; vy := 0; dx := 0; dy := 0;

if abs(D) > 1E-8 then begin

for k := 1 to 10 do begin

E := 0; F := 0;

for i := max(px - 7, 1) to min(px + 7, nWidth - 2) do

for j := max(py - 7, 1) to min(py + 7, nHeight - 2) do begin

fi := i - px + 7; fj := j - py + 7;

kx := i + gx + dx; ky := j + gy + dy;

if kx < 0 then kx := 0; if kx > nWidth - 1 then kx := nWidth - 1;

if ky < 0 then ky := 0; if ky > nHeight - 1 then ky := nHeight - 1;

Ik := ImageOld[i, j, ll] - ImageNew[kx, ky, ll];

E := E + Ik * Ix[fi, fj];

F := F + Ik * Iy[fi, fj];

end;

{计算2x1阶矩阵b:

|Ik(x,y)*Ix(x,y)|

∑|Ik(x,y)*Iy(x,y)|}

nx := (C * E - B * F) / D;

ny := (B * E - A * F) / (-D);

{计算η=G^-1*b}

vx := vx + nx; vy := vy + ny;

dx := trunc(vx); dy := trunc(vy);

{得到相对运动向量d}

end;

end;

gx := (gx + dx) shl 1; gy := (gy + dy) shl 1;

{得到下一层的预测运动向量g}

end;

gx := gx div 2; gy := gy div 2;

px := px + gx; py := py + gy;

Features[m].Info.X := px;

Features[m].Info.Y := py;

Features[m].Vector.X := gx;

Features[m].Vector.Y := gy;

if (px > Width - 1) or (px < 0) or (py > Height - 1) or (py < 0) then Features[m].Index := 1;

{失去特征点处理}

end;

for k := 0 to L - 1 do begin

nWidth := Width shr k; nHeight := Height shr k;

for i := 0 to nWidth - 1 do

for j := 0 to nHeight - 1 do

ImageOld[i, j, k] := ImageNew[i, j, k];

end;

{复制J到I}

repeat

Change := false;

for i := 0 to FeatureCount - 1 do begin

if Features[i].Index = 1 then

for j := i + 1 to FeatureCount - 1 do begin

Features[j - 1] := Features[j];

Change := true;

end;

if Change then break;

end;

if Change then Dec(FeatureCount);

until not Change;

setlength(Features, FeatureCount);

{删除失去的特征点}

Speed := 0; Radio := 0; RadioX := 0; RadioY := 0;

if FeatureCount > 0 then begin

SupportCount := 0;

for i := 0 to FeatureCount - 1 do

if (Features[i].Vector.X <> 0) and (Features[i].Vector.Y <> 0) then begin

RadioX := RadioX + Features[i].Vector.X;

RadioY := RadioY + Features[i].Vector.Y;

Speed := Speed + sqrt(sqr(Features[i].Vector.X) + sqr(Features[i].Vector.Y));

Inc(SupportCount);

end;

if SupportCount > 0 then begin

Speed := Speed / SupportCount; //*0.0352778;

Radio := ArcTan2(RadioY, RadioX);

end;

end;

{计算平均速度和整体方向}

Frame.Canvas.Pen.Style := psSolid;

Frame.Canvas.Pen.Width := 1;

Frame.Canvas.Pen.Color := clLime;

Frame.Canvas.Brush.Style := bsClear;

for i := 0 to FeatureCount - 1 do

Frame.Canvas.Ellipse(Features[i].Info.X - 4, Features[i].Info.Y - 4, Features[i].Info.X + 4, Features[i].Info.Y + 4);

{用绿圈标识特征点}

Frame.Canvas.Pen.Color := clYellow;

for i := 0 to FeatureCount - 1 do begin

Frame.Canvas.MoveTo(Features[i].Info.X - Features[i].Vector.X, Features[i].Info.Y - Features[i].Vector.Y);

Frame.Canvas.LineTo(Features[i].Info.X, Features[i].Info.Y);

end;

{用黄色线条表示运动向量}

if (SupportCount > 0) and (Speed > 3) then begin

Frame.Canvas.Pen.Style := psSolid;

Frame.Canvas.Pen.Width := 6;

Frame.Canvas.Pen.Color := clWhite;

Frame.Canvas.Ellipse(Width div 2 - 100, Height div 2 - 100, Width div 2 + 100, Height div 2 + 100);

Frame.Canvas.MoveTo(Width div 2, Height div 2);

Frame.Canvas.Pen.Color := clBlue;

Frame.Canvas.LineTo(Width div 2 + trunc(90 * Cos(Radio)), Height div 2 + trunc(90 * Sin(Radio)));

Frame.Canvas.Pen.Style := psClear;

Frame.Canvas.Brush.Style := bsSolid;

Frame.Canvas.Brush.Color := clRed;

Frame.Canvas.Ellipse(Width div 2 + trunc(90 * Cos(Radio)) - 8, Height div 2 + trunc(90 * Sin(Radio)) - 8, Width div 2 + trunc(90 * Cos(Radio)) + 8, Height div 2 + trunc(90 * Sin(Radio)) + 8);

end;

end;

destructor TOpticalFlowLK.Destroy;

begin

setlength(ImageOld, 0);

setlength(ImageNew, 0);

setlength(ImageGray, 0);

setlength(Eigenvalues, 0);

setlength(dx, 0);

setlength(dy, 0);

setlength(dxy, 0);

setlength(WBPoint, 0);

inherited;

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有