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
26
27
28
29
30
31
32
33
34
35
36 import unittest
37 import os.path
38
39 from numpy import array,ones,zeros,nonzero
40 from scipy.ndimage import convolve
41 from scipy.ndimage import gaussian_filter,maximum_filter
42
43
44
45
46 import pyvision as pv
47 from pyvision.point.DetectorROI import DetectorROI
48
49
50
51
52 conv2 = convolve
53
55 - def __init__(self,mask = [[-1,0,1]], radius=9, sigma=0.7, k=0.04, **kwargs):
56 '''
57 Corner Detector
58
59 mask - first dirivitive filter
60 radius - radius of the max filter
61 sigma - sigma of the smoothing gaussian.
62 k - not sure what this parameter means.
63
64 Passed to superclass:
65 n - is the approximate number of points requested.
66 bin_size - the width and height of each bin in pixels.
67 corner_selector ('all', 'bins', or 'best') - stratagy for point selection.
68
69 When corner_selector is set to bins, the image is subdivided in to bins of
70 size <bin_size>X<bin_size> pixels and an equal number of points will be taken
71 from each of those bins. This insures that points are found in all parts of the
72 image not just where the corners are strongest.
73
74 This code is based on a function originally written for matlab.
75
76 Original matlab code by:
77 Jingyu Yan and Marc Pollefeys
78 Department of Computer Science
79 University of North Carolina at Chapel Hill
80
81 Converted to Python by:
82 David Bolme
83 Department of Computer Science
84 Colorado State Univerisity
85 '''
86 DetectorROI.__init__(self,**kwargs)
87
88 self.mask = mask
89 self.radius = radius
90 self.sigma = sigma
91 self.k = k
92
94
95 A = None
96 if isinstance(image,pv.Image):
97 A = image.asMatrix2D()
98 elif isinstance(image,array) and len(image.shape)==2:
99 A = image
100 else:
101 raise TypeError("ERROR Unknown Type (%s) - Only arrays and pyvision images supported."%type(image))
102
103 mask = array(self.mask)
104 assert len(mask.shape) == 2
105
106
107 del_A_1 = conv2(A,mask)
108 del_A_2 = conv2(A,mask.transpose())
109
110
111 del_A_1_1 = del_A_1 * del_A_1
112 matrix_1_1 = gaussian_filter(del_A_1_1, self.sigma)
113 del del_A_1_1
114
115 del_A_2_2 = del_A_2 * del_A_2
116 matrix_2_2 = gaussian_filter(del_A_2_2, self.sigma)
117 del del_A_2_2
118
119 del_A_1_2 = del_A_1 * del_A_2
120 matrix_1_2 = gaussian_filter(del_A_1_2, self.sigma)
121 del del_A_1_2
122
123 del del_A_1,del_A_2
124
125 dM = matrix_1_1*matrix_2_2 - matrix_1_2*matrix_1_2
126 tM = matrix_1_1+matrix_2_2
127
128 del matrix_1_1 , matrix_1_2, matrix_2_2
129
130 R = dM-self.k*pow(tM,2)
131
132 footprint = ones((self.radius,self.radius))
133 mx = maximum_filter(R, footprint = footprint)
134 local_maxima = (R == mx) * (R != zeros(R.shape))
135 del mx
136
137 points = nonzero(local_maxima)
138 del local_maxima
139
140 points = array([points[0],points[1]]).transpose()
141 L = []
142 for each in points:
143 L.append((R[each[0],each[1]],each[0],each[1],None))
144
145 del R
146
147 return L
148
149
150
153 self.SHOW_IMAGES = False
154
155
157 detector = DetectorCorner()
158 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_1.jpg')
159 im = pv.Image(filename,bw_annotate=True)
160
161 points = detector.detect(im)
162 for _,pt,_ in points:
163 im.annotatePoint(pt)
164
165 if self.SHOW_IMAGES: im.show()
166 self.assertEquals(len(points),390)
167
169 detector = DetectorCorner()
170 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_19.jpg')
171 im = pv.Image(filename,bw_annotate=True)
172
173 points = detector.detect(im)
174 for _,pt,_ in points:
175 im.annotatePoint(pt)
176
177 if self.SHOW_IMAGES: im.show()
178 self.assertEquals(len(points),390)
179
181 detector = DetectorCorner()
182 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_22.jpg')
183 im = pv.Image(filename,bw_annotate=True)
184
185 points = detector.detect(im)
186 for _,pt,_ in points:
187 im.annotatePoint(pt)
188
189 if self.SHOW_IMAGES: im.show()
190 self.assertEquals(len(points),390)
191
193 detector = DetectorCorner()
194 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg')
195 im = pv.Image(filename,bw_annotate=True)
196
197 points = detector.detect(im)
198 for _,pt,_ in points:
199 im.annotatePoint(pt)
200
201 if self.SHOW_IMAGES: im.show()
202 self.assertEquals(len(points),351)
203
205 detector = DetectorCorner(selector='best')
206 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg')
207 im = pv.Image(filename,bw_annotate=True)
208
209 points = detector.detect(im)
210 for _,pt,_ in points:
211 im.annotatePoint(pt)
212
213 if self.SHOW_IMAGES: im.show()
214 self.assertEquals(len(points),250)
215
217 detector = DetectorCorner(selector='all')
218 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg')
219 im = pv.Image(filename,bw_annotate=True)
220
221 points = detector.detect(im)
222 for _,pt,_ in points:
223 im.annotatePoint(pt)
224
225 if self.SHOW_IMAGES: im.show()
226 self.assertEquals(len(points),2143)
227