@@ -34,7 +34,7 @@ class POI(Structure):
34
34
'variance_map' : (None ,
35
35
[c_int , c_int , array_2d_uchar , array_2d_double ,
36
36
c_int , c_int ]),
37
-
37
+
38
38
'line_intersect' : (None ,
39
39
[c_double , c_double , c_double , c_double ,
40
40
c_double , c_double , c_double , c_double ,
@@ -52,11 +52,13 @@ class POI(Structure):
52
52
[c_int , c_int , array_2d_uchar ,
53
53
c_int , c_int , array_2d_double , array_2d_double ,
54
54
c_char , c_uint8 , array_2d_uchar ]),
55
+ 'interp_transf_polygon' : (None ,
56
+ [c_int , c_int , array_2d_uchar ,
57
+ c_int , c_int , array_2d_double ,
58
+ array_2d_double ]),
55
59
}
56
60
57
61
def register_api (lib ,api ):
58
- import inspect
59
- parent_frame = inspect .currentframe ().f_back
60
62
for f , (restype , argtypes ) in api .iteritems ():
61
63
func = getattr (lib , f )
62
64
func .restype = restype
@@ -66,13 +68,13 @@ def register_api(lib,api):
66
68
67
69
# Python wrappers for libsupreme functions
68
70
69
- def _atype (arrays , types ):
71
+ def atype (arrays , types ):
70
72
"""Return contiguous arrays of given types.
71
73
72
74
arrays - list of input arrays
73
75
types - list of corresponding types
74
-
75
- """
76
+
77
+ """
76
78
out = ()
77
79
try :
78
80
za = zip (arrays ,types )
@@ -81,28 +83,25 @@ def _atype(arrays, types):
81
83
82
84
out = ()
83
85
for A ,T in za :
84
- try :
85
- out += N .ascontiguousarray (A ,T ),
86
- except :
87
- out += N .ascontiguousarray (A ).astype (T ),
86
+ out += N .ascontiguousarray (A ,T ),
88
87
89
88
return out
90
-
89
+
91
90
def npnpoly (x_vertices , y_vertices , x_points , y_points ):
92
91
"""Calculate whether points are in a given polygon.
93
92
94
93
Returns a boolean array of length len(x_points).
95
-
94
+
96
95
"""
97
- xi ,yi ,x ,y = _atype ([x_vertices ,y_vertices ,
96
+ xi ,yi ,x ,y = atype ([x_vertices ,y_vertices ,
98
97
x_points ,y_points ],[N .double ]* 4 )
99
98
100
99
if xi [0 ] != xi [- 1 ] or yi [0 ] != yi [- 1 ]:
101
100
xi = N .append (xi ,x [0 ])
102
101
yi = N .append (yi ,y [0 ])
103
102
104
103
out = N .empty (len (x ),dtype = N .uint8 )
105
-
104
+
106
105
_lib .npnpoly (len (xi ), xi , yi ,
107
106
len (x ), x , y ,
108
107
out )
@@ -118,7 +117,7 @@ def variance_map(image, shape=(3,3)):
118
117
the variance of all the pixels in the window is stored.
119
118
120
119
"""
121
- image , = _atype (image ,N .uint8 )
120
+ image , = atype (image ,N .uint8 )
122
121
assert image .ndim == 2 , "Image must be 2-dimensional"
123
122
window_size_rows , window_size_columns = shape
124
123
rows , columns = image .shape
@@ -151,16 +150,16 @@ def poly_clip(x, y, xleft, xright, ytop, ybottom):
151
150
x and y are 1D arrays describing the coordinates of the vertices.
152
151
xleft, xright, ytop and ybottom specify the borders of the
153
152
bounding box. Note that a cartesian axis system is used such that
154
- the following must hold:
153
+ the following must hold true :
155
154
156
155
x_left < x_right
157
156
y_bottom < y_top
158
157
159
158
The x and y coordinates of the vertices of the resulting polygon
160
159
are returned.
161
-
160
+
162
161
"""
163
- x ,y = _atype ([x ,y ],[N .double ,N .double ])
162
+ x ,y = atype ([x ,y ],[N .double ,N .double ])
164
163
165
164
assert len (x ) == len (y ), "Equal number of x and y coordinates required"
166
165
assert ytop > ybottom
@@ -170,11 +169,11 @@ def poly_clip(x, y, xleft, xright, ytop, ybottom):
170
169
if x [0 ] != x [- 1 ] or y [0 ] != y [- 1 ]:
171
170
x = N .append (x ,x [0 ])
172
171
y = N .append (y ,y [0 ])
173
-
172
+
174
173
xleft ,xright ,ytop ,ybottom = map (N .double ,[xleft ,xright ,ytop ,ybottom ])
175
174
176
- workx = N .empty (2 * len (x - 1 ) ,dtype = N .double )
177
- worky = N .empty ( 2 * len ( x - 1 ), dtype = N . double )
175
+ workx = N .empty (2 * len (x ) - 1 ,dtype = N .double )
176
+ worky = N .empty_like ( workx )
178
177
M = _lib .poly_clip (len (x ),x ,y ,xleft ,xright ,ytop ,ybottom ,workx ,worky )
179
178
workx [M ] = workx [0 ]
180
179
worky [M ] = worky [0 ]
@@ -193,10 +192,10 @@ def correlate(A,B,
193
192
mode_column -- values outside boundaries, 'zero' or 'mirror'
194
193
195
194
Returns a rows-by-columns array of correlation values.
196
-
195
+
197
196
"""
198
197
199
- A ,B = _atype ([A ,B ],[N .double ,N .double ])
198
+ A ,B = atype ([A ,B ],[N .double ,N .double ])
200
199
assert A .ndim == 2 and B .ndim == 2 , "Input arrays must be two dimensional"
201
200
202
201
A_r ,A_c = A .shape
@@ -210,7 +209,7 @@ def correlate(A,B,
210
209
211
210
modes = {'zero' : 0 ,
212
211
'mirror' : 1 }
213
-
212
+
214
213
output = N .empty ((rows ,columns ),dtype = N .double )
215
214
_lib .correlate (A_r , A_c , A ,
216
215
B_r , B_c , B ,
@@ -220,7 +219,8 @@ def correlate(A,B,
220
219
221
220
return output
222
221
223
- def interp_bilinear (grey_image ,transform_coords_r ,transform_coords_c ,
222
+ def interp_bilinear (grey_image ,
223
+ transform_coords_r = None ,transform_coords_c = None ,
224
224
mode = 'N' ,cval = 0 ,output = None ):
225
225
"""Calculate values at given coordinates using bi-linear interpolation.
226
226
@@ -249,23 +249,36 @@ def interp_bilinear(grey_image,transform_coords_r,transform_coords_c,
249
249
An image of shape transform_coords_r.shape and type N.uint8.
250
250
251
251
"""
252
- grey_image , = _atype (grey_image ,N .uint8 )
253
- transform_coords_r ,transform_coords_c = _atype ([transform_coords_r ,transform_coords_c ],
252
+ grey_image , = atype (grey_image ,N .uint8 )
253
+ transform_coords_r ,transform_coords_c = atype ([transform_coords_r ,transform_coords_c ],
254
254
[N .double ,N .double ])
255
255
assert grey_image .ndim == 2 , "Input image must be 2-dimensional"
256
256
assert transform_coords_r .ndim == 2 and transform_coords_c .ndim == 2 , \
257
257
"Transform coordinates must be 2-dimensional"
258
258
if output is None :
259
- output = N .empty (transform_coords_r .shape ,dtype = N .uint8 )
259
+ output = N .empty (transform_coords_r .shape ,dtype = N .uint8 )
260
260
else :
261
- output , = _atype (output ,N .uint8 )
262
- output .shape = transform_coords_r .shape
261
+ output , = atype (output ,N .uint8 )
262
+ output .shape = transform_coords_r .shape
263
263
264
264
rows ,columns = grey_image .shape
265
265
tf_rows ,tf_columns = transform_coords_r .shape
266
266
_lib .interp_bilinear (rows ,columns ,grey_image ,
267
267
tf_rows ,tf_columns ,
268
268
transform_coords_r ,transform_coords_c ,
269
269
mode [0 ],cval ,output )
270
-
270
+
271
271
return output
272
+
273
+ def interp_transf_polygon (grey_image ,transform ,oshape = None ):
274
+ grey_image ,transform = atype ([grey_image ,transform ],[N .uint8 ,N .double ])
275
+ ishape = grey_image .shape
276
+ if oshape is None :
277
+ oshape = ishape
278
+
279
+ out = N .empty (oshape ,dtype = SC .ftype )
280
+ _lib .interp_transf_polygon (ishape [0 ],ishape [1 ],grey_image ,
281
+ oshape [0 ],oshape [1 ],out ,
282
+ transform )
283
+ return out
284
+
0 commit comments