function [relativeFeatureVector] = makeRelativeFeatureVector(img, type, scales) % Version 0.1, Feb 2006. % This code is copyrighted by Ashutosh Saxena, Stanford University % It is available for non-commercial use only. For commericial use, please % contact the authors. % Any publication or report resulting from Use of this code, should cite: % Learning Depth from Single Monocular Images, Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng, NIPS 2005. % 3-D Depth Reconstruction from a Single Still Image, Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng, To appear in IJCV 2007. % For more info and dataset, visit: http://ai.stanford.edu/~asaxena/learningdepth/ % For a better version of the algorithm, also visit: % http://ai.stanford.edu/~asaxena/reconstruction3d/ global columnWidth rowWidth nGridCol nGridRow global nDim nLaw global nStatistics global nHistBins global minHist maxHist stepHist numscales = length(scales); H1size = nDim / nStatistics; HistMinMax; relativeFeatureVector = zeros( nGridRow, nGridCol, H1size*numscales*nHistBins ); for s=1:numscales scale = scales(s); reductionScale = 1/(2*(scale-1) + 1); if scale ~= 1 resizedImg = imresize(img, reductionScale, 'bilinear'); else resizedImg = img; end resizedImg(:,:,1) = medfilt2(resizedImg(:,:,1), [5, 5], 'symmetric'); resizedImg(:,:,2) = medfilt2(resizedImg(:,:,2), [5, 5], 'symmetric'); resizedImg(:,:,3) = medfilt2(resizedImg(:,:,3), [5, 5], 'symmetric'); % numoverlaps = 0; % if scale == 2 % numoverlaps = scale - 1; % end numoverlaps = scale - 1; edgefactor = (2*numoverlaps + 1); % Assume that the image is correctly oriented. imheight = size(resizedImg, 1); imwidth = size(resizedImg, 2); % Step sizes in x and y stepwidth = imwidth/nGridCol; stepheight = imheight/nGridRow; %==================================================================== %==============Apply the Laws filters to the image=================== %==================================================================== L3 = [1 2 1]; E3 = [-1 0 1]; S3 = [-1 2 -1]; NB1 = [ -100, -100, 0, 100, 100; ... -100, -100, 0, 100, 100; ... -100, -100, 0, 100, 100; ... -100, -100, 0, 100, 100; ... -100, -100, 0, 100, 100 ]; NB2 = [ -100, 32, 100, 100, 100; ... -100,-78, 92, 100, 100; ... -100,-100, 0, 100, 100; ... -100,-100,-92, 78, 100; ... -100,-100,-100,-32, 100 ]; NB3 = -NB2'; NB4 = -NB1'; NB5 = -NB3(end:-1:1,:); NB6 = NB5'; ycbcr_img = double(rgb2ycbcr(resizedImg)); clear resizedImg; H = zeros( size(ycbcr_img,1), size(ycbcr_img,2), nLaw ); H(:,:,1) = conv2(ycbcr_img(:,:,1), L3'*L3, 'same'); H(:,:,2) = conv2(ycbcr_img(:,:,1), L3'*E3, 'same'); H(:,:,3) = conv2(ycbcr_img(:,:,1), L3'*S3, 'same'); H(:,:,4) = conv2(ycbcr_img(:,:,1), E3'*L3, 'same'); H(:,:,5) = conv2(ycbcr_img(:,:,1), E3'*E3, 'same'); H(:,:,6) = conv2(ycbcr_img(:,:,1), E3'*S3, 'same'); H(:,:,7) = conv2(ycbcr_img(:,:,1), S3'*L3, 'same'); H(:,:,8) = conv2(ycbcr_img(:,:,1), S3'*E3, 'same'); H(:,:,9) = conv2(ycbcr_img(:,:,1), S3'*S3, 'same'); H(:,:,10) = conv2(ycbcr_img(:,:,2), L3'*L3, 'same'); H(:,:,11) = conv2(ycbcr_img(:,:,3), L3'*L3, 'same'); H(:,:,12) = conv2(ycbcr_img(:,:,1), NB1, 'same'); H(:,:,13) = conv2(ycbcr_img(:,:,1), NB2, 'same'); H(:,:,14) = conv2(ycbcr_img(:,:,1), NB3, 'same'); H(:,:,15) = conv2(ycbcr_img(:,:,1), NB4, 'same'); H(:,:,16) = conv2(ycbcr_img(:,:,1), NB5, 'same'); H(:,:,17) = conv2(ycbcr_img(:,:,1), NB6, 'same'); %==================================================================== %==================The Laws' Filters Applied========================= %==================================================================== intstepheight = floor((2*numoverlaps+1)*stepheight); intstepwidth = floor((2*numoverlaps+1)*stepwidth); for g = 1:nGridRow for c = 1:nGridCol gridLeft = round( (g - 1 - numoverlaps)*stepheight + 1); gridRight = round( (g + numoverlaps)*stepheight ); tempstep = gridRight - gridLeft + 1; residue = tempstep - intstepheight; if residue == 1 gridRight = gridRight - residue; end if residue > 1 || residue < 0 residue display('oops'); end normfactor = 1.0; if gridLeft < 1 gridLeft = 1; normfactor = normfactor * edgefactor / (numoverlaps + g); elseif gridRight > (nGridRow * stepheight) gridRight = round(nGridRow * stepheight); normfactor = normfactor * edgefactor ... / (numoverlaps + nGridRow - g + 1); end gridTop = round( (c - 1 - numoverlaps)*stepwidth + 1); gridBot = round( (c + numoverlaps)*stepwidth ); tempstep = gridBot - gridTop + 1; residue = tempstep - intstepwidth; if residue == 1 gridBot = gridBot - residue; end if residue > 1 || residue < 0 residue display('oops'); end if gridTop < 1 gridTop = 1; normfactor = normfactor * edgefactor / (numoverlaps + c); elseif gridBot > (nGridCol * stepwidth) gridBot = round(nGridCol * stepwidth); normfactor = normfactor * edgefactor ... / (numoverlaps + nGridCol - c + 1); end % The Laws Histogram for h=1:nLaw tmpPatch = zeros(gridRight-gridLeft+1, gridBot-gridTop+1); tmpPatch = abs(H(gridLeft:gridRight, gridTop:gridBot, h)); relativeFeatureVector(g,c,... ((s-1)*nHistBins*H1size + (h-1)*nHistBins + 1):((s-1)*nHistBins*H1size + h*nHistBins)) = ... hist(tmpPatch(:), minHist(h):stepHist(h):maxHist(h)); end end end clear H; end return;