r_isolateCircle.m 5.99 KB
function  r_isolateCircle(expNumber,camNumber,rowNumber,nb,nbF)
% R_ISOLATECIRCLE - Isolate and identify objects in a batch of images
%
% Syntax:  r_isolateCircle(expNumber,camNumber,rowNumber,nb)
%
% Inputs:
%    expNumber  - The experiment number
%    camNumber  - The camera number
%    rowNumber  - The row number
%    nb         - Number of objects to be identified
%    nbF        - The number of floes (40 or 80)
%   
%
% Example:
%    r_isolateCircle(201307231525,1,1,5,80)
%
% Other m-files required: Image Processing Toolbox
% Subfunctions: none
% MAT-files required: list1.mat
%                     info.mat
%
% See also: r_createFloeCircle

% Author: Marie-Pier Marquis, Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% October 2014
% ______________________________________________________________________
%%
%%
cstr   = num2str(camNumber);
estr   = num2str(expNumber);
rstr   = num2str(rowNumber);     
nbFStr = num2str(nbF);

rootdir   = r_root(nbF);
listfile  = [rootdir, '/list1.mat'];
listfile2 = [rootdir, '/info.mat'];
load(listfile)
load(listfile2)
interp  = info.interp;
data    = info.data; % The save name
expdir  = [rootdir,'/',estr,'_multicam'];
camdir  = [expdir,'/cam',cstr];
rectdir = [camdir,interp]; 
cd(rectdir)

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

%% Cropping parameters
% Images are 421x601 pixels
% The domain defined by I(iCrop:jCrop) will be set to zero (black).
 iBlack2 = 1:2;
 jBlack2 = 1:2;
 iBlack3 = 1:2;
 jBlack3 = 1:2;
 iBlack4 = 1:2;
 jBlack4 = 1:2;
 iBlack5 = 1:2;
 jBlack5 = 1:2;
 
if camNumber == 1
    if rowNumber == 1
        iBlack = 1:601;
        jBlack = 109:421;
    elseif rowNumber == 2
        iBlack = 1:601;
         jBlack = [1:108,213:421];
    elseif rowNumber == 3
        iBlack = 1:601;
        jBlack = [1:213,307:421];
    elseif rowNumber == 4
        iBlack = 1:601;
        jBlack = 1:308;    
        
    elseif rowNumber == 99 % The collision experiment (201307311605)
        iBlack = 1:300;
        jBlack = 1:180;
    end
    
elseif camNumber == 2
    if rowNumber == 1
        iBlack  = 1:601;
        jBlack  = 112:421;
    elseif rowNumber == 2
        iBlack = 1:601;
        jBlack = [1:110,210:421];
    elseif rowNumber == 3
        iBlack = 1:601;
        jBlack = [1:210,307:421];
%         iBlack2 = 270:601;
%         jBlack2 = 1:225;
%         iBlack3 = 390:475;
%         jBlack3 = 300:350;
%         iBlack4 = 50:160;
%         jBlack4 = 1:215;
%         iBlack5 = 170:250;
%         jBlack5 = 300:350;
    elseif rowNumber == 4
        iBlack = 1:601;
        jBlack = [1:308,409:421];
%         iBlack2 = 290:400;
%         jBlack2 = 1:325;
    end
    
elseif camNumber == 3
    if rowNumber == 1
        iBlack = 1:601;
        jBlack = [1:13,107:421];
    elseif rowNumber == 2
        iBlack = 1:601;
        jBlack = [1:108,208:421];    
    elseif rowNumber == 3
        iBlack = 1:601;
        jBlack = [1:214,310:421];
    elseif rowNumber == 4
         iBlack = 1:601;
        jBlack = [1:308,412:421];
%         iBlack2 = 1:601;
%         jBlack2 = [1:310,410:421];
    end
    
elseif camNumber == 4
    if rowNumber == 1
        iBlack = 1:601;
        jBlack = [1:13,112:421];
    elseif rowNumber == 2
        iBlack = 1:601;
        jBlack = [1:109,210:421];
    elseif rowNumber == 3
      iBlack = 1:601;
        jBlack = [1:210,314:421]; 
    elseif rowNumber == 4
        iBlack = 1:601;
         jBlack = 1:315;
    end
    
end

xpos = nan.*ones(nFiles,nb);
ypos = nan.*ones(nFiles,nb);
t    = nan.*ones(nFiles,1);
m    = nan.*ones(nFiles,1);
for n = 1:nFiles
    
    imfilename = imfiles(n).name;
    imNumber   = imfilename(3:7);
    imtime     = str2double(imNumber)./25;    % seconds since im00001
    %% Crop images by blocking user-specified zones
    I0 = imread(imfilename);   
    I0(iBlack,jBlack,:) = 0;
    I0(iBlack2,jBlack2,:) = 0;
    I0(iBlack3,jBlack3,:) = 0;
    I0(iBlack4,jBlack4,:) = 0;
    I0(iBlack5,jBlack5,:) = 0;
    I2 = I0;   % On crée des copies pour la rangée 4 de la caméra 1
    %% Find all circles in the specidief row
    
    if camNumber == 1 && rowNumber == 4
%          iBlack6 = 190:270;
%          jBlack6 = 300:420;
%          I1(iBlack6,jBlack6,:) = 0;
%          iBlack7 = [1:170,280:601];
%          jBlack7 = 300:420;
%          I2(iBlack7,jBlack7,:) = 0;
%         [center1, radii1] = imfindcircles(I2,[43 60], ...
%       'ObjectPolarity','bright','Sensitivity',0.95,'EdgeThreshold',0.025);
       [center, radii] = imfindcircles(I0,[47 60],'ObjectPolarity',...
                        'bright','Sensitivity',0.944,'EdgeThreshold',0.02);
%          center = [center1;center2];
%          radii  = [radii1;radii2];    
    else
        [center, radii] = imfindcircles(I0,[47 55],'ObjectPolarity',...
                       'bright','Sensitivity',0.965,'EdgeThreshold',0.015);
    end
    
    %% Return an error if the number of objects found does not correspond 
    % to the user-specified number nb
    err = 0;
    foundNumber = size(center,1);
    if foundNumber > nb
        err = 1;
        disp([' * Found more objects (',num2str(foundNumber),...
            ') than expected (',num2str(nb),')']);
    end
    if foundNumber < nb
        err = 2;
        disp([' * Found fewer objects (',num2str(foundNumber),...
            ') than expected (',num2str(nb),')']);
    end
    
    %% Retrieve the position of the object
    x     = nan.*ones(foundNumber,1);
    y     = nan.*ones(foundNumber,1);
    for k=1:foundNumber
        x(k) = center(k,1);       % x position of the nb objects
        y(k) = center(k,2);       % y position of the nb objects
    end
    
    if err == 0;
        t(n,1)    = imtime;
        m(n,1)    = foundNumber;
        xpos(n,:) = x;
        ypos(n,:) = y;
    else
        t(n,1)    = imtime;
        m(n,1)    = foundNumber;
    end
    disp(imNumber);
    
end
%% Saving data

disp(' Saving data ...');
time   = t; %#ok<*NASGU>
nfloes = m;
save([expdir,'/',data,estr,'_',cstr,'_',rstr],'time','nfloes','xpos',...
    'ypos', 'nb','-v7.3'); % 5fps
disp('Done!');
end