我正在实现光流上 Horn Schunck paper的偏导数方程.然而,即使对于相对较小的图像(320×568),也需要令人沮丧的长时间(~30-40秒)才能完成.我假设这是由于320 x 568 = 181760循环迭代,但我无法找到一
有没有办法将其转化为更高效的MATLAB操作(也许是卷积)?我可以弄清楚如何做它作为它的卷积而不是Ix和Iy.我也考虑过矩阵移位,但这只适用于它,据我所知.
有没有其他人遇到这个问题并找到了解决方案?
我的代码如下:
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
是这里的方式,正如在问题中预测的那样取代那些重均值/平均值的计算.此外,这些迭代差异可以由
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
的差异所取代,所以这应该是另一个改进.>那些昂贵的平均计算被非常快速的卷积计算所取代,这应该是主要的改进部分.