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

detect_color_object

PURPOSE ^

This function is the key part of color object detection

SYNOPSIS ^

function [blob_size, blob_center, blob_cells] = detect_color_object(img, agent_type, obj_num, obj_params) % blob_cells

DESCRIPTION ^

  This function is the key part of color object detection 
  You need to change this function for another experiment setting. 
 
  img: input RGB image
  seg_img: output segmented binary image. It actually consists of four
  images. 
 
  write by: txu@indiana.edu update date: Oct. 15, 2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [blob_size, blob_center, blob_cells] = detect_color_object(img, agent_type, obj_num, obj_params) % blob_cells
0002 %  This function is the key part of color object detection
0003 %  You need to change this function for another experiment setting.
0004 %
0005 %  img: input RGB image
0006 %  seg_img: output segmented binary image. It actually consists of four
0007 %  images.
0008 %
0009 %  write by: txu@indiana.edu update date: Oct. 15, 2013
0010 
0011 
0012 % convert from RGB to HSV
0013 hsv = rgb2hsv(img);
0014 h = hsv(:,:,1);
0015 s = hsv(:,:,2);
0016 v = hsv(:,:,3);
0017 
0018 %%%%%%%%%%%%%%%%%%%%%%%
0019 % img_hsv = figure;
0020 % subplot(1, 3, 1);
0021 % imshow(h);
0022 % title('Hue')
0023 % subplot(1, 3, 2);
0024 % imshow(s);
0025 % title('Saturation')
0026 % subplot(1, 3, 3);
0027 % imshow(v);
0028 % title('Value')
0029 % pause
0030 %%%%%%%%%%%%%%%%%%%%%%%
0031 
0032 % [img_h, img_w] = size(h);
0033 blob_size = nan(1, (obj_num+1));
0034 blob_center = nan(1, (obj_num+1)*2);
0035 
0036 % Roughly segment image according to thresholds
0037 blob_cells = cell(1, obj_num);
0038 bk   = (s < obj_params.bg_s_low) | (v < obj_params.bg_v_low);  % background
0039 blob_cells{2} = (h > obj_params.blue_h_low) & (h < obj_params.blue_h_high) ...
0040     & (s > obj_params.blue_s_low)  & ~bk; % blue  object
0041 if strcmp(agent_type, 'topdown')
0042     blob_cells{3} = (h > obj_params.green_h_low) & (h < obj_params.green_h_high) & (s > obj_params.green_s_low_topdown)  & ~bk; % green object
0043 else
0044     blob_cells{3} = (h > obj_params.green_h_low) & (h < obj_params.green_h_high) & (s > obj_params.green_s_low)  & ~bk; % green object
0045 end
0046 
0047 % if isfield(obj_params, 'red_h_low_child') || isfield(obj_params, 'red_h_low_parent')
0048 if strcmp(agent_type, 'child')
0049     if isfield(obj_params, 'red_h_orhigh_child')
0050         blob_cells{4} = ((h > obj_params.red_h_low_child) | (h < obj_params.red_h_orhigh_child)) ...
0051             & (s > obj_params.red_s_low_child)  & (v > obj_params.red_v_low_child) & ~bk; % red objects
0052     else
0053         blob_cells{4} = ((h > obj_params.red_h_low_child) & (h < obj_params.red_h_andhigh_child)) ...
0054             & (s > obj_params.red_s_low_child)  & (v > obj_params.red_v_low_child) & ~bk; % red objects
0055     end
0056 elseif strcmp(agent_type, 'parent')
0057     if isfield(obj_params, 'red_h_orhigh_parent')
0058         blob_cells{4} = ((h > obj_params.red_h_low_parent) | (h < obj_params.red_h_orhigh_parent)) ...
0059             & (s > obj_params.red_s_low_parent)  & (v > obj_params.red_v_low_parent) & ~bk; % red objects
0060     else
0061         blob_cells{4} = ((h > obj_params.red_h_low_parent) & (h < obj_params.red_h_andhigh_parent)) ...
0062             & (s > obj_params.red_s_low_parent)  & (v > obj_params.red_v_low_parent) & ~bk; % red objects
0063     end
0064 elseif strcmp(agent_type, 'topdown')
0065     blob_cells{4} = ((h > obj_params.red_h_low_parent) | (h < obj_params.red_h_orhigh_parent)) ...
0066         & (s > obj_params.red_s_low_topdown) & (v > obj_params.red_v_low_child) & ~bk;
0067 end
0068 
0069 skin = h < 0;
0070 
0071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0072 
0073 % skin = (h >= 0.967  |  h < 0.08)  & (v > 0.35) & (s > 0.11 & s < 0.6) &
0074 % ~bk; % skin color; not very well tuned yet.
0075 
0076 for oidx = 2:(obj_num+1)
0077     tmp_blob = blob_cells{1, oidx};
0078     % smooth the images
0079     tmp_blob = medfilt2(tmp_blob, [8 8]);
0080 
0081     % filter out small far blobs, get only the blobs that are the actual
0082     % object
0083     tmp_blob = get_object_blobs(tmp_blob);
0084     
0085     blob_cells{1, oidx} = tmp_blob;
0086     
0087     [blob_size(oidx), blob_center(1, (oidx*2-1):oidx*2)] = cal_blob_size_and_center(tmp_blob);
0088 end

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005