performance - 高效计算光流参数 - MATLAB

标签 performance matlab image-processing computer-vision vectorization

我正在执行 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正如问题中预测的那样,这里的方法是取代那些繁重的 mean/average 计算。此外,这些迭代微分可以替换为 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)

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

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

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

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

关于performance - 高效计算光流参数 - MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34383257/

相关文章:

javascript - HTML 中 DOM 查询的最佳属性顺序

产生闭区间随机实数的matlab函数

SQL Server 运行缓慢

performance - 如何在 Qt4 中有效地移动像素图的像素

matlab - 在子图的情况下,如何为所有 x 轴和 y 轴使用通用标签?

java - 一张图像中包含多个数据矩阵条形码

Python/OpenCV - 间歇性错误

PHP:析构函数与 register_shutdown_function

performance - 合并两个在不同机器上运行的测试报告的*.jtl文件

matlab - 如何在MATLAB中模拟间歇性需求?