r_interp.m 2.75 KB
function r_interp(expNumber,camNumber,iFirst,nSkip,iEnd)
% R_INTERP - Uses the rectification data to interpolate RGB values on a
% regular grid and save it to an image.
% 
% Syntax:  r_interp(expNumber,camNumber,iFirst,nSkip,iEnd)
% 
% Inputs:
%    expNumber - The experiment identifier
%    camNumber - The camera number
%    iFirst    - The first image to be treated
%    nSkip     - The number of images skipped
%    iEnd      - The last image to be treated
%                (if iEnd is set to zero, the loop is done from iFirst to
%                 nFiles)
% 
% Example:
%    g_interp(201307231525,2,1000,5,4000)
% 
% Other m-files required: g_rect toolbox
%   (http://demeter.uqar.ca/g_rect)
% Subfunctions: none
% MAT-files required: the rectification parameter file
% 
% See also: G_RECT, R_UNDISTORT, R_ROTATE

% Author: Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% September 2013
% ______________________________________________________________________
%%
cstr = num2str(camNumber);
estr = num2str(expNumber);
nbF = 40;
nbFStr = num2str(nbF);

rootdir = ['/MP_oceanographie/MP/images/multicam_',nbFStr,'f'];
expdir  = [rootdir,'/',estr,'_multicam'];
camdir  = [expdir,'/cam',cstr];
origdir = [camdir,'/original']; %#ok<NASGU>
unddir  = [camdir,'/undistorted'];
intdir  = [camdir,'/interp'];
% intdir  = [camdir,'/interp_25'];  %25fps
calfile = [rootdir,'/calib/cam',cstr,'/cam',cstr,'_undistorted_rect.mat'];

%% Load the calibration file
load(calfile)

%% Creation of the grid
dx = 0.01;      % m
dy = 0.01;      % m

if camNumber == 1
    x1vec = -0.1:dx:4.1;
elseif camNumber == 2
    x1vec =  3.9:dx:8.1;
elseif camNumber == 3
    x1vec =  7.9:dx:12.1;
elseif camNumber == 4
    x1vec = 11.9:dx:16.1;
end
y1vec = -5.0:dy:1.0;
y1vec = y1vec';

[X1,Y1] = meshgrid(x1vec,y1vec);

cd(unddir)

imfiles = dir('im*.jpg');
nFiles  = size(imfiles,1);

x1 = LON(:); %#ok<NODEF>
y1 = LAT(:); %#ok<NODEF>

if ~exist(intdir,'dir')
    mkdir(intdir)
end

if iEnd == 0
    iEnd = nFiles;
end

for n = iFirst:nSkip:iEnd
    
    imfilename = imfiles(n).name;
    imNumber   = imfilename(3:7);
    
    I0 = double(imread(imfilename));
    
    R0 = I0(:,:,1)';
    G0 = I0(:,:,2)';
    B0 = I0(:,:,3)';
   
    r0 = R0(:);
    g0 = G0(:);
    b0 = B0(:);
    
    Fr = TriScatteredInterp(x1,y1,r0);
    Fg = TriScatteredInterp(x1,y1,g0);
    Fb = TriScatteredInterp(x1,y1,b0);

    R = flipud(Fr(X1,Y1));
    G = flipud(Fg(X1,Y1));
    B = flipud(Fb(X1,Y1));

    I(:,:,1) = R;
    I(:,:,2) = G;
    I(:,:,3) = B;
    
    I = uint8(I);
    
    imwrite(I,[intdir,'/im',imNumber,'_interp.jpg'],'jpg');
        
    disp([' ',imNumber]);
    
end