高效计算光流参数 - MATLAB

Efficiently calculate optical flow parameters - MATLAB

我正在对光流实现 Horn & Schunck paper 中的偏导数方程。然而,即使是相对较小的图像 (320x568),完成它也需要很长的时间(约 30-40 秒),令人沮丧。我假设这是由于 320 x 568 = 181760 循环迭代,但我想不出更有效的方法来做到这一点(缺少 MEX 文件)。

有什么方法可以将其转化为更高效的 MATLAB 运算(也许是卷积运算)?我可以弄清楚如何将此作为 It 的卷积而不是 IxIy。我也考虑过矩阵移位,但据我所知,这也只适用于 It

有其他人运行解决这个问题并找到解决方案吗?

我的代码如下:

function [Ix, Iy, It] = getFlowParams(img1, img2)

% Make sure image dimensions match up
assert(size(img1, 1) == size(img2, 1) && size(img1, 2) == size(img2, 2), ...
    'Images must be the same size');
assert(size(img1, 3) == 1, 'Images must be grayscale');

% Dimensions of original image
[rows, cols] = size(img1);
Ix = zeros(numel(img1), 1);
Iy = zeros(numel(img1), 1);
It = zeros(numel(img1), 1);

% Pad images to handle edge cases
img1 = padarray(img1, [1,1], 'post');
img2 = padarray(img2, [1,1], 'post');

% Concatenate i-th image with i-th + 1 image
imgs = cat(3, img1, img2);

% Calculate energy for each pixel
for i = 1 : rows
    for j = 1 : cols
        cube = imgs(i:i+1, j:j+1, :);
        Ix(sub2ind([rows, cols], i, j)) = mean(mean(cube(:, 2, :) - cube(:, 1, :)));
        Iy(sub2ind([rows, cols], i, j)) = mean(mean(cube(2, :, :) - cube(1, :, :)));
        It(sub2ind([rows, cols], i, j)) = mean(mean(cube(:, :, 2) - cube(:, :, 1)));
    end
end

2D convolution is the way to go here as also predicted in the question to replace those heavy mean/average calculations. Also, those iterative differentiations could be replaced by MATLAB's diff。因此,结合所有这些,矢量化实现将是 -

%// Pad images to handle edge cases
img1 = padarray(img1, [1,1], 'post');
img2 = padarray(img2, [1,1], 'post');

%// Store size parameters for later usage
[m,n] = size(img1);

%// Differentiation along dim-2 on input imgs for Ix calculations
df1 = diff(img1,[],2)
df2 = diff(img2,[],2)

%// 2D Convolution to simulate average calculations & reshape to col vector
Ixvals = (conv2(df1,ones(2,1),'same') + conv2(df2,ones(2,1),'same'))./4;
Ixout = reshape(Ixvals(1:m-1,:),[],1);

%// Differentiation along dim-1 on input imgs for Iy calculations
df1 = diff(img1,[],1)
df2 = diff(img2,[],1)

%// 2D Convolution to simulate average calculations & reshape to col vector
Iyvals = (conv2(df1,ones(1,2),'same') + conv2(df2,ones(1,2),'same'))./4
Iyout = reshape(Iyvals(:,1:n-1),[],1);

%// It just needs elementwise diffentiation between input imgs.
%// 2D convolution to simulate mean calculations & reshape to col vector
Itvals = conv2(img2-img1,ones(2,2),'same')./4
Itout = reshape(Itvals(1:m-1,1:n-1),[],1)

这种矢量化实施的好处是:

  • 内存效率:不再会产生内存开销的第三维串联。同样,在性能方面这将是一个好处,因为我们不需要索引到这样的 重数组 .

  • loopy代码中的迭代微分被diff微分代替,所以这应该是另一个改进。

  • 那些昂贵的平均计算被非常快速的卷积计算所取代,这应该是主要的改进部分。

一种更快的方法,具有改进的结果(在我观察到的情况下)如下:

function [Ix, Iy, It] = getFlowParams(imNew,imPrev)

gg = [0.2163, 0.5674, 0.2163]; 
f = imNew + imPrev; 
Ix = f(:,[2:end end]) - f(:,[1 1:(end-1)]); 
Ix = conv2(Ix,gg','same');

Iy = f([2:end end],:) - f([1 1:(end-1)],:); 
Iy = conv2(Iy,gg ,'same');

It = 2*conv2(gg,gg,imNew - imPrev,'same');

这可以优雅地处理边界情况。

这是我 optical flow toolbox 的一部分,您可以在其中轻松实时查看 H&S、Lucas Kanade 等。在工具箱中,该函数称为 grad3D.m。您可能还想在同一个工具箱中查看 grad3Drec.m,它添加了简单的时间模糊。