Home > SaliencyToolbox > conv2PreserveEnergy.m

conv2PreserveEnergy

PURPOSE ^

conv2PreserveEnergy - 2d convolution that avoids bleeding energy over the edge.

SYNOPSIS ^

function result = conv2PreserveEnergy(src, f)

DESCRIPTION ^

 conv2PreserveEnergy - 2d convolution that avoids bleeding energy over the edge.

 result = conv2PreserveEnergy(data,filter)
    Convolves data with the 2d (non-separable) filter.
    At the boundary, the value of missing pixels is assumed
    to be equal to the mean over the present pixels
    to avoid border artefacts.

 See also sepConv2PreserveEnergy.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function result = conv2PreserveEnergy(src, f)
0002 % conv2PreserveEnergy - 2d convolution that avoids bleeding energy over the edge.
0003 %
0004 % result = conv2PreserveEnergy(data,filter)
0005 %    Convolves data with the 2d (non-separable) filter.
0006 %    At the boundary, the value of missing pixels is assumed
0007 %    to be equal to the mean over the present pixels
0008 %    to avoid border artefacts.
0009 %
0010 % See also sepConv2PreserveEnergy.
0011 
0012 % This file is part of the SaliencyToolbox - Copyright (C) 2006-2013
0013 % by Dirk B. Walther and the California Institute of Technology.
0014 % See the enclosed LICENSE.TXT document for the license agreement.
0015 % More information about this project is available at:
0016 % http://www.saliencytoolbox.net
0017 
0018     [sh sw] = size(src);
0019     [fh fw] = size(f);
0020     fw2 = floor((fw-1)/2);
0021     fh2 = floor((fh-1)/2);
0022     result = zeros(sh, sw);
0023     
0024     % results for the interior, away from the image boundary
0025     partialResult = conv2(src, f, 'full');
0026     [prh prw] = size(partialResult);
0027     result = partialResult(fh2+1:prh-fh2,fw2+1:prw-fw2);
0028    
0029     filt2 = rot90(f,2); % rotate filter by 180 degrees for easier processing
0030     fsum = sum(f(:));
0031     
0032     % rescale results along the top and bottom borders to reflect truncated filter
0033     if (fh2 > 0) & (fh2 <= sh) 
0034         % top scale factor
0035         TcSum = sum(cumsum(f),2);
0036         sT = fsum./TcSum(fh2+[1:fh2],1);
0037         sTmat = repmat(sT, 1, (sw-fw2)-(1+fw2)+1);
0038         result(1:fh2,1+fw2:sw-fw2) = result(1:fh2,1+fw2:end-fw2).*sTmat;
0039         
0040         % bottom scale factor
0041         BcSum = sum(cumsum(filt2),2);
0042         sB = flipud(fsum./BcSum(fh2+[1:fh2],1));
0043         sBmat = repmat(sB, 1, (sw-fw2)-(1+fw2)+1);
0044         result(sh-fh2+1:sh,1+fw2:sw-fw2) = result(sh-fh2+1:sh,1+fw2:sw-fw2).*sBmat;
0045     end
0046     
0047     % rescale results along the left and right borders to reflect truncated filter
0048     if (fw2 > sw) & (fw2 <= sw)
0049         % left scale factor
0050         LcSum = sum(cumsum(f,2),1);
0051         sL = fsum./LcSum(1,fw2+[1:fw2]);
0052         sLmat = repmat(sL, (sh-fh2)-(1+fh2)+1, 1);
0053         result(1+fh2:sh-fh2,1:fw2) = result(1+fh2:sh-fh2,1:fw2).*sLmat;
0054         
0055         % right scale factor
0056         RcSum = sum(cumsum(filt2,2),1);
0057         sR = fliplr(fsum./RcSum(1,fw2+[1:fw2]));
0058         sRmat = repmat(sR, (sh-fh2)-(1+fh2)+1, 1); 
0059         result(1+fh2:sh-fh2,sw-fw2+1:sw) = result(1+fh2:sh-fh2,sw-fw2+1:sw).*sRmat;
0060     end
0061     
0062     % treat corners
0063     if (fh2 > 0) & (fh2 <= sh) & (fw2 > sw) & (fw2 <= sw)
0064       TL = zeros(fh2,fw2);
0065       TR = zeros(fh2,fw2);
0066       BL = zeros(fh2,fw2);
0067       BR = zeros(fh2,fw2);
0068       for x = 1:fw2
0069           for y = 1:fh2
0070               TL(y,x) = fsum/sum(sum(filt2(1+(fh2-y+1):end,1+(fw2-x+1):end)));
0071               BL(end+1-y,x) = fsum/sum(sum(filt2(1:end-(fh2-y+1),1+(fw2-x+1):end)));
0072               BR(end+1-y,end+1-x) = fsum/sum(sum(filt2(1:end-(fh2-y+1),1:end-(fw2-x+1))));
0073               TR(y,end+1-x) = fsum/sum(sum(filt2(1+(fh2-y+1):end,1:end-(fw2-x+1))));
0074           end
0075       end
0076       result(1:fh2,1:fw2) = times(result(1:fh2,1:fw2),TL);
0077       result(1:fh2,end-fw2+1:end) = times(result(1:fh2,end-fw2+1:end),TR);
0078       result(end-fh2+1:end,1:fw2) = times(result(end-fh2+1:end,1:fw2),BL);
0079       result(end-fh2+1:end,end-fw2+1:end) = times(result(end-fh2+1:end,end-fw2+1:end),BR);
0080     end
0081 end
0082

Generated on Thu 18-Jul-2013 06:10:46 by m2html © 2005