r_isolate.m 5.51 KB
function [err,nf,imout,imtime,x,y,area,shape,color,nb] = ...
    r_isolate(imfilename,nb,filter,iBlack,jBlack, camNumber)
% R_ISOLATE - Isolate and identify objects in an image
%
% Syntax:  [err,nf,imout,imtime,x,y,area,shape,color] = ...   
%                      r_isolate(imfile,nb,filter,iBlack,jBlack,camNumber)
%
% Inputs:
%    imfilename - The image file name
%    nb         - Number of objects to be identified
%    filter     - String specifying which color filters to use
%                 'r': red
%                 'g': green
%                 'b': blue
%                 'y': yellow
%    iBlack     - Selection of the x-axis to crop
%    jBlack     - Selection of the y-axis to crop
%    camNumber  - The camera number
%
% Outputs:
%    err        - Logical error value indicating the number of objects 
%                 founds
%                  0: no error
%                  1: found more objects than expected
%                  2: found fewer objects than expected
%    nf         - The number of objects found
%    imout      - The final logical image
%    imtime     - The time of creation of the file
%    x,y        - The x and y position of objects
%    area       - The area of objects
%    shape      - A number linked to the shape of the object
%                  1: circle
%                  2: square
%                  3: equilateral triangle
%    color      - A string indicating the color of the object
%
% Example:
%    [err,nf,imout,imtime,x,y,area,shape,color] = ...
%                                r_isolate('IMG_9733.JPG',4,'rgby',1,1,1);
%
% Other m-files required: Image Processing Toolbox
% Subfunctions: R_CAM_FILTER
% MAT-files required: none
%
% See also: R_BATCH, R_SHOW

% Author: Yannick Levesque, Dany Dumont, Marie-Pier Marquis
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% May 2013; Last revision: 17-Jul-2013, October 2014
% ______________________________________________________________________


%% Choose the color filters parameters for the specified camera

[rf , bf , gf , yf] = r_cam_filter(camNumber , filter,nb );



I0      = imread(imfilename);
[n m l] = size(I0); %#ok<NASGU>


%% Retrieve time information
imNumber = imfilename(3:7);
imtime   = str2double(imNumber)./25;    % seconds since im00001

%% Crop images by blocking user-specified zones
I0(iBlack,jBlack,:) = 0;
%% Black out the white line on the top of images on cam 2
if camNumber == 2   
    I0(1:45,1:421,:)    = 0;   
end
%% Extract RGB color components
R = squeeze(I0(:,:,1));
G = squeeze(I0(:,:,2));
B = squeeze(I0(:,:,3));

%% Create a binary image and apply color filters
I = zeros(n,m);
% Red filter
IR = I;
if rf.init
    IR( R     >= rf.Rmin & ...
        G     <= rf.Gmax & ...
        R - G >= rf.RG   & ...
        R - B >= rf.RB   ) = 255; 
end
% Green filter
IG = I;
if gf.init
    IG( R     >= gf.Rmin & R <= gf.Rmax & ...
        G     >= gf.Gmin & G <= gf.Gmax & ...
        B     >= gf.Bmin & B <= gf.Bmax & ...
        G - R >= gf.GR  ) = 255;
end
% Blue filter
IB = I;
if bf.init
    IB( B - R >= bf.BR  & ...
        B - G >= bf.BG  ) = 255;
end
% Yellow filter
IY = I;
if yf.init
    IY( G     >= yf.Gmin &  & ...
        G - B >= yf.GB  ) = 255;
end

%% Isolate objects
% Black out parts of images having less than M white adjacent pixels
%M = 5e3;

% For cam1
% M  = 20;
% T  = 20;
% For collisions
M  = 250;
T  = 250;

IR = bwareaopen(IR,T);
IG = bwareaopen(IG,T);
IB = bwareaopen(IB,T);
IY = bwareaopen(IY,M);
I  = IR + IG + IB + IY;

% Black out zones in which a circle of 20 pixels does not fit
I  = imfill (I,'holes');

% Count the number of objects in the binary image
cc = bwconncomp(I,8);

% Extract information from each object
data         = regionprops(cc,'Area','Centroid','Perimeter','BoundingBox');
center       = [data.Centroid];         % Position of the centroid
area         = [data.Area];             % Area of objects
perimeter    = [data.Perimeter];        % Perimeter of objects
%box          = [data.BoundingBox];      % Dimension of the smallest
                                         % rectangle containing an object

% Return an error if the number of objects found does not correspond to
% the user-specified number nb
err = 0;
nf  = size(area,2);
if nf > nb
    err = 1;
    disp([' * Found more objects (',num2str(nf),') than expected (',num2str(nb),')']);
end
if nf < nb
    err = 2;
    disp([' * Found fewer objects (',num2str(nf),') than expected (',num2str(nb),')']);
end

% Retrieve the shape of the object
gamma = 4.*pi.*area./(perimeter).^2;
shape = nan.*gamma;
shape(gamma > 0.9)                 = 1; % circle
shape(gamma < 0.85 & gamma > 0.70) = 2; % square
shape(gamma < 0.65 & gamma > 0.40) = 3; % triangle

% Retrieve the position and color of the object
x     = nan.*gamma;
y     = nan.*gamma;
color = nan.*gamma;
%color = cell(1,nf);

for k=1:nf
    x(k) = center(2.*k - 1);   % x position of the nb objects
    y(k) = center(2.*k);       % y position of the nb objects
    ii   = floor(x(k));
    jj   = floor(y(k));
    tol  = 1;
    
    if max(max(IR(jj-tol:jj+tol,ii-tol:ii+tol)));
        color(k)   = 1;
    end
    
    if max(max(IG(jj-tol:jj+tol,ii-tol:ii+tol)));
        color(k)   = 2;
    end
    
    if max(max(IB(jj-tol:jj+tol,ii-tol:ii+tol)));
        color(k)   = 3;
    end
    
    if max(max(IY(jj-tol:jj+tol,ii-tol:ii+tol)));
        color(k)   = 4;
    end
    
end

imout = I;

end