1Image2D(3) User Contributed Perl Documentation Image2D(3)
2
3
4
6 PDL::Image2D - Miscellaneous 2D image processing functions
7
9 Miscellaneous 2D image processing functions - for want of anywhere else
10 to put them.
11
13 use PDL::Image2D;
14
16 conv2d
17
18 Signature: (a(m,n); kern(p,q); [o]b(m,n); int opt)
19
20 2D convolution of an array with a kernel (smoothing)
21
22 For large kernels, using a FFT routine, such as fftconvolve() in
23 "PDL::FFT", will be quicker.
24
25 $new = conv2d $old, $kernel, {OPTIONS}
26
27 $smoothed = conv2d $image, ones(3,3), {Boundary => Reflect}
28
29 Boundary - controls what values are assumed for the image when kernel
30 crosses its edge:
31 => Default - periodic boundary conditions
32 (i.e. wrap around axis)
33 => Reflect - reflect at boundary
34 => Truncate - truncate at boundary
35
36 med2d
37
38 Signature: (a(m,n); kern(p,q); [o]b(m,n); int opt)
39
40 2D median-convolution of an array with a kernel (smoothing)
41
42 Note: only points in the kernel >0 are included in the median, other
43 points are weighted by the kernel value (medianing lots of zeroes is
44 rather pointless)
45
46 $new = med2d $old, $kernel, {OPTIONS}
47
48 $smoothed = med2d $image, ones(3,3), {Boundary => Reflect}
49
50 Boundary - controls what values are assumed for the image when kernel
51 crosses its edge:
52 => Default - periodic boundary conditions (i.e. wrap around axis)
53 => Reflect - reflect at boundary
54 => Truncate - truncate at boundary
55
56 med2df
57
58 Signature: (a(m,n); [o]b(m,n); int __p_size; int __q_size; int opt)
59
60 2D median-convolution of an array in a pxq window (smoothing)
61
62 Note: this routine does the median over all points in a rectangular
63 window and is not quite as flexible as "med2d" in this regard
64 but slightly faster instead
65
66 $new = med2df $old, $xwidth, $ywidth, {OPTIONS}
67
68 $smoothed = med2df $image, 3, 3, {Boundary => Reflect}
69
70 Boundary - controls what values are assumed for the image when kernel
71 crosses its edge:
72 => Default - periodic boundary conditions (i.e. wrap around axis)
73 => Reflect - reflect at boundary
74 => Truncate - truncate at boundary
75
76 box2d
77
78 Signature: (a(n,m); [o] b(n,m); int wx; int wy; int edgezero)
79
80 fast 2D boxcar average
81
82 $smoothim = $im->box2d($wx,$wy,$edgezero=1);
83
84 The edgezero argument controls if edge is set to zero (edgezero=1) or
85 just keeps the original (unfiltered) values.
86
87 "box2d" should be updated to support similar edge options as "conv2d"
88 and "med2d" etc.
89
90 Boxcar averaging is a pretty crude way of filtering. For serious stuff
91 better filters are around (e.g., use conv2d with the appropriate ker‐
92 nel). On the other hand it is fast and computational cost grows only
93 approximately linearly with window size.
94
95 patch2d
96
97 Signature: (a(m,n); int bad(m,n); [o]b(m,n))
98
99 patch bad pixels out of 2D images using a mask
100
101 $patched = patch2d $data, $bad;
102
103 $bad is a 2D mask array where 1=bad pixel 0=good pixel. Pixels are
104 replaced by the average of their non-bad neighbours; if all neighbours
105 are bad, the original data value is copied across.
106
107 patchbad2d
108
109 Signature: (a(m,n); [o]b(m,n))
110
111 patch bad pixels out of 2D images containing bad values
112
113 $patched = patchbad2d $data;
114
115 Pixels are replaced by the average of their non-bad neighbours; if all
116 neighbours are bad, the output is set bad. If the input piddle con‐
117 tains no bad values, then a straight copy is performed (see patch2d).
118
119 max2d_ind
120
121 Signature: (a(m,n); [o]val(); int [o]x(); int[o]y())
122
123 Return value/position of maximum value in 2D image
124
125 Contributed by Tim Jeness
126
127 centroid2d
128
129 Signature: (im(m,n); x(); y(); box(); [o]xcen(); [o]ycen())
130
131 Refine a list of object positions in 2D image by centroiding in a box
132
133 $box is the full-width of the box, i.e. the window is "+/- $box/2".
134
135 cc8compt
136
137 Signature: (a(m,n); [o]b(m,n))
138
139 Connected 8-component labeling of a binary image.
140
141 Connected 8-component labeling of 0,1 image - i.e. find seperate seg‐
142 mented objects and fill object pixels with object number
143
144 $segmented = cc8compt( $image > $threshold );
145
146 polyfill
147
148 Signature: (int [o,nc] im(m,n); float ps(two=2,np); int col())
149
150 fill the area inside the given polygon with a given colour
151
152 This function works inplace, i.e. modifies "im".
153
154 polyfillv
155
156 return the (dataflown) area of an image within a polygon
157
158 # increment intensity in area bounded by $poly
159 $im->polyfillv($pol)++; # legal in perl >= 5.6
160 # compute average intensity within area bounded by $poly
161 $av = $im->polyfillv($poly)->avg;
162
163 rot2d
164
165 Signature: (im(m,n); float angle(); bg(); int aa(); [o] om(p,q))
166
167 rotate an image by given "angle"
168
169 # rotate by 10.5 degrees with antialiasing, set missing values to 7
170 $rot = $im->rot2d(10.5,7,1);
171
172 This function rotates an image through an "angle" between -90 and + 90
173 degrees. Uses/doesn't use antialiasing depending on the "aa" flag.
174 Pixels outside the rotated image are set to "bg".
175
176 Code modified from pnmrotate (Copyright Jef Poskanzer) with an algo‐
177 rithm based on "A Fast Algorithm for General Raster Rotation" by
178 Alan Paeth, Graphics Interface '86, pp. 77-81.
179
180 Use the "rotnewsz" function to find out about the dimension of the
181 newly created image
182
183 ($newcols,$newrows) = rotnewsz $oldn, $oldm, $angle;
184
185 PDL::Transform offers a more general interface to distortions, includ‐
186 ing rotation, with various types of sampling; but rot2d is faster.
187
188 bilin2d
189
190 Signature: (I(n,m); O(q,p))
191
192 Bilinearly maps the first piddle in the second. The interpolated values
193 are actually added to the second piddle which is supposed to be larger
194 than the first one.
195
196 rescale2d
197
198 Signature: (I(m,n); O(p,q))
199
200 The first piddle is rescaled to the dimensions of the second (expanding
201 or meaning values as needed) and then added to it in place. Nothing
202 useful is returned.
203
204 If you want photometric accuracy or automatic FITS header metadata
205 tracking, consider using PDL::Transform::map instead: it does these
206 things, at some speed penalty compared to rescale2d.
207
208 fitwarp2d
209
210 Find the best-fit 2D polynomial to describe a coordinate transforma‐
211 tion.
212
213 ( $px, $py ) = fitwarp2d( $x, $y, $u, $v, $nf. { options } )
214
215 Given a set of points in the output plane ("$u,$v"), find the best-fit
216 (using singular-value decomposition) 2D polynomial to describe the map‐
217 ping back to the image plane ("$x,$y"). The order of the fit is con‐
218 trolled by the $nf parameter (the maximum power of the polynomial is
219 "$nf - 1"), and you can restrict the terms to fit using the "FIT"
220 option.
221
222 $px and $py are "np" by "np" element piddles which describe a polyno‐
223 mial mapping (of order "np-1") from the output "(u,v)" image to the
224 input "(x,y)" image:
225
226 x = sum(j=0,np-1) sum(i=0,np-1) px(i,j) * u^i * v^j
227 y = sum(j=0,np-1) sum(i=0,np-1) py(i,j) * u^i * v^j
228
229 The transformation is returned for the reverse direction (ie output to
230 input image) since that is what is required by the warp2d() routine.
231 The applywarp2d() routine can be used to convert a set of "$u,$v"
232 points given $px and $py.
233
234 Options:
235
236 FIT - which terms to fit? default ones(byte,$nf,$nf)
237 THRESH - in svd, remove terms smaller than THRESH * max value
238 default is 1.0e-5
239
240 FIT "FIT" allows you to restrict which terms of the polynomial to fit:
241 only those terms for which the FIT piddle evaluates to true will be
242 evaluated. If a 2D piddle is sent in, then it is used for the x
243 and y polynomials; otherwise "$fit->slice(":,:,(0)")" will be used
244 for $px and "$fit->slice(":,:,(1)")" will be used for $py.
245
246 THRESH
247 Remove all singular values whose valus is less than "THRESH" times
248 the largest singular value.
249
250 The number of points must be at least equal to the number of terms to
251 fit ("$nf*$nf" points for the default value of "FIT").
252
253 # points in original image
254 $x = pdl( 0, 0, 100, 100 );
255 $y = pdl( 0, 100, 100, 0 );
256 # get warped to these positions
257 $u = pdl( 10, 10, 90, 90 );
258 $v = pdl( 10, 90, 90, 10 );
259 #
260 # shift of origin + scale x/y axis only
261 $fit = byte( [ [1,1], [0,0] ], [ [1,0], [1,0] ] );
262 ( $px, $py ) = fitwarp2d( $x, $y, $u, $v, 2, { FIT => $fit } );
263 print "px = ${px}py = $py";
264 px =
265 [
266 [-12.5 1.25]
267 [ 0 0]
268 ]
269 py =
270 [
271 [-12.5 0]
272 [ 1.25 0]
273 ]
274 #
275 # Compared to allowing all 4 terms
276 ( $px, $py ) = fitwarp2d( $x, $y, $u, $v, 2 );
277 print "px = ${px}py = $py";
278 px =
279 [
280 [ -12.5 1.25]
281 [ 1.110223e-16 -1.1275703e-17]
282 ]
283 py =
284 [
285 [ -12.5 1.6653345e-16]
286 [ 1.25 -5.8546917e-18]
287 ]
288
289 applywarp2d
290
291 Transform a set of points using a 2-D polynomial mapping
292
293 ( $x, $y ) = applywarp2d( $px, $py, $u, $v )
294
295 Convert a set of points (stored in 1D piddles "$u,$v") to "$x,$y" using
296 the 2-D polynomial with coefficients stored in $px and $py. See fit‐
297 warp2d() for more information on the format of $px and $py.
298
299 warp2d
300
301 Signature: (img(m,n); double px(np,np); double py(np,np); [o] warp(m,n); { options })
302
303 Warp a 2D image given a polynomial describing the reverse mapping.
304
305 $out = warp2d( $img, $px, $py, { options } );
306
307 Apply the polynomial transformation encoded in the $px and $py piddles
308 to warp the input image $img into the output image $out.
309
310 The format for the polynomial transformation is described in the docu‐
311 mentation for the fitwarp2d() routine.
312
313 At each point "x,y", the closest 16 pixel values are combined with an
314 interpolation kernel to calculate the value at "u,v". The interpola‐
315 tion is therefore done in the image, rather than Fourier, domain. By
316 default, a "tanh" kernel is used, but this can be changed using the
317 "KERNEL" option discussed below (the choice of kernel depends on the
318 frequency content of the input image).
319
320 The routine is based on the "warping" command from the Eclipse data-
321 reduction package - see http://www.eso.org/eclipse/ - and for further
322 details on image resampling see Wolberg, G., "Digital Image Warping",
323 1990, IEEE Computer Society Press ISBN 0-8186-8944-7).
324
325 Currently the output image is the same size as the input one, which
326 means data will be lost if the transformation reduces the pixel scale.
327 This will (hopefully) be changed soon.
328
329 $img = rvals(byte,501,501);
330 imag $img, { JUSTIFY => 1 };
331 #
332 # use a not-particularly-obvious transformation:
333 # x = -10 + 0.5 * $u - 0.1 * $v
334 # y = -20 + $v - 0.002 * $u * $v
335 #
336 $px = pdl( [ -10, 0.5 ], [ -0.1, 0 ] );
337 $py = pdl( [ -20, 0 ], [ 1, 0.002 ] );
338 $wrp = warp2d( $img, $px, $py );
339 #
340 # see the warped image
341 imag $warp, { JUSTIFY => 1 };
342
343 The options are:
344
345 KERNEL - default value is tanh
346 NOVAL - default value is 0
347
348 "KERNEL" is used to specify which interpolation kernel to use (to see
349 what these kernels look like, use the warp2d_kernel() routine). The
350 options are:
351
352 tanh
353 Hyperbolic tangent: the approximation of an ideal box filter by the
354 product of symmetric tanh functions.
355
356 sinc
357 For a correctly sampled signal, the ideal filter in the fourier
358 domain is a rectangle, which produces a "sinc" interpolation kernel
359 in the spatial domain:
360
361 sinc(x) = sin(pi * x) / (pi * x)
362
363 However, it is not ideal for the "4x4" pixel region used here.
364
365 sinc2
366 This is the square of the sinc function.
367
368 lanczos
369 Although defined differently to the "tanh" kernel, the result is
370 very similar in the spatial domain. The Lanczos function is
371 defined as
372
373 L(x) = sinc(x) * sinc(x/2) if abs(x) < 2
374 = 0 otherwise
375
376 hann
377 This kernel is derived from the following function:
378
379 H(x) = a + (1-a) * cos(2*pi*x/(N-1)) if abs(x) < 0.5*(N-1)
380 = 0 otherwise
381
382 with "a = 0.5" and N currently equal to 2001.
383
384 hamming
385 This kernel uses the same H(x) as the Hann filter, but with "a =
386 0.54".
387
388 "NOVAL" gives the value used to indicate that a pixel in the output
389 image does not map onto one in the input image.
390
391 warp2d_kernel
392
393 Return the specified kernel, as used by warp2d
394
395 ( $x, $k ) = warp2d_kernel( $name )
396
397 The valid values for $name are the same as the "KERNEL" option of
398 warp2d().
399
400 line warp2d_kernel( "hamming" );
401
403 Copyright (C) Karl Glazebrook 1997 with additions by Robin Williams
404 (rjrw@ast.leeds.ac.uk), Tim Jeness (timj@jach.hawaii.edu), and Doug
405 Burke (burke@ifa.hawaii.edu).
406
407 All rights reserved. There is no warranty. You are allowed to redis‐
408 tribute this software / documentation under certain conditions. For
409 details, see the file COPYING in the PDL distribution. If this file is
410 separated from the PDL distribution, the copyright notice should be
411 included in the file.
412
413
414
415perl v5.8.8 2006-12-02 Image2D(3)