1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 __all__ = ['NonObjectGenerator']
26
27
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
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
60
62 if self.img is not None:
63 cvReleaseImage(self.img)
64 self.img = None
65
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
79 if self.cnt <= 0:
80 self._reset()
81 else:
82 self.cnt -= 1
83
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
95 w = self.img[0].width
96 h = self.img[0].height
97
98 maxr = min(w,h) / 2
99 r = randrange(min(16, maxr/2), maxr)
100 x = randrange(r, w-r)
101 y = randrange(r, h-r)
102
103
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