Commit 0f098f65 authored by Dany Dumont's avatar Dany Dumont

g_stabilize a ete revise en profondeur.

parent e2875858
Examples/Lab/IMG_8368.jpg

2.73 MB | W: | H:

Examples/Lab/IMG_8368.jpg

775 KB | W: | H:

Examples/Lab/IMG_8368.jpg
Examples/Lab/IMG_8368.jpg
Examples/Lab/IMG_8368.jpg
Examples/Lab/IMG_8368.jpg
  • 2-up
  • Swipe
  • Onion skin
......@@ -167,12 +167,14 @@ fprintf('\n')
% image(imread(imgFname));
imagesc(imread(imgFname));
colormap(gray);
hold on
for i = 1:ngcp
text(i_gcp(i),j_gcp(i),num2str(i),'color','r','horizontalalignment','center');
plot(i_gcp(i),j_gcp(i),'r.');
text(i_gcp(i),j_gcp(i),num2str(i),'color','r','horizontalalignment','right');
end
title('Ground Control Points','color','r');
print -dpng image1.png
print -dpng -r300 image1.png
fprintf('\n')
ok = input('Is it ok to proceed with the rectification (y/n): ','s');
......@@ -359,11 +361,12 @@ else
end
%%
% Compute the effective resolution
Delta = g_res(LON, LAT, field);
fprintf('\n')
fprintf('Saving rectification file in: %s\n',outputFname);
errGeoFit = 0.0;
save(outputFname,'imgFname','firstImgFname','lastImgFname',...
'LON','LAT',...
'LON0','LAT0',...
......@@ -371,7 +374,7 @@ save(outputFname,'imgFname','firstImgFname','lastImgFname',...
'i_gcp','j_gcp',...
'hfov','lambda','phi','H','theta',...
'errGeoFit','errPolyFit',...
'precision');
'precision','Delta');
clear LON LAT
......
%function Delta = g_res(LON,LAT,i,j,field);
function [Delta DeltaX DeltaY] = g_res(LON,LAT,i,j,field);
[m n] = size(LON);
if i == 1
Delta1 = g_dist(LON(i+1,j),LAT(i+1,j),LON(i,j),LAT(i,j),field);
elseif i == m
Delta1 = g_dist(LON(i,j),LAT(i,j),LON(i-1,j),LAT(i-1,j),field);
else
Delta1 = g_dist(LON(i+1,j),LAT(i+1,j),LON(i-1,j),LAT(i-1,j),field);
Delta1 = Delta1/2;
end
if j == 1
Delta2 = g_dist(LON(i,j+1),LAT(i,j+1),LON(i,j),LAT(i,j),field);
elseif j == n
Delta2 = g_dist(LON(i,j),LAT(i,j),LON(i,j-1),LAT(i,j-1),field);
else
Delta2 = g_dist(LON(i,j+1),LAT(i,j+1),LON(i,j-1),LAT(i,j-1),field);
Delta2 = Delta2/2;
end
Delta = sqrt(Delta1^2 + Delta2^2);
DeltaX = sqrt(Delta1^2);
DeltaY = sqrt(Delta2^2);
function res = g_res_field(LON, LAT, field)
% g_res_field - Function calculating the loss of resolution with de distance
% from the camera
%
% Inputs:
%
% LON : Longitude matrix obtained from g_rect
% LAT : Latitude matrix obtained from g_rect
% field : 'true' for field case. 'false' for lab case.
%
% Outputs:
%
% res : The field of resolution which is degrading with distance from
% the camera
%
% Author: Daniel Bourgault
% Institut des sciences de la mer de Rimouski
%
% email: daniel_bourgault@uqar.ca
% Website: http://demeter.uqar.ca/g_rect/
% February 2013
%
% Author: Elie Dumas-Lefebvre
% Institut des Science de la Mer de Rimouski
%
% Note: Matricial formulation of the calculation for a faster execution
%
% email: elie.dumas-lefebvre@uqar.ca
% March 2019
%
% LAT and LON difference in the x-axis
dLATi = diff(LAT, 1, 2);
dLONi = diff(LON, 1, 2);
% LAT and LON difference in the y-axis
dLATj = diff(LAT, 1, 1);
dLONj = diff(LON, 1, 1);
% Mean LAT in both x and y axis
LAT_meani = 0.5*(LAT(:, 2:end) + LAT(:, 1:end-1));
LAT_meanj = 0.5*(LAT(2:end, :) + LAT(1:end-1, :));
% Add a point at boundaries such that the resolution matrix
% has the same size of the LAT LON matrices
dLATi(:,end+1) = dLATi(:,end);
dLONi(:,end+1) = dLONi(:,end);
dLATj(end+1,:) = dLATj(end,:);
dLONj(end+1,:) = dLONj(end,:);
LAT_meani(:,end+1) = LAT_meani(:,end);
LAT_meanj(end+1,:) = LAT_meani(end,:);
% Conversion from degree to meters
meterPerDegLat = 1852*60.0;
meterPerDegLoni = meterPerDegLat.*cosd(LAT_meani);
meterPerDegLonj = meterPerDegLat.*cosd(LAT_meanj);
if field
% Conversion from degrees to meters
dxi = dLONi.*meterPerDegLoni;
dxj = dLONj.*meterPerDegLonj;
dyi = dLATi.*meterPerDegLat;
dyj = dLATj.*meterPerDegLat;
else
% Lab case. Data are already in meters. No conversion required.
dxi = dLONi;
dxj = dLONj;
dyi = dLATi;
dyj = dLATj;
end
% Distances in x and y axis
deltai = sqrt(dxi.^2 + dyi.^2);
deltaj = sqrt(dxj.^2 + dyj.^2);
% field of resolution
res = sqrt(deltai.^2 + deltaj.^2);
end
\ No newline at end of file
function[ A, T ] = g_computemotion( fx, fy, ft, roi )
[ydim,xdim] = size(fx);
[x,y] = meshgrid( [1:xdim]-xdim/2, [1:ydim]-ydim/2 );
%%% TRIM EDGES
fx = fx( 3:end-2, 3:end-2 );
fy = fy( 3:end-2, 3:end-2 );
ft = ft( 3:end-2, 3:end-2 );
fx = fx( 3:end-2, 3:end-2 );
fy = fy( 3:end-2, 3:end-2 );
ft = ft( 3:end-2, 3:end-2 );
roi = roi( 3:end-2, 3:end-2 );
x = x( 3:end-2, 3:end-2 );
y = y( 3:end-2, 3:end-2 );
x = x( 3:end-2, 3:end-2 );
y = y( 3:end-2, 3:end-2 );
ind = find( roi > 0 );
x = x(ind); y = y(ind);
fx = fx(ind); fy = fy(ind); ft = ft(ind);
xfx = x.*fx; xfy = x.*fy; yfx = y.*fx; yfy = y.*fy;
M(1,1) = sum( xfx .* xfx ); M(1,2) = sum( xfx .* yfx ); M(1,3) = sum( xfx .* xfy );
M(1,4) = sum( xfx .* yfy ); M(1,5) = sum( xfx .* fx ); M(1,6) = sum( xfx .* fy );
M(2,1) = M(1,2); M(2,2) = sum( yfx .* yfx ); M(2,3) = sum( yfx .* xfy );
M(2,4) = sum( yfx .* yfy ); M(2,5) = sum( yfx .* fx ); M(2,6) = sum( yfx .* fy );
M(3,1) = M(1,3); M(3,2) = M(2,3); M(3,3) = sum( xfy .* xfy );
M(3,4) = sum( xfy .* yfy ); M(3,5) = sum( xfy .* fx ); M(3,6) = sum( xfy .* fy );
M(4,1) = M(1,4); M(4,2) = M(2,4); M(4,3) = M(3,4);
M(4,4) = sum( yfy .* yfy ); M(4,5) = sum( yfy .* fx ); M(4,6) = sum( yfy .* fy );
M(5,1) = M(1,5); M(5,2) = M(2,5); M(5,3) = M(3,5);
M(5,4) = M(4,5); M(5,5) = sum( fx .* fx ); M(5,6) = sum( fx .* fy );
M(6,1) = M(1,6); M(6,2) = M(2,6); M(6,3) = M(3,6);
M(6,4) = M(4,6); M(6,5) = M(5,6); M(6,6) = sum( fy .* fy );
x = x(ind);
y = y(ind);
fx = fx(ind);
fy = fy(ind);
ft = ft(ind);
xfx = x.*fx;
xfy = x.*fy;
yfx = y.*fx;
yfy = y.*fy;
M(1,1) = sum( xfx .* xfx );
M(1,2) = sum( xfx .* yfx );
M(1,3) = sum( xfx .* xfy );
M(1,4) = sum( xfx .* yfy );
M(1,5) = sum( xfx .* fx );
M(1,6) = sum( xfx .* fy );
M(2,1) = M(1,2);
M(2,2) = sum( yfx .* yfx );
M(2,3) = sum( yfx .* xfy );
M(2,4) = sum( yfx .* yfy );
M(2,5) = sum( yfx .* fx );
M(2,6) = sum( yfx .* fy );
M(3,1) = M(1,3);
M(3,2) = M(2,3);
M(3,3) = sum( xfy .* xfy );
M(3,4) = sum( xfy .* yfy );
M(3,5) = sum( xfy .* fx );
M(3,6) = sum( xfy .* fy );
M(4,1) = M(1,4);
M(4,2) = M(2,4);
M(4,3) = M(3,4);
M(4,4) = sum( yfy .* yfy );
M(4,5) = sum( yfy .* fx );
M(4,6) = sum( yfy .* fy );
M(5,1) = M(1,5);
M(5,2) = M(2,5);
M(5,3) = M(3,5);
M(5,4) = M(4,5);
M(5,5) = sum( fx .* fx );
M(5,6) = sum( fx .* fy );
M(6,1) = M(1,6);
M(6,2) = M(2,6);
M(6,3) = M(3,6);
M(6,4) = M(4,6);
M(6,5) = M(5,6);
M(6,6) = sum( fy .* fy );
k = ft + xfx + yfy;
b(1) = sum( k .* xfx ); b(2) = sum( k .* yfx );
b(3) = sum( k .* xfy ); b(4) = sum( k .* yfy );
b(5) = sum( k .* fx ); b(6) = sum( k .* fy );
b(1) = sum( k .* xfx );
b(2) = sum( k .* yfx );
b(3) = sum( k .* xfy );
b(4) = sum( k .* yfy );
b(5) = sum( k .* fx );
b(6) = sum( k .* fy );
%v = inv(M) * b';
v = M\b';
%A = [1 0 ; 0 1];
%A = [1 0 ; 0 1]; % For translation only
A = [v(1) v(2) ; v(3) v(4)];
T = [v(5) ; v(6)];
\ No newline at end of file
T = [v(5) ; v(6)];
function[ f ] = g_down( f, L );
blur = [1 2 1]/4;
for k = 1 : L
f = conv2( conv2( f, blur, 'same' ), blur', 'same' );
f = f(1:2:end,1:2:end);
f = conv2( conv2( f, blur, 'same' ), blur', 'same' );
f = f(1:2:end,1:2:end);
end
\ No newline at end of file
function[ Acum, Tcum ] = g_opticalflow( f1, f2, roi, L )
f2orig = f2;
Acum = [1 0 ; 0 1];
Tcum = [0 ; 0];
for k = L : -1 : 0
%for k = L : -1 : L
%%% DOWN-SAMPLE
f1d = g_down( f1, k );
f2d = g_down( f2, k );
ROI = g_down( roi, k );
%%% COMPUTE MOTION
[Fx,Fy,Ft] = g_spacetimederiv( f1d, f2d );
[A,T] = g_computemotion( Fx, Fy, Ft, ROI );
T = (2^k) * T;
[Acum,Tcum] = g_accumulatewarp( Acum, Tcum, A, T );
%%% WARP ACCORDING TO ESTIMATED MOTION
f2 = g_warp( f2orig, Acum, Tcum );
......
......@@ -58,4 +58,4 @@ roi(ij) = 1;
figure(2);
imagesc(roi);
save roi.mat roi imgFname
\ No newline at end of file
save([imgFname(1:end-4),'_roi.mat'],'roi','imgFname');
\ No newline at end of file
This diff is collapsed.
%%% STABILIZE VIDEO
function[ motion, stable ] = g_videostabilize( frames, roi, L )
N = length( frames );
roiorig = roi;
%%% ESTIMATE PAIRWISE MOTION
Acum = [1 0 ; 0 1];
Tcum = [0 ; 0];
......@@ -13,6 +15,7 @@ for k = 1 : N-1
[Acum,Tcum] = g_accumulatewarp( Acum, Tcum, A, T );
roi = g_warp( roiorig, Acum, Tcum );
end
%%% STABILIZE TO LAST FRAME
stable(N).im = frames(N).im;
Acum = [1 0 ; 0 1];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment