PC12_CROPIMG Automatically crops images Inputs: img = array of image data pad = amount of padding to use Outputs: cropimg = cropped image range = range of signal within the resulting 2D crop
0001 function [ cropimg, range ] = pc12_cropImg( img, pad) 0002 %PC12_CROPIMG Automatically crops images 0003 % 0004 %Inputs: 0005 % img = array of image data 0006 % pad = amount of padding to use 0007 % 0008 %Outputs: 0009 % cropimg = cropped image 0010 % range = range of signal within the resulting 2D crop 0011 0012 %Author:Unknown, Greg Johnson? Date:Unknown 0013 % Copyright (C) 2007-2013 Murphy Lab 0014 % Carnegie Mellon University 0015 % 0016 % This program is free software; you can redistribute it and/or modify 0017 % it under the terms of the GNU General Public License as published 0018 % by the Free Software Foundation; either version 2 of the License, 0019 % or (at your option) any later version. 0020 % 0021 % This program is distributed in the hope that it will be useful, but 0022 % WITHOUT ANY WARRANTY; without even the implied warranty of 0023 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0024 % General Public License for more details. 0025 % 0026 % You should have received a copy of the GNU General Public License 0027 % along with this program; if not, write to the Free Software 0028 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0029 % 02110-1301, USA. 0030 % 0031 % For additional information visit http://murphylab.web.cmu.edu or 0032 % send email to murphy@cmu.edu 0033 0034 0035 0036 if ~exist('padding', 'var') 0037 pad = 20; 0038 end 0039 0040 imsize = size(img); 0041 mask = img > 0; 0042 0043 %Crop the image so we dont waste extra compute time 0044 sumx = find(sum(sum(mask,3),1)); 0045 sumy = find(sum(sum(mask,3),2)); 0046 0047 xstart = sumx(1)-pad; 0048 if xstart < 1; xstart = 1; end 0049 0050 xend = sumx(end)+pad; 0051 if xend > imsize(2); xend = imsize(2); end 0052 0053 ystart = sumy(1)-pad; 0054 if ystart < 1; ystart = 1; end 0055 0056 yend = sumy(end)+pad; 0057 if yend > imsize(1); yend = imsize(1); end 0058 0059 0060 %Crop to remove empty space 0061 cropimg = img(ystart:yend, xstart:xend,:); 0062 0063 range = [ystart yend xstart xend]; 0064 0065 end 0066