FASTDEPTHSEGMENTATIONDSKCF_INITFRAME.m segments depth data FASTDEPTHSEGMENTATIONDSKCF_INITFRAME function applies the fast depth segmentation algorithm described in [1]. The segmentation is composed by two different stage a fast version of the Kmeans applied to the depth data, plus a connected component analysis to refine clusters in the image plane. This function was implemented by starting from the "Fast segmentation of N-dimensional grayscale images" presented by Anton Semechko and shared in the Matlab Central at this link under BSD licence http://www.mathworks.com/matlabcentral/fileexchange/41967-fast-segmentation-of-n-dimensional-grayscale-images INPUT: - im depth image coded in 16bits, each pixel contains mm data. - c positive interger greater than 1 specifying the number of clusters. c=2 is the default setting. Alternatively, c initialized by considering peaks in the depth distribution. -nanMatrix binary mask containing flags for missing depth pixels -minimumError convergence criteria for the Kmeans algorithm -histStep histogram bin used to compose depth histogram -Cinit initial Kmeans seeds. Set this values to -1 to not initialize externally the starting seeds -findPeak boolean flag to initialize the Kmeans seeds with the peaks of the depth distribution OUTPUT - L label image of the same size as the input image. For example, L==i represents the region associated with prototype C(i), where i=[1,k] (k = number of clusters). - C 1-by-k array of cluster centroids. - LUT L-by-1 array that specifies the intensity-class relations, where L is the dynamic intensity range of the input image. Specifically, LUT(1) corresponds to class assigned to min(im(:)) and LUT(L) corresponds to the class assigned to max(im(:)). -H histogram's bins height -I histogram's bins centers See also LUT2LABELNANSUPPORT, INITDISTRIBUTIONFAST [1] S. Hannuna, M. Camplani, J. Hall, M. Mirmehdi, D. Damen, T. Burghardt, A. Paiement, L. Tao, DS-KCF: A real-time tracker for RGB-D data, Journal of Real-Time Image Processing University of Bristol Massimo Camplani and Sion Hannuna massimo.camplani@bristol.ac.uk hannuna@compsci.bristol.ac.uk
0001 function [L,C,LUT,H,I]=fastDepthSegmentationDSKCF_initFrameV2(im,c,nanMatrix,minimumError,histStep,Cinit, findPeak) 0002 % FASTDEPTHSEGMENTATIONDSKCF_INITFRAME.m segments depth data 0003 % 0004 % FASTDEPTHSEGMENTATIONDSKCF_INITFRAME function applies the fast depth 0005 % segmentation algorithm described in [1]. The segmentation is composed 0006 % by two different stage a fast version of the Kmeans applied to the 0007 % depth data, plus a connected component analysis to refine clusters in 0008 % the image plane. This function was implemented by starting from the 0009 % "Fast segmentation of N-dimensional grayscale images" presented by 0010 % Anton Semechko and shared in the Matlab Central at this link under BSD 0011 % licence 0012 % http://www.mathworks.com/matlabcentral/fileexchange/41967-fast-segmentation-of-n-dimensional-grayscale-images 0013 % 0014 % INPUT: 0015 % - im depth image coded in 16bits, each pixel contains mm data. 0016 % - c positive interger greater than 1 specifying the number of 0017 % clusters. c=2 is the default setting. Alternatively, c 0018 % initialized by considering peaks in the depth distribution. 0019 % -nanMatrix binary mask containing flags for missing depth pixels 0020 % -minimumError convergence criteria for the Kmeans algorithm 0021 % -histStep histogram bin used to compose depth histogram 0022 % -Cinit initial Kmeans seeds. Set this values to -1 to not initialize 0023 % externally the starting seeds 0024 % -findPeak boolean flag to initialize the Kmeans seeds with the peaks of 0025 % the depth distribution 0026 % 0027 % OUTPUT 0028 % - L label image of the same size as the input image. For example, 0029 % L==i represents the region associated with prototype C(i), 0030 % where i=[1,k] (k = number of clusters). 0031 % - C 1-by-k array of cluster centroids. 0032 % - LUT L-by-1 array that specifies the intensity-class relations, 0033 % where L is the dynamic intensity range of the input image. 0034 % Specifically, LUT(1) corresponds to class assigned to 0035 % min(im(:)) and LUT(L) corresponds to the class assigned to 0036 % max(im(:)). 0037 % -H histogram's bins height 0038 % -I histogram's bins centers 0039 % 0040 % See also LUT2LABELNANSUPPORT, INITDISTRIBUTIONFAST 0041 % 0042 % [1] S. Hannuna, M. Camplani, J. Hall, M. Mirmehdi, D. Damen, T. 0043 % Burghardt, A. Paiement, L. Tao, DS-KCF: A real-time tracker for RGB-D 0044 % data, Journal of Real-Time Image Processing 0045 % 0046 % 0047 % University of Bristol 0048 % Massimo Camplani and Sion Hannuna 0049 % 0050 % massimo.camplani@bristol.ac.uk 0051 % hannuna@compsci.bristol.ac.uk 0052 0053 % Default input arguments 0054 if nargin<2 || isempty(c), c=2; end 0055 0056 % Basic error checking 0057 if nargin<1 || isempty(im) 0058 error('Insufficient number of input arguments') 0059 end 0060 msg='Revise variable used to specify class centroids. See function documentaion for more info.'; 0061 if ~isnumeric(c) || ~isvector(c) 0062 error(msg) 0063 end 0064 if numel(c)==1 && (~isnumeric(c) || round(c)~=c || c<2) 0065 error(msg) 0066 end 0067 0068 % Check image format 0069 if isempty(strfind(class(im),'int')) 0070 error('Input image must be specified in integer format (e.g. uint8, int16)') 0071 end 0072 if sum(isnan(im(:)))~=0 || sum(isinf(im(:)))~=0 0073 error('Input image contains NaNs or Inf values. Remove them and try again.') 0074 end 0075 0076 %exclude from the clustering pixels with missing depth data 0077 newPointSet=im(~nanMatrix); 0078 Imin=double(min(newPointSet)); 0079 Imax=double(max(newPointSet)); 0080 0081 %calculate the histograms 0082 I=(Imin:histStep:Imax)'; 0083 if(I(end)~=Imax) 0084 I(end+1)=Imax+histStep; 0085 end 0086 %I=I-histStep/2; 0087 % Compute intensity histogram 0088 H=hist(double(newPointSet),I); 0089 H=H(:); 0090 maxValue=max(H); 0091 0092 %default parameters for the first frame as we cannot use any noise model 0093 %since the target depth peak is unknown.... 0094 0095 [peakDepth,posPeak]=findpeaks([0; H ;0],'MINPEAKDISTANCE',5,'MINPEAKHEIGHT',0.02*maxValue); 0096 % Initialize cluster centroids 0097 if numel(c)>1 0098 C=c; 0099 c=numel(c); 0100 else 0101 dI=(Imax-Imin)/c; 0102 if(isempty(Cinit)) 0103 C=Imin+dI/2:dI:Imax; 0104 else 0105 C=Cinit; 0106 end 0107 end 0108 0109 %initialize with the histogram's peaks 0110 if(findPeak) 0111 if(length(C)==length(posPeak) && C(1)==-1); 0112 C=I(posPeak-1); 0113 elseif (length(C)~=length(posPeak)) 0114 c=length(posPeak); 0115 C=I(posPeak-1); 0116 end 0117 C=C'; 0118 end 0119 unusedPOINTS=[]; 0120 if(length(C)==1) 0121 unusedPOINTS=find(H<H(posPeak-1)*0.1); 0122 H(unusedPOINTS)=0; 0123 end 0124 0125 % Update cluster centroids 0126 IH=I.*H; dC=Inf; 0127 0128 C0=C; 0129 Citer=C; 0130 0131 %KMEANS applied to the depth histogram 0132 while dC>minimumError 0133 0134 Citer=C; 0135 0136 % Distance to the centroids 0137 D=abs(bsxfun(@minus,I,C)); 0138 0139 % Classify by proximity 0140 [Dmin,LUT]=min(D,[],2); 0141 for j=1:c 0142 C(j)=sum(IH(LUT==j))/sum(H(LUT==j)); 0143 if(isnan(C(j))) 0144 C(j)=Citer(j); 0145 end 0146 end 0147 0148 % Change in centroids 0149 dC=max(abs(C-Citer)); 0150 0151 end 0152 0153 if(length(posPeak)==1) 0154 C=C0; 0155 LUT(unusedPOINTS)=0; 0156 end 0157 %given the depth segmentation, assign each pixel to the corresponding cluster 0158 L=LUT2labelNanSupport(im,LUT,nanMatrix,histStep); 0159