brox optic flow matlab代码
2017-01-03 14:42
211 查看
function [u, v] = optic_flow_brox(img1, img2)
img1 = imread('frame07.png');
img2 = imread('frame08.png');
alpha = 30.0 ; % Global smoothness variable.
gamma = 80.0 ; % Global weight for derivatives.
[ht, wt, dt] = size( img1 ) ;
num_levels = 40 ; % for face; Number of pyramid levels. Can also be calculated automatically
%power(0.95,40) = 0.1285
im1_hr = gaussianRescaling( img1, power( 0.95, num_levels ) ) ; % Rescaling images
im2_hr = gaussianRescaling( img2, power( 0.95, num_levels ) ) ; % to the top of the
% laplacian pyramid.
u = zeros(size(rgb2gray(im1_hr))); % Initialization.
v = zeros(size(rgb2gray(im1_hr)));
for i = num_levels-1 : -1 : 1
I1 = rgb2gray(im1_hr) ;
I2 = rgb2gray(im2_hr) ;
% Computing derivatives.
[Ikx Iky] = imgGrad( I2 ) ;
[Ikx2 Iky2] = imgGrad( I1 ) ;
Ikz = double(I2) - double(I1) ;
Ixz = double(Ikx) - double(Ikx2) ;
Iyz = double(Iky) - double(Iky2) ;
% Calling the processing for a particular resolution.
% Last two arguments are the outer and inner iterations, respectively.
% 1.8 is the omega value for the SOR iteration.
[du, dv] = resolutionProcess_brox( Ikz, Ikx, Iky, Ixz, Iyz, alpha, gamma, 1.8, u, v, 3, 500 ) ;
% Adding up the optical flow.
u = u + du ;
v = v + dv ;
im1_hr = gaussianRescaling( img1, power( 0.95, i ) ) ;
im2_hr = gaussianRescaling( img2, power( 0.95, i ) ) ;
im2_orig = im2_hr ; % Original image without warping for comparison.
% Resize optical flow to current resolution.
u = imresize( u, [size(im1_hr, 1), size(im1_hr, 2)], 'bilinear' ) ;
v = imresize( v, [size(im1_hr, 1), size(im1_hr, 2)], 'bilinear' ) ;
im2_hr = uint8( mywarp_rgb( double( im2_hr ), u, v ) ) ; % taking im1_hr closer to im2_hr.
% Displaying relevant figures.
figure(2);
subplot(3, 3, 1); imshow(im1_hr) ;
subplot(3, 3, 2); imshow(rgb2gray(im2_hr)) ;
subplot(3, 3, 3); imshow(rgb2gray(im2_orig)) ;
subplot(3, 3, 4); imshow(uint8(double(im1_hr)-double(im2_hr))) ;
subplot(3, 3, 5); imshow(uint8(double(im1_hr)-double(im2_orig))) ;
subplot(3, 3, 6); imshow(uint8(double(im2_hr)-double(im2_orig))) ;
subplot(3, 3, 7); imagesc(u) ;
subplot(3, 3, 8); imagesc(v) ;
subplot(3, 3, 9); imagesc(sqrt(u.^2 + v.^2)) ;
%imagesc 数据标定然后显示成图片
% pause;
fprintf('第%d次迭代\n',40-i);
end
img1 = imread('frame07.png');
img2 = imread('frame08.png');
alpha = 30.0 ; % Global smoothness variable.
gamma = 80.0 ; % Global weight for derivatives.
[ht, wt, dt] = size( img1 ) ;
num_levels = 40 ; % for face; Number of pyramid levels. Can also be calculated automatically
%power(0.95,40) = 0.1285
im1_hr = gaussianRescaling( img1, power( 0.95, num_levels ) ) ; % Rescaling images
im2_hr = gaussianRescaling( img2, power( 0.95, num_levels ) ) ; % to the top of the
% laplacian pyramid.
u = zeros(size(rgb2gray(im1_hr))); % Initialization.
v = zeros(size(rgb2gray(im1_hr)));
for i = num_levels-1 : -1 : 1
I1 = rgb2gray(im1_hr) ;
I2 = rgb2gray(im2_hr) ;
% Computing derivatives.
[Ikx Iky] = imgGrad( I2 ) ;
[Ikx2 Iky2] = imgGrad( I1 ) ;
Ikz = double(I2) - double(I1) ;
Ixz = double(Ikx) - double(Ikx2) ;
Iyz = double(Iky) - double(Iky2) ;
% Calling the processing for a particular resolution.
% Last two arguments are the outer and inner iterations, respectively.
% 1.8 is the omega value for the SOR iteration.
[du, dv] = resolutionProcess_brox( Ikz, Ikx, Iky, Ixz, Iyz, alpha, gamma, 1.8, u, v, 3, 500 ) ;
% Adding up the optical flow.
u = u + du ;
v = v + dv ;
im1_hr = gaussianRescaling( img1, power( 0.95, i ) ) ;
im2_hr = gaussianRescaling( img2, power( 0.95, i ) ) ;
im2_orig = im2_hr ; % Original image without warping for comparison.
% Resize optical flow to current resolution.
u = imresize( u, [size(im1_hr, 1), size(im1_hr, 2)], 'bilinear' ) ;
v = imresize( v, [size(im1_hr, 1), size(im1_hr, 2)], 'bilinear' ) ;
im2_hr = uint8( mywarp_rgb( double( im2_hr ), u, v ) ) ; % taking im1_hr closer to im2_hr.
% Displaying relevant figures.
figure(2);
subplot(3, 3, 1); imshow(im1_hr) ;
subplot(3, 3, 2); imshow(rgb2gray(im2_hr)) ;
subplot(3, 3, 3); imshow(rgb2gray(im2_orig)) ;
subplot(3, 3, 4); imshow(uint8(double(im1_hr)-double(im2_hr))) ;
subplot(3, 3, 5); imshow(uint8(double(im1_hr)-double(im2_orig))) ;
subplot(3, 3, 6); imshow(uint8(double(im2_hr)-double(im2_orig))) ;
subplot(3, 3, 7); imagesc(u) ;
subplot(3, 3, 8); imagesc(v) ;
subplot(3, 3, 9); imagesc(sqrt(u.^2 + v.^2)) ;
%imagesc 数据标定然后显示成图片
% pause;
fprintf('第%d次迭代\n',40-i);
end
相关文章推荐
- 解析在main函数之前调用函数以及对设计的作用详解
- C++如何调用matlab函数
- 详解Matlab中 sort 函数用法
- WPF调用Matlab函数的方法
- java和matlab画多边形闭合折线图示例讲解
- C#调用Matlab生成的dll方法的详细说明
- 简述Matlab中size()函数的用法
- 从java中调用matlab详细介绍
- Ubuntu 如何建立Matlab快捷方式
- 稀疏自动编码器 (Sparse Autoencoder)
- 详解Matlab中 sort 函数用法
- 简述Matlab中size()函数的用法
- VC++与Matlab混合编程的快速实现
- Matlab 矩阵运算
- matlab与opencv部分函数的对照
- matlab神经网络工具箱创建神经网络
- Matlab
- MATLAB 入门教程
- matlab函数_连通区域