function [featureVector] = makeScaledDepthFeatureTexture(img, type, scale, numberOfStatistics)
%write comment about function here

% This code is copyrighted by Stanford University.
% Author: Ashutosh Saxena, Justin Drieymeyer
% Code cannot be used for commercial purposes.
% Use of the code, if used with permission from authors, must acknowledge:
% Learning to Grasp Novel Objects using Vision,
% Ashutosh Saxena, Justin Driemeyer, Justin Kearns, Chioma Osondu, Andrew Y. Ng. In 10th International Symposium on Experimental Robotics, ISER, 2006.
% Robotic Grasping of Novel Obects,
% Ashutosh Saxena, Justing Driemeyer, Justin Kearns, Andrew Y. Ng, In Neural Information Processing Systems (NIPS), 2006.
% More details at: http://ai.stanford.edu/~asaxena/learninggrasp/
% Version 0.1:   Aug 2006.


global columnWidth nGridCol nGridRow rowWidth
global scale2Factor
global nDim nLaw nHarris

if nargin < 4
  numberOfStatistics = 1;
end

if nargin < 3
  scale = [1 2 5];
end
if nargin < 2
  type = 'laws';
end

nGCol = nGridCol;
nGRow = nGridRow;

numoverlaps = 0;

% If you want to work with
% reduced image size, just
% scale the image appropriately
% below.

numoverlaps = scale - 1;
edgefactor = (2*numoverlaps + 1);

% Assume that the image is correctly oriented.
imheight = size(img, 1);
imwidth  = size(img, 2);

% Step sizes in x and y
stepwidth  = imwidth/nGridCol;
stepheight = imheight/nGridRow;

%===========calculating filter banks
depthValid = img >= 0;
img = depthValid .* img;
[H2, H4, H1] = calculateFilterBanks(img, type);

nDim = size(H2, 3) + 0;
%forming spatial masks

%=========================================
%featureVectorTmp = zeros( nGRow, nGCol, 2*nLaw+3*nHarris);
featureVectorTmp = zeros( nGRow, nGCol, nDim);

% Here we account for missing depth values in the image
depthValid = depthValid(:, :, 1);
depthValidMultiplier = repmat(depthValid, [1 1 nDim]);
H2 = H2 .* depthValidMultiplier;

for g = 1:nGRow
  for c = 1:nGCol
    gridLeft  = round( (g - 1 - numoverlaps)*stepheight + 1);
    gridRight = round( (g + numoverlaps)*stepheight );
    normfactor = 1.0;
    if gridLeft < 1
      gridLeft = 1;
      normfactor = normfactor * edgefactor / (numoverlaps + g);
    elseif gridRight > (nGRow * stepheight)
      gridRight = round(nGRow*stepheight);
      normfactor = normfactor * edgefactor ...
                   / (numoverlaps + nGRow - g + 1);
    end
    gridTop = round( (c - 1 - numoverlaps)*stepwidth + 1);
    gridBot = round( (c + numoverlaps)*stepwidth );
    if gridTop < 1
      gridTop = 1;
      normfactor = normfactor * edgefactor / (numoverlaps + c);
    elseif gridBot > (nGCol * stepwidth)
      gridBot = round(nGCol*stepwidth);
      normfactor = normfactor * edgefactor ...
                   / (numoverlaps + nGCol - c + 1);
    end

    % Now calcuate the rescaling in the case of missing depth values
    numValid = sum( sum( depthValid(gridLeft:gridRight, gridTop:gridBot) ) );
    numWanted = length(gridLeft:gridRight) * length(gridTop:gridBot);
    if (numValid == 0)
      normfactor = 0;
    else
      normfactor = normfactor * numWanted / numValid;
    end

%LAWS-----------
    if nLaw
      featureVectorTmp(g,c,1:nLaw) = normfactor * sum( sum( H2( gridLeft:gridRight, gridTop:gridBot, :), 2), 1);
    end
    if nHarris
      %Can be optimized
      for row = gridLeft:gridRight
        for col = gridTop:gridBot;
          featureVectorTmp(g,c,2*nLaw+angleImage(row,col) ) = featureVectorTmp(g,c,2*nLaw+angleImage(row,col) ) + gradientMagImage(row,col);
        end
      end
      featureVectorTmp(g,c,2*nLaw+(1:nHarris) ) = featureVectorTmp(g,c,2*nLaw+(1:nHarris) ) ./ ...
                                                  (max( featureVectorTmp(g,c,2*nLaw+(1:nHarris) ) ) + upsilon);
    end

    
  end
end
 
featureVector = featureVectorTmp;
return;
