% imageVertOffsetCorr.m This function perform offset correction of image,
% to reallineate acoustic and optical center

function [ imageCorrected ] = imageVertOffsetCorr( image, radiusCorrection, verticalOffset )

imageHeigth         = size( image, 1 );
imageWidth          = size( image, 2 );

[ xImage, yImage ]  = meshgrid( 1 : imageWidth, 1 : imageHeigth );
middleIndexY        = round( imageWidth / 2 );                              % Find index of middle element
elevation           = yImage / middleIndexY * 180 - 90;                     % elevation from -90 to +90
yCorrected          = ( atand( ( radiusCorrection * sind(  elevation ) - verticalOffset ) ...
                      ./ ( radiusCorrection * cosd(  elevation ) ) ) + 90 ) / 180 * middleIndexY;
correctionMatrix    = cat( 3, xImage, yCorrected );
resamp              = makeresampler( 'linear', 'fill' );
imageCorrected      = uint8( tformarray( image, [], resamp, [2 1], [1 2], [], correctionMatrix, .3 ) );

fprintf( 'Check correction...then press any key to continue or Ctrl-C to abort\n\n' );
imshow( image )
title( 'Original' )
figure
imshow( imageCorrected )
title( 'Corrected' )
pause
close all
