Home > data-import > supporting_functions > lib > cal_eye2obj_dist.m

cal_eye2obj_dist

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function [eye_mean_dist, eye_min_dist, center_mean_dist, center_min_dist] = cal_eye2obj_dist(this_eye_xy, obj_num, ratio, blob_cells, max_distance_half)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 Warning: this set of parameters are hard-coded

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [eye_mean_dist, eye_min_dist, center_mean_dist, center_min_dist] = cal_eye2obj_dist(this_eye_xy, obj_num, ratio, blob_cells, max_distance_half)
0002 
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 % Warning: this set of parameters are hard-coded
0005 eye_w = 640;
0006 eye_h = 480;
0007 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0008 
0009 eye_mean_dist = NaN(1, obj_num+2);
0010 eye_min_dist  = NaN(1, obj_num+2);
0011 center_mean_dist = NaN(1, obj_num+2);
0012 center_min_dist  = NaN(1, obj_num+2);
0013 
0014 [img_h, img_w] = size(blob_cells{2});
0015 
0016 if (eye_w*ratio ~= img_w) || (eye_h*ratio ~= img_h)
0017     error('Height and widtch ranges of eye data do not match with the recorded images');
0018 end
0019 
0020 if ~isempty(this_eye_xy)
0021     seg_eye_x = this_eye_xy(:,1)*ratio;
0022     seg_eye_y = this_eye_xy(:,2)*ratio;
0023     box_gaze_x_min = max(1, floor(seg_eye_x)-1);
0024     box_gaze_x_max = min(img_w, ceil(seg_eye_x)+1);
0025     box_gaze_y_min = max(1, floor(seg_eye_y)-1);
0026     box_gaze_y_max = min(img_h, ceil(seg_eye_y)+1);
0027 end
0028 
0029 seg_center_x = img_w/2;
0030 seg_center_y = img_h/2;
0031 if max_distance_half
0032     max_distance = sqrt(seg_center_x^2 + seg_center_y^2);
0033 else
0034     max_distance = sqrt(img_w^2 + img_h^2);
0035 end
0036 blob_cells{5} = blob_cells{2} | blob_cells{3} | blob_cells{4};
0037 
0038 for bidx = 2 : (obj_num+2)
0039     %spatial(bidx,frame_idx) = region_spatial_dist(segs{bidx}, 480, 8, 720, 18);
0040     % there could be multiple area for each object
0041     blob_one = blob_cells{bidx};
0042 %     if length(unique(blob_one)) > 2
0043 %         unique(blob_one)
0044 %         error('The input segmented blobs are not logical matrix')
0045 %     end
0046     area = regionprops(blob_one, 'Area', 'Centroid', 'PixelList', 'BoundingBox'); %, 'Image'
0047     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0048     %%%%%%%%%%%%%%% for debugging %%%%%%%%%%%%%%%%%%%%%
0049     % htmp = figure;
0050     % subplot(1,2,1);
0051     % imshow(bwlabel(blob_one));
0052     % subplot(1,2,2);
0053     % imshow(logical(blob_one));
0054     % pause
0055     % close(htmp)
0056     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0057     if ~isempty(this_eye_xy)
0058         mask_within_blob = blob_one(box_gaze_y_min:box_gaze_y_max, box_gaze_x_min:box_gaze_x_max);
0059         %             is_within_blob = false;
0060         if sum(~mask_within_blob) < 1
0061             is_within_blob = true;
0062         else
0063             is_within_blob = false;
0064         end
0065         
0066         if ~isnan(seg_eye_x) && ~isnan(seg_eye_y) && ~isempty(area)
0067             dmin_eye = max_distance; % a big number
0068             dsum_eye = 0;
0069             npixel_eye = 0;
0070             
0071             for n = 1 : size(area,1)
0072                 % compute the min
0073                 if ~is_within_blob
0074                     temp_eye = min(distance([seg_eye_x; seg_eye_y], area(n).PixelList'));
0075                     if (temp_eye < dmin_eye)
0076                         dmin_eye = temp_eye;
0077                     end
0078                 end
0079                 
0080                 dsum_eye = dsum_eye + area(n).Area * distance([seg_eye_x; seg_eye_y], area(n).Centroid');
0081                 npixel_eye = npixel_eye + area(n).Area;
0082             end
0083             
0084             if is_within_blob
0085                 eye_min_dist(bidx) = 0;
0086             else
0087                 eye_min_dist(bidx) = dmin_eye/max_distance;
0088             end
0089             
0090             eye_mean_dist(bidx) = (dsum_eye/npixel_eye)/max_distance;
0091         end
0092     end
0093     if ~isempty(area)
0094         dmin_center = max_distance;
0095         dsum_center = 0;
0096         npixel_center = 0;
0097         for n = 1 : size(area,1)
0098             % compute the min
0099             temp_center = min(distance([seg_center_x; seg_center_y], area(n).PixelList'));
0100             if (temp_center < dmin_center)
0101                 dmin_center = temp_center;
0102             end;
0103 
0104             dsum_center = dsum_center + area(n).Area * distance([seg_center_x; seg_center_y], area(n).Centroid');
0105             npixel_center = npixel_center + area(n).Area;
0106         end
0107 
0108         center_min_dist(bidx) = dmin_center/max_distance;
0109         if (npixel_center > 0)
0110             center_mean_dist(bidx) = (dsum_center/npixel_center)/max_distance;
0111         else
0112             center_mean_dist(bidx) = 1;
0113         end
0114     end
0115 end

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005