function [RegionImage,RegionBinaryImage,RegionX,RegionY]=PreProcessRegion(RegionLoc,INPUT_IM,SmallObjThres,RoundThresh,EnableFiltering) % This function deletes the small and the rounded objects and creates % the images of the zone to be process by the entropyfilter. % Use: % [RegionImage,RegionBinaryImage]=PreProcessRegion(RegionLoc,INPUT_IM,SmallObjThres,EnableFiltering) % Where-- % RegionLoc=>Region area loaded from locations file % INPUT_IM =>Input Image variable % SmallObjThres => Threshold to define an object as small and delete it % RoundThresh => Coeff to define an object as round and delete it % EnableFiltering => Flag (1 or 0) to Enable filtering small and rounded % objects % (c) Gustavo Liņan Cembrano % IMSE-CNM-CSIC % linan@imse-cnm.csic.es Region=zeros(size(INPUT_IM,1),size(INPUT_IM,2)); Region(RegionLoc)=1; RegionProps=regionprops(Region,'centroid'); RegionX=RegionProps.Centroid(1); RegionY=RegionProps.Centroid(2); RegionBinaryImage=regionprops(Region,'Image'); RegionBinaryImage=RegionBinaryImage.Image; RegionBBox=regionprops(Region,'BoundingBox'); RegionBBox=RegionBBox.BoundingBox; RegionBBox=round(RegionBBox); RegionImage=uint8(zeros(size(RegionBinaryImage,1),... size(RegionBinaryImage,2),... 3)); RegionImage(:,:,1)=INPUT_IM(RegionBBox(2):RegionBBox(2)+RegionBBox(4)-1,... RegionBBox(1):RegionBBox(1)+RegionBBox(3)-1,... 1); RegionImage(:,:,2)=INPUT_IM(RegionBBox(2):RegionBBox(2)+RegionBBox(4)-1,... RegionBBox(1):RegionBBox(1)+RegionBBox(3)-1,... 2); RegionImage(:,:,3)=INPUT_IM(RegionBBox(2):RegionBBox(2)+RegionBBox(4)-1,... RegionBBox(1):RegionBBox(1)+RegionBBox(3)-1,... 3); if(EnableFiltering) %% Removing rounded&small objects from image and substituting them by average around them threshold=graythresh(RegionImage); bw = im2bw(RegionImage,threshold); %Erasing small objects bw2 = bwareaopen(bw,SmallObjThres); SmallObjects=xor(bw,bw2); [L,NUM] = bwlabel(SmallObjects,8); fprintf('Deleted %d Small Objects in Region\n',NUM); RegionBinaryImage=and(RegionBinaryImage,not(SmallObjects)); % fill a gap in the pen's cap se = strel('disk',2); bw = imclose(bw2,se); % fill any holes, so that regionprops can be used to estimate % the area enclosed by each of the boundaries bw = imfill(bw,'holes'); [B,L] = bwboundaries(bw,'noholes'); stats = regionprops(L,'Area','Centroid','PixelIdxList'); threshold = RoundThresh; %the closer to 1 the more round % loop over the boundaries Deleted=0; for k = 1:length(B) % obtain (X,Y) boundary coordinates corresponding to label 'k' boundary = B{k}; % compute a simple estimate of the object's perimeter delta_sq = diff(boundary).^2; perimeter = sum(sqrt(sum(delta_sq,2))); % obtain the area calculation corresponding to label 'k' area = stats(k).Area; % compute the roundness metric metric = 4*pi*area/perimeter^2; % mark objects above the threshold with a black circle if metric > threshold Deleted=Deleted+1; RegionBinaryImage(stats(k).PixelIdxList)=0; end end fprintf('Deleted %d round objects in Region\n',Deleted); end end