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

Source Code for Module pycv.cs.cv.patch.nonobject

  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__ = ['NonObjectGenerator'] 
 26   
 27  # import the necessary things 
 28  from dircache import listdir 
 29  from numpy import array 
 30  from os.path import exists, join, dirname 
 31  from random import randrange 
 32   
 33  from pycv.interfaces.opencv import cvLoadImage, CV_LOAD_IMAGE_GRAYSCALE, \ 
 34      cvReleaseImage, CV_64FC1, cvGetAffineTransform, cvWarpAffine, \ 
 35      CvPoint2D32f, cvCreateMat, cvReleaseMat, CV_INTER_LINEAR, \ 
 36      CV_WARP_FILL_OUTLIERS, CvScalar 
 37  from pycv import tprint 
 38   
 39   
40 -class NonObjectGenerator:
41 """A generator that generates non-object patches.""" 42
43 - def __init__(self,database_path,maxCnt = 100):
44 """Initialize the generator. 45 46 :Parameters: 47 database_path : string 48 the path to the folder containing the non-object images 49 """ 50 self.filenames = listdir(database_path) 51 self.filenames = [join(database_path,x) for x in self.filenames] 52 self.maxCnt = maxCnt 53 self.mat = cvCreateMat(2,3,CV_64FC1) 54 self.img = None 55 self._reset()
56
57 - def __del__(self):
58 self._clear() 59 cvReleaseMat(self.mat)
60
61 - def _clear(self):
62 if self.img is not None: 63 cvReleaseImage(self.img) 64 self.img = None
65
66 - def _reset(self):
67 while True: 68 self._clear() 69 afile = self.filenames[randrange(len(self.filenames))] 70 self.img = cvLoadImage(afile,CV_LOAD_IMAGE_GRAYSCALE) 71 try: 72 self.img.contents 73 break 74 except ValueError: 75 tprint('Warning, file %s cannot be loaded' % afile) 76 self.cnt = self.maxCnt
77
78 - def _inc(self):
79 if self.cnt <= 0: 80 self._reset() 81 else: 82 self.cnt -= 1
83
84 - def generate(self,patch):
85 """Fill in a patch of size N-by-N with a non-object patch. 86 87 :Parameters: 88 patch : POITNER(IplImage) 89 a POINTER(IplImage) that points to a N-by-N uint8 IplImage. The 90 patch is filled after calling generate() 91 """ 92 self._inc() 93 94 # get a random patch 95 w = self.img[0].width 96 h = self.img[0].height 97 98 maxr = min(w,h) / 2 # expect min(w, h) >= 4 99 r = randrange(min(16, maxr/2), maxr) 100 x = randrange(r, w-r) 101 y = randrange(r, h-r) 102 103 # copy the patch over 104 N = float(patch[0].height) 105 C3 = CvPoint2D32f * 3 106 src = C3() 107 dst = C3() 108 109 src[0].x = x 110 src[0].y = y 111 dst[0].x = 0.5*N 112 dst[0].y = 0.5*N 113 114 src[1].x = x+r 115 src[1].y = y 116 dst[1].x = N-1 117 dst[1].y = 0.5*N 118 119 src[2].x = x 120 src[2].y = y+r 121 dst[2].x = 0.5*N 122 dst[2].y = N-1 123 124 cvGetAffineTransform(src,dst,self.mat) 125 cvWarpAffine(self.img,patch,self.mat, 126 CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,CvScalar(0,0,0,0))
127