Package pyvision :: Package other :: Module normalize
[hide private]
[frames] | no frames]

Source Code for Module pyvision.other.normalize

  1  # PyVision License 
  2  # 
  3  # Copyright (c) 2006-2008 David S. Bolme 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  #  
 10  # 1. Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  #  
 13  # 2. Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  #  
 17  # 3. Neither name of copyright holders nor the names of its contributors 
 18  # may be used to endorse or promote products derived from this software 
 19  # without specific prior written permission. 
 20  #  
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 23  # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 24  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 25  # A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR 
 26  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 27  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 28  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 30  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 31  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 32  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  #from math import * 
 35  #import scipy as sp 
 36  import scipy.ndimage as ndi 
 37  import numpy as np 
 38  import pyvision as pv 
 39  import cv 
 40   
41 -def normalizeMeanStd(matrix):
42 ''' TODO: deprecated please use meanStd.''' 43 print '''normalizeMeanStd is deprecated. Please call as normalize.meanStd''' 44 return meanStd(matrix)
45
46 -def clipRange(matrix,min_val,max_val):
47 ''' zero mean, one standard deviation ''' 48 is_image = False 49 if isinstance(matrix,pv.Image): 50 matrix = matrix.asMatrix2D() 51 is_image = True 52 # Otherwize, assume it is a numpy matrix 53 mask = matrix > max_val 54 matrix = max_val*mask + matrix*(~mask) 55 56 mask = matrix < min_val 57 matrix = min_val*mask + matrix*(~mask) 58 59 if is_image: 60 return pv.Image(matrix) 61 return matrix
62
63 -def meanStd(matrix):
64 ''' zero mean, one standard deviation ''' 65 is_image = False 66 if isinstance(matrix,pv.Image): 67 matrix = matrix.asMatrix2D() 68 is_image = True 69 # Otherwize, assume it is a numpy matrix 70 matrix = matrix - matrix.mean() 71 matrix = (1.0/(matrix.std()+0.000001)) * matrix 72 if is_image: 73 return pv.Image(matrix) 74 return matrix
75
76 -def meanUnit(matrix):
77 ''' zero mean, unit length ''' 78 is_image = False 79 if isinstance(matrix,pv.Image): 80 matrix = matrix.asMatrix2D() 81 is_image = True 82 matrix = matrix - matrix.mean() 83 length = np.sqrt( (matrix*matrix).sum() ) 84 if length > 0.0: 85 matrix = (1.0/length) * matrix 86 87 if is_image: 88 return pv.Image(matrix) 89 return matrix
90
91 -def unit(matrix):
92 ''' unit length ''' 93 is_image = False 94 if isinstance(matrix,pv.Image): 95 matrix = matrix.asMatrix2D() 96 is_image = True 97 length = np.sqrt( (matrix*matrix).sum() ) 98 if length < 0.00001: #Prevent divide by zero 99 length = 0.00001 100 matrix = (1.0/length) * matrix 101 if is_image: 102 return pv.Image(matrix) 103 return matrix
104
105 -def selfQuotientImage(matrix,sigma=5.0):
106 ''' 107 Compute a self quotient image. 108 109 Based on work by Wang et.al. "Self Quotient Image for Face Recognition" ICIP 2004 110 ''' 111 is_image = False 112 if isinstance(matrix,pv.Image): 113 matrix = matrix.asMatrix2D() 114 is_image = True 115 116 assert matrix.min() >= 0 117 matrix = matrix + 0.01*matrix.max() 118 denom = ndi.gaussian_filter(matrix,sigma) 119 120 # make sure there are no divide by zeros 121 matrix = matrix/(denom+0.000001) 122 123 if is_image: 124 return pv.Image(matrix) 125 return matrix
126 127
128 -def gaussianFilter(im,sigma):
129 ''' 130 Smooth an image using a Gaussian filter. 131 ''' 132 cvim = cv.CreateImage(im.size,cv.IPL_DEPTH_8U,im.channels) 133 134 cv.Smooth(im.asOpenCV(),cvim,cv.CV_GAUSSIAN,0,0,sigma) 135 return pv.Image(cvim)
136
137 -def highPassFilter(matrix,sigma):
138 ''' 139 This function computes a high and low pass filter. This can be used 140 to reduce the effect of lighting. 141 142 A low pass image is first computed by convolving the image with a 143 Gausian filter of radius sigma. Second, a high pass image is computed 144 by subtracting the low pass image from the original image. This means that 145 the original image can be reconstructed by adding a low pass image and a high 146 pass image. 147 148 @returns: high_pass_image 149 ''' 150 is_image = False 151 if isinstance(matrix,pv.Image): 152 matrix = matrix.asMatrix2D() 153 is_image = True 154 155 matrix = matrix - ndi.gaussian_filter(matrix,sigma) 156 157 if is_image: 158 return pv.Image(matrix) 159 return matrix
160
161 -def lowPassFilter(matrix,sigma):
162 ''' 163 This function computes a low pass filter. It basically smoothes the image 164 by convolving with a Gaussian. This is often used to reduce the effect of 165 noise in images or to reduce the effect of small registration errors. 166 167 @returns: an pv.Image set from a numpy matrix if input was an image or a numpy 168 matrix otherwize. 169 ''' 170 is_image = False 171 if isinstance(matrix,pv.Image): 172 matrix = matrix.asMatrix2D() 173 is_image = True 174 175 matrix = ndi.gaussian_filter(matrix,sigma) 176 177 if is_image: 178 return pv.Image(matrix) 179 return matrix
180 181
182 -def bandPassFilter(matrix,sigma_low, sigma_high):
183 ''' 184 This function computes a high and low pass filter. This can be used 185 to reduce the effect of lighting. 186 187 A low pass image is first computed by convolving the image with a 188 Gausian filter of radius sigma. Second, a high pass image is computed 189 by subtracting the low pass image from the original image. This means that 190 the original image can be reconstructed by adding a low pass image and a high 191 pass image. 192 193 @returns: high_pass_image 194 ''' 195 assert sigma_low > sigma_high 196 is_image = False 197 if isinstance(matrix,pv.Image): 198 matrix = matrix.asMatrix2D() 199 is_image = True 200 201 matrix = ndi.gaussian_filter(matrix,sigma_high) - ndi.gaussian_filter(matrix,sigma_low) 202 203 if is_image: 204 return pv.Image(matrix) 205 return matrix
206