Package pycv :: Package cs :: Package cv :: Module integralimage
[hide private]
[frames] | no frames]

Source Code for Module pycv.cs.cv.integralimage

  1  # PyCV - A Computer Vision Package for Python Incorporating Fast Training of Face Detection 
  2   
  3  # Copyright 2007 Nanyang Technological University, Singapore. 
  4  # Authors: Minh-Tri Pham, Viet-Dung D. Hoang, and Tat-Jen Cham. 
  5   
  6  # This file is part of PyCV. 
  7   
  8  # PyCV is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public  
 10  # License as published by the Free Software Foundation, either version  
 11  # 3 of the License, or (at your option) any later version. 
 12   
 13  # PyCV is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17   
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 20   
 21  # --------------------------------------------------------------------- 
 22  #!/usr/bin/env python 
 23   
 24   
 25  __all__ = ['make_vi','integral','integral_uint32','integral_int32', 
 26      'stats_integral','compute_Stats2_integral'] 
 27   
 28  from numpy import array, add, zeros, dot, arange, argmin, ones, concatenate 
 29  from pycv.cs.linalg import daxpy, dger 
 30  from pycv.cs.stats import mean, covariance, Stats2 
 31  from pycv.ext import integralimage_integral, integralimage_integral_uint32, \ 
 32      integralimage_integral_int32 
 33   
34 -def integral(img,iimg):
35 """Convert an image of type uint8 into an image integral of type double. 36 37 Input: 38 img: a 2D numpy.array of type uint8 39 iimg: a 2D numpy.array of type double of the same size to hold the data 40 Output: 41 data filled into img2, where iimg[x,y] = int( img[x,y], i = 0..x, j = 0..y ) 42 """ 43 h, w = img.shape 44 integralimage_integral(img, iimg, w, h, img.strides[0], iimg.strides[0] / 8)
45
46 -def integral_uint32(img,iimg):
47 """Convert an image of type uint8 into an image integral of type uint32. 48 49 Input: 50 img: a 2D numpy.array of type uint8 51 iimg: a 2D numpy.array of type uint32 of the same size to hold the data 52 Output: 53 data filled into img2, where iimg[x,y] = int( img[x,y], i = 0..x, j = 0..y ) 54 """ 55 h, w = img.shape 56 integralimage_integral_uint32(img, iimg, w, h, img.strides[0], iimg.strides[0] / 4)
57
58 -def integral_int32(img,iimg):
59 """Convert an image of type uint8 into an image integral of type int32. 60 61 Input: 62 img: a 2D numpy.array of type uint8 63 iimg: a 2D numpy.array of type int32 of the same size to hold the data 64 Output: 65 data filled into img2, where iimg[x,y] = int( img[x,y], i = 0..x, j = 0..y ) 66 """ 67 h, w = img.shape 68 integralimage_integral_int32(img, iimg, w, h, img.strides[0], iimg.strides[0] / 4)
69 70 71
72 -def make_vi(input_data):
73 """Given a list of image patches, integrate then vectorize them all. 74 75 Input: 76 input_data: a numpy.array of shape (N,d,d) type uint8 77 Output: 78 output_data: the vectorized-integrated version, a numpy.array of shape (N,d*d) type float64 79 """ 80 theshape = input_data.shape 81 N = theshape[0] 82 d = theshape[1] 83 output_data = zeros([N,d*d]) 84 map(integral,input_data,output_data.reshape(N,d,d)) 85 return output_data
86 87
88 -def stats_integral(input_data,weights = None):
89 """Similar to stats.statslist(), but integrate each image of type uint8 first, then vectorize, then do the stats. 90 91 Input: 92 input_data: a array 2D images of type uint8 (numpy.array) 93 Output: 94 w, mean, cov: total weight, mean vector and covariance matrix 95 """ 96 97 n, h, w = input_data.shape 98 sz = w*h 99 tmp = zeros([h,w]) 100 tmpr = tmp.ravel() 101 sum1r = zeros(sz) 102 sum2r = zeros([sz,sz]) 103 if weights is None: 104 tw = n 105 for x in input_data: 106 integral(x,tmp) 107 sum1r = daxpy(tmpr,sum1r) 108 sum2r = dger(1,tmpr,tmpr,a=sum2r,overwrite_a=1) 109 else: 110 tw = weights.sum() 111 for i in xrange(n): 112 integral(input_data[i],tmp) 113 sum1r = daxpy(tmpr,sum1r,a=weights[i]) 114 sum2r = dger(weights[i],tmpr,tmpr,a=sum2r,overwrite_a=1) 115 themean = mean(tw,sum1r) 116 thecov = covariance(tw,sum2r,themean) 117 return (tw,themean,thecov)
118 119
120 -def compute_Stats2_integral(classification_dataset, vi = True):
121 """Given a J-class ClassificationDataset of image patches. 122 The patches are either in 'uint8' raw form (vi = False), 123 or in 'double' vectorintegrated form (vi = True) 124 125 Input: 126 classification_dataset: a ClassificationDataset 127 vi: input data are vectorintegrated? 128 Output: 129 stats2: a Stats2 holding the obtained statistics 130 """ 131 132 if vi: 133 return classification_dataset.compute_Stats2() 134 else: 135 cd = classification_dataset 136 J = cd.J 137 if cd.weights is None: 138 mlist = map( stats_integral, cd.input_data ) 139 else: 140 mlist = map( stats_integral, cd.input_data, cd.weights ) 141 ldim = mlist[0][1].size 142 A = zeros([J,1+ldim+ldim*ldim]) 143 for i in xrange(J): 144 A[i,0] = mlist[i][0] 145 A[i,1:ldim+1] = mlist[i][1].ravel() 146 A[i,ldim+1:] = mlist[i][2].ravel() 147 return Stats2(J, (ldim,), A)
148