【发布时间】:2017-01-09 17:53:27
【问题描述】:
我正在实现来自Horn & Schunck paper 的光流偏导方程。然而,即使是相对较小的图像 (320x568),也需要非常长的时间(约 30-40 秒)才能完成。我认为这是由于 320 x 568 = 181760 循环迭代造成的,但我想不出更有效的方法来做到这一点(缺少 MEX 文件)。
有没有办法把它变成更高效的 MATLAB 操作(也许是卷积)?我可以弄清楚如何将其作为It 的卷积而不是Ix 和Iy。我也考虑过矩阵移位,但据我所知,这只适用于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
【问题讨论】:
-
计算机视觉系统工具箱中实现了几种光流算法。见
opticalFlowHS、opticalFlowLK、opticalFlowFarneback。
标签: performance matlab image-processing computer-vision vectorization