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

get_object_blobs

PURPOSE ^

SYNOPSIS ^

function [new_bw] = get_object_blobs(bw)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [new_bw] = get_object_blobs(bw)
0002 
0003 [h w] = size(bw);
0004 
0005 % setting the thresholds
0006 thresh_major_blob_ratio = 0.7;
0007 thresh_max_dist_bw_blobs = 0.7;
0008 thresh_min_blob_ratio = 0.1;
0009 thresh_min_blob_rel = 0.02;
0010 thresh_min_blob_abs = h*w*0.0001;
0011 
0012 thresh_filter = thresh_max_dist_bw_blobs * (1/thresh_min_blob_ratio) ...
0013     * thresh_major_blob_ratio;
0014 
0015 diag = sqrt(h*h + w*w);
0016 blob_prpts = regionprops(bw);
0017 num_blobs = length(blob_prpts);
0018 
0019 if num_blobs > 1
0020     blob_prpts = regionprops(bw, 'Area', 'Centroid', 'BoundingBox', 'Image');
0021     
0022     blob_cells = {};
0023     blob_area = nan(num_blobs, 1);
0024     blob_centers = nan(num_blobs, 2);
0025     filter_blob_list = [];
0026     
0027     
0028     for nbidx = 1:num_blobs
0029         tmp_mask = false(h,w);
0030         tmp_bounding_box = blob_prpts(nbidx).BoundingBox;
0031         [blob_h blob_w] = size(blob_prpts(nbidx).Image);
0032         x1 = max(1, floor(tmp_bounding_box(1)));
0033         y1 = max(1, floor(tmp_bounding_box(2)));
0034         x2 = min(w, x1+blob_w-1);
0035         y2 = min(h, y1+blob_h-1);
0036         
0037         tmp_mask(y1:y2, x1:x2) = blob_prpts(nbidx).Image > 0;
0038         tmp_blob = tmp_mask & bw;
0039         blob_cells{nbidx} = tmp_blob;
0040         
0041         blob_area(nbidx) = blob_prpts(nbidx).Area;        
0042         blob_centers(nbidx, :) = blob_prpts(nbidx).Centroid;
0043     end
0044 
0045     blob_area_sum = sum(blob_area);
0046     blob_area_ratio = blob_area / blob_area_sum;
0047     [v_major i_major] = max(blob_area_ratio);
0048     is_major_blob = v_major > thresh_major_blob_ratio;
0049     blob_dist2major_ratio = nan(num_blobs, 1);
0050     blob_filter_thresh = nan(num_blobs, 1);
0051 
0052     for nbidx = 1:num_blobs        
0053         if is_major_blob
0054             tmp_diff = blob_centers(i_major, :) - blob_centers(nbidx, :);
0055             tmp_dist = sqrt(sum(tmp_diff.*tmp_diff));            
0056             tmp_dist_ratio = tmp_dist / diag;
0057             blob_dist2major_ratio(nbidx) = tmp_dist_ratio;
0058             
0059             tmp_probs = tmp_dist_ratio * (1/blob_area_ratio(nbidx)) ...
0060                 * v_major;
0061             blob_filter_thresh(nbidx) = tmp_probs;
0062 
0063             if tmp_probs > thresh_filter
0064                 filter_blob_list = [filter_blob_list nbidx];
0065             end            
0066         end
0067         
0068         if blob_area_ratio(nbidx) < thresh_min_blob_rel || ...
0069                 blob_area(nbidx) < thresh_min_blob_abs
0070             filter_blob_list = [filter_blob_list nbidx];
0071         end
0072         
0073     end
0074 %     is_major_blob
0075 %     blob_area_ratio
0076 %     blob_area
0077 %     thresh_min_blob_abs
0078 %     filter_blob_list
0079 
0080     obj_blob_list = setdiff(1:num_blobs, filter_blob_list);
0081     new_bw = false(h,w);
0082     for nbidx = 1:length(obj_blob_list)
0083         
0084         new_bw = new_bw | blob_cells{obj_blob_list(nbidx)};
0085 %         imshow(blob_cells{obj_blob_list(nbidx)});
0086     end
0087 
0088 % blob_area_ratio
0089 % blob_dist2major_ratio
0090 % blob_filter_thresh
0091 %
0092 % num_blobs
0093 % filter_blob_list
0094 % imshow(new_bw)
0095 % pause
0096 
0097 else
0098     new_bw = bw;
0099 end
0100 
0101 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005