0
# Transformations
1
2
Geometric and coordinate transformations for image registration, warping, scaling, and analysis. Includes support for various transformation models, image pyramids, and specialized transforms like Hough and Radon transforms.
3
4
## Capabilities
5
6
### Basic Image Transformations
7
8
Fundamental operations for resizing, rotating, and scaling images with various interpolation methods.
9
10
```python { .api }
11
def resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True, preserve_range=False, anti_aliasing=None, anti_aliasing_sigma=None):
12
"""
13
Resize image to specified dimensions.
14
15
Parameters:
16
image : array_like
17
Input image
18
output_shape : tuple
19
Target output shape (rows, cols)
20
order : int, optional
21
Interpolation order (0-5)
22
mode : str, optional
23
Boundary condition mode
24
cval : float, optional
25
Constant value for constant mode
26
clip : bool, optional
27
Whether to clip output to input range
28
preserve_range : bool, optional
29
Keep original data range
30
anti_aliasing : bool, optional
31
Whether to apply anti-aliasing
32
anti_aliasing_sigma : float, optional
33
Standard deviation for anti-aliasing
34
35
Returns:
36
ndarray
37
Resized image
38
"""
39
40
def rescale(image, scale, order=None, mode='reflect', cval=0, clip=True, preserve_range=False, multichannel=None, anti_aliasing=None, anti_aliasing_sigma=None, channel_axis=None):
41
"""
42
Scale image by a given factor.
43
44
Parameters:
45
image : array_like
46
Input image
47
scale : float or sequence
48
Scale factor(s) for each axis
49
order : int, optional
50
Interpolation order (0-5)
51
mode : str, optional
52
Boundary condition mode
53
cval : float, optional
54
Constant value for constant mode
55
clip : bool, optional
56
Whether to clip output to input range
57
preserve_range : bool, optional
58
Keep original data range
59
multichannel : bool, optional
60
Whether last axis is channels (deprecated)
61
anti_aliasing : bool, optional
62
Whether to apply anti-aliasing
63
anti_aliasing_sigma : float, optional
64
Standard deviation for anti-aliasing
65
channel_axis : int, optional
66
Axis for color channels
67
68
Returns:
69
ndarray
70
Rescaled image
71
"""
72
73
def rotate(image, angle, resize=False, center=None, order=None, mode='constant', cval=0, clip=True, preserve_range=False):
74
"""
75
Rotate image by specified angle.
76
77
Parameters:
78
image : array_like
79
Input image
80
angle : float
81
Rotation angle in degrees (counter-clockwise)
82
resize : bool, optional
83
Whether to resize output to fit rotated image
84
center : tuple, optional
85
Center of rotation
86
order : int, optional
87
Interpolation order (0-5)
88
mode : str, optional
89
Boundary condition mode
90
cval : float, optional
91
Constant value for constant mode
92
clip : bool, optional
93
Whether to clip output to input range
94
preserve_range : bool, optional
95
Keep original data range
96
97
Returns:
98
ndarray
99
Rotated image
100
"""
101
102
def swirl(image, center=None, strength=1, radius=120, rotation=0, output_shape=None, order=None, mode='reflect', cval=0, clip=True, preserve_range=False):
103
"""
104
Apply swirl transformation to image.
105
106
Parameters:
107
image : array_like
108
Input image
109
center : tuple, optional
110
Center of swirl
111
strength : float, optional
112
Swirl strength
113
radius : float, optional
114
Swirl radius
115
rotation : float, optional
116
Additional rotation angle
117
output_shape : tuple, optional
118
Shape of output
119
order : int, optional
120
Interpolation order (0-5)
121
mode : str, optional
122
Boundary condition mode
123
cval : float, optional
124
Constant value for constant mode
125
clip : bool, optional
126
Whether to clip output to input range
127
preserve_range : bool, optional
128
Keep original data range
129
130
Returns:
131
ndarray
132
Swirl-transformed image
133
"""
134
```
135
136
### Image Warping
137
138
Advanced warping operations using coordinate transformations and mapping functions.
139
140
```python { .api }
141
def warp(image, inverse_map, map_args=None, output_shape=None, order=None, mode='constant', cval=0.0, clip=True, preserve_range=False):
142
"""
143
Warp image using inverse coordinate transformation.
144
145
Parameters:
146
image : array_like
147
Input image
148
inverse_map : callable or ndarray
149
Inverse coordinate mapping function or coordinate array
150
map_args : dict, optional
151
Additional arguments for mapping function
152
output_shape : tuple, optional
153
Shape of output image
154
order : int, optional
155
Interpolation order (0-5)
156
mode : str, optional
157
Boundary condition mode
158
cval : float, optional
159
Constant value for constant mode
160
clip : bool, optional
161
Whether to clip output to input range
162
preserve_range : bool, optional
163
Keep original data range
164
165
Returns:
166
ndarray
167
Warped image
168
"""
169
170
def warp_coords(coord_map, shape, dtype=np.float64):
171
"""
172
Build coordinate map for image warping.
173
174
Parameters:
175
coord_map : callable
176
Function mapping (row, col) coordinates
177
shape : tuple
178
Shape of coordinate array
179
dtype : data-type, optional
180
Data type of coordinate array
181
182
Returns:
183
ndarray
184
Coordinate array for warping
185
"""
186
187
def warp_polar(image, center=None, radius=None, output_shape=None, scaling='linear', multichannel=None, channel_axis=None):
188
"""
189
Transform image from Cartesian to polar coordinates.
190
191
Parameters:
192
image : array_like
193
Input image
194
center : tuple, optional
195
Center point for transformation
196
radius : float, optional
197
Maximum radius for transformation
198
output_shape : tuple, optional
199
Shape of output image
200
scaling : str, optional
201
Scaling method ('linear' or 'log')
202
multichannel : bool, optional
203
Whether last axis is channels (deprecated)
204
channel_axis : int, optional
205
Axis for color channels
206
207
Returns:
208
ndarray
209
Polar-transformed image
210
"""
211
```
212
213
### Transformation Classes
214
215
Object-oriented interface for geometric transformations with parameter estimation and composition.
216
217
```python { .api }
218
class EuclideanTransform:
219
"""
220
Euclidean (rigid) transformation: rotation + translation.
221
222
Parameters:
223
rotation : float, optional
224
Rotation angle in radians
225
translation : tuple, optional
226
Translation vector (dx, dy)
227
matrix : array_like, optional
228
Homogeneous transformation matrix
229
"""
230
231
def __init__(self, rotation=None, translation=None, matrix=None):
232
pass
233
234
def estimate(self, src, dst):
235
"""Estimate transformation from point correspondences."""
236
pass
237
238
def __call__(self, coords):
239
"""Apply transformation to coordinates."""
240
pass
241
242
class SimilarityTransform:
243
"""
244
Similarity transformation: rotation + translation + uniform scaling.
245
246
Parameters:
247
matrix : array_like, optional
248
Homogeneous transformation matrix
249
scale : float, optional
250
Scale factor
251
rotation : float, optional
252
Rotation angle in radians
253
translation : tuple, optional
254
Translation vector
255
"""
256
257
def __init__(self, matrix=None, scale=None, rotation=None, translation=None):
258
pass
259
260
def estimate(self, src, dst):
261
"""Estimate transformation from point correspondences."""
262
pass
263
264
def __call__(self, coords):
265
"""Apply transformation to coordinates."""
266
pass
267
268
class AffineTransform:
269
"""
270
Affine transformation: linear transformation + translation.
271
272
Parameters:
273
matrix : array_like, optional
274
Homogeneous transformation matrix
275
scale : tuple, optional
276
Scale factors for each axis
277
rotation : float, optional
278
Rotation angle in radians
279
shear : float, optional
280
Shear angle in radians
281
translation : tuple, optional
282
Translation vector
283
"""
284
285
def __init__(self, matrix=None, scale=None, rotation=None, shear=None, translation=None):
286
pass
287
288
def estimate(self, src, dst):
289
"""Estimate transformation from point correspondences."""
290
pass
291
292
def __call__(self, coords):
293
"""Apply transformation to coordinates."""
294
pass
295
296
class ProjectiveTransform:
297
"""
298
Projective (homography) transformation.
299
300
Parameters:
301
matrix : array_like, optional
302
Homogeneous transformation matrix
303
"""
304
305
def __init__(self, matrix=None):
306
pass
307
308
def estimate(self, src, dst):
309
"""Estimate transformation from point correspondences."""
310
pass
311
312
def __call__(self, coords):
313
"""Apply transformation to coordinates."""
314
pass
315
316
class PiecewiseAffineTransform:
317
"""
318
Piecewise affine transformation using triangulation.
319
"""
320
321
def __init__(self):
322
pass
323
324
def estimate(self, src, dst):
325
"""Estimate transformation from point correspondences."""
326
pass
327
328
def __call__(self, coords):
329
"""Apply transformation to coordinates."""
330
pass
331
332
class PolynomialTransform:
333
"""
334
Polynomial coordinate transformation.
335
336
Parameters:
337
params : array_like, optional
338
Polynomial coefficients
339
"""
340
341
def __init__(self, params=None):
342
pass
343
344
def estimate(self, src, dst, order=2):
345
"""Estimate transformation from point correspondences."""
346
pass
347
348
def __call__(self, coords):
349
"""Apply transformation to coordinates."""
350
pass
351
```
352
353
### Transformation Estimation
354
355
Estimate geometric transformations from point correspondences using various algorithms.
356
357
```python { .api }
358
def estimate_transform(ttype, src, dst, **kwargs):
359
"""
360
Estimate geometric transformation from point correspondences.
361
362
Parameters:
363
ttype : str or Transform class
364
Type of transformation ('euclidean', 'similarity', 'affine', 'projective', 'polynomial', 'piecewise-affine')
365
src : array_like
366
Source coordinates
367
dst : array_like
368
Destination coordinates
369
**kwargs
370
Additional arguments for transformation estimation
371
372
Returns:
373
Transform
374
Estimated transformation object
375
"""
376
377
def matrix_transform(coords, matrix):
378
"""
379
Apply matrix transformation to coordinates.
380
381
Parameters:
382
coords : array_like
383
Input coordinates
384
matrix : array_like
385
Transformation matrix
386
387
Returns:
388
ndarray
389
Transformed coordinates
390
"""
391
```
392
393
### Hough Transforms
394
395
Detect geometric shapes using Hough transform methods.
396
397
```python { .api }
398
def hough_line(image, theta=None):
399
"""
400
Classical Hough transform for straight line detection.
401
402
Parameters:
403
image : array_like
404
Input binary edge image
405
theta : array_like, optional
406
Angles to test (default: 0 to π)
407
408
Returns:
409
tuple
410
(accumulator, angles, distances)
411
"""
412
413
def hough_line_peaks(hspace, angles, dists, min_angle=1, min_distance=1, threshold=None, num_peaks=np.inf):
414
"""
415
Find peaks in Hough line transform.
416
417
Parameters:
418
hspace : array_like
419
Hough transform accumulator
420
angles : array_like
421
Angles corresponding to accumulator
422
dists : array_like
423
Distances corresponding to accumulator
424
min_angle : float, optional
425
Minimum angle between peaks
426
min_distance : float, optional
427
Minimum distance between peaks
428
threshold : float, optional
429
Minimum peak threshold
430
num_peaks : int, optional
431
Maximum number of peaks
432
433
Returns:
434
tuple
435
(peak_indices, angles, distances)
436
"""
437
438
def probabilistic_hough_line(image, threshold=10, line_length=50, line_gap=10, theta=None, seed=None):
439
"""
440
Probabilistic Hough transform for line detection.
441
442
Parameters:
443
image : array_like
444
Input binary edge image
445
threshold : int, optional
446
Minimum vote threshold
447
line_length : int, optional
448
Minimum line length
449
line_gap : int, optional
450
Maximum gap between line segments
451
theta : array_like, optional
452
Angles to test
453
seed : int, optional
454
Random seed
455
456
Returns:
457
list
458
List of line segments as ((x0, y0), (x1, y1))
459
"""
460
461
def hough_circle(image, radius, normalize=True, full_output=False):
462
"""
463
Hough transform for circle detection.
464
465
Parameters:
466
image : array_like
467
Input binary edge image
468
radius : array_like
469
Circle radii to detect
470
normalize : bool, optional
471
Normalize accumulator by circle perimeter
472
full_output : bool, optional
473
Return additional outputs
474
475
Returns:
476
ndarray or tuple
477
Accumulator array(s)
478
"""
479
480
def hough_circle_peaks(hspaces, radii, min_xdistance=1, min_ydistance=1, threshold=None, num_peaks=np.inf, total_num_peaks=np.inf, normalize=False):
481
"""
482
Find peaks in Hough circle transform.
483
484
Parameters:
485
hspaces : array_like
486
Circle accumulator arrays
487
radii : array_like
488
Circle radii
489
min_xdistance : int, optional
490
Minimum x distance between peaks
491
min_ydistance : int, optional
492
Minimum y distance between peaks
493
threshold : float, optional
494
Minimum peak threshold
495
num_peaks : int, optional
496
Maximum peaks per radius
497
total_num_peaks : int, optional
498
Maximum total peaks
499
normalize : bool, optional
500
Normalize by circle perimeter
501
502
Returns:
503
tuple
504
(y_peaks, x_peaks, radii_peaks)
505
"""
506
507
def hough_ellipse(image, accuracy=1, threshold=4, min_size=4, max_size=None):
508
"""
509
Hough transform for ellipse detection.
510
511
Parameters:
512
image : array_like
513
Input binary edge image
514
accuracy : float, optional
515
Bin size for accumulator
516
threshold : int, optional
517
Minimum vote threshold
518
min_size : float, optional
519
Minimum ellipse size
520
max_size : float, optional
521
Maximum ellipse size
522
523
Returns:
524
list
525
List of ellipse parameters
526
"""
527
```
528
529
### Radon Transform
530
531
Radon transform and inverse transform for tomographic reconstruction and analysis.
532
533
```python { .api }
534
def radon(image, theta=None, circle=None, preserve_range=None):
535
"""
536
Compute Radon transform (forward projection).
537
538
Parameters:
539
image : array_like
540
Input image
541
theta : array_like, optional
542
Projection angles in degrees
543
circle : bool, optional
544
Whether to use circular domain
545
preserve_range : bool, optional
546
Keep original data range
547
548
Returns:
549
ndarray
550
Radon transform (sinogram)
551
"""
552
553
def iradon(radon_image, theta=None, output_size=None, filter_name='ramp', interpolation='linear', circle=None, preserve_range=None):
554
"""
555
Compute inverse Radon transform (filtered backprojection).
556
557
Parameters:
558
radon_image : array_like
559
Input sinogram
560
theta : array_like, optional
561
Projection angles in degrees
562
output_size : int, optional
563
Output image size
564
filter_name : str, optional
565
Filter type ('ramp', 'shepp-logan', 'cosine', 'hamming', 'hann', None)
566
interpolation : str, optional
567
Interpolation method ('linear' or 'nearest')
568
circle : bool, optional
569
Whether to use circular domain
570
preserve_range : bool, optional
571
Keep original data range
572
573
Returns:
574
ndarray
575
Reconstructed image
576
"""
577
578
def iradon_sart(radon_image, theta=None, image=None, projection_shifts=None, clip=None, relaxation=0.15):
579
"""
580
Simultaneous Algebraic Reconstruction Technique (SART).
581
582
Parameters:
583
radon_image : array_like
584
Input sinogram
585
theta : array_like, optional
586
Projection angles in degrees
587
image : array_like, optional
588
Initial image estimate
589
projection_shifts : array_like, optional
590
Shifts for each projection
591
clip : tuple, optional
592
Data range for clipping
593
relaxation : float, optional
594
Relaxation parameter
595
596
Returns:
597
ndarray
598
Reconstructed image
599
"""
600
```
601
602
### Image Pyramids
603
604
Generate and manipulate image pyramids for multi-scale analysis and processing.
605
606
```python { .api }
607
def pyramid_reduce(image, downscale=2, sigma=None, order=1, mode='reflect', cval=0, multichannel=None, preserve_range=False, channel_axis=None):
608
"""
609
Smooth and downsample image.
610
611
Parameters:
612
image : array_like
613
Input image
614
downscale : float, optional
615
Downsampling factor
616
sigma : float, optional
617
Gaussian smoothing parameter
618
order : int, optional
619
Interpolation order
620
mode : str, optional
621
Boundary condition mode
622
cval : float, optional
623
Constant value for constant mode
624
multichannel : bool, optional
625
Whether last axis is channels (deprecated)
626
preserve_range : bool, optional
627
Keep original data range
628
channel_axis : int, optional
629
Axis for color channels
630
631
Returns:
632
ndarray
633
Downsampled image
634
"""
635
636
def pyramid_expand(image, upscale=2, sigma=None, order=1, mode='reflect', cval=0, multichannel=None, preserve_range=False, channel_axis=None):
637
"""
638
Upsample and smooth image.
639
640
Parameters:
641
image : array_like
642
Input image
643
upscale : float, optional
644
Upsampling factor
645
sigma : float, optional
646
Gaussian smoothing parameter
647
order : int, optional
648
Interpolation order
649
mode : str, optional
650
Boundary condition mode
651
cval : float, optional
652
Constant value for constant mode
653
multichannel : bool, optional
654
Whether last axis is channels (deprecated)
655
preserve_range : bool, optional
656
Keep original data range
657
channel_axis : int, optional
658
Axis for color channels
659
660
Returns:
661
ndarray
662
Upsampled image
663
"""
664
665
def pyramid_gaussian(image, max_layer=-1, downscale=2, sigma=None, order=1, mode='reflect', cval=0, multichannel=None, preserve_range=False, channel_axis=None):
666
"""
667
Generate Gaussian pyramid.
668
669
Parameters:
670
image : array_like
671
Input image
672
max_layer : int, optional
673
Number of pyramid layers
674
downscale : float, optional
675
Downsampling factor between layers
676
sigma : float, optional
677
Gaussian smoothing parameter
678
order : int, optional
679
Interpolation order
680
mode : str, optional
681
Boundary condition mode
682
cval : float, optional
683
Constant value for constant mode
684
multichannel : bool, optional
685
Whether last axis is channels (deprecated)
686
preserve_range : bool, optional
687
Keep original data range
688
channel_axis : int, optional
689
Axis for color channels
690
691
Yields:
692
ndarray
693
Images in pyramid from finest to coarsest
694
"""
695
696
def pyramid_laplacian(image, max_layer=-1, downscale=2, sigma=None, order=1, mode='reflect', cval=0, multichannel=None, preserve_range=False, channel_axis=None):
697
"""
698
Generate Laplacian pyramid.
699
700
Parameters:
701
image : array_like
702
Input image
703
max_layer : int, optional
704
Number of pyramid layers
705
downscale : float, optional
706
Downsampling factor between layers
707
sigma : float, optional
708
Gaussian smoothing parameter
709
order : int, optional
710
Interpolation order
711
mode : str, optional
712
Boundary condition mode
713
cval : float, optional
714
Constant value for constant mode
715
multichannel : bool, optional
716
Whether last axis is channels (deprecated)
717
preserve_range : bool, optional
718
Keep original data range
719
channel_axis : int, optional
720
Axis for color channels
721
722
Yields:
723
ndarray
724
Images in pyramid from finest to coarsest
725
"""
726
```
727
728
## Usage Examples
729
730
### Basic Geometric Transformations
731
732
```python
733
from skimage import data, transform
734
import matplotlib.pyplot as plt
735
import numpy as np
736
737
# Load image
738
image = data.camera()
739
740
# Apply basic transformations
741
resized = transform.resize(image, (256, 256))
742
rotated = transform.rotate(image, 45)
743
scaled = transform.rescale(image, 0.5, anti_aliasing=True)
744
745
# Display results
746
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
747
axes[0, 0].imshow(image, cmap='gray')
748
axes[0, 0].set_title('Original')
749
axes[0, 1].imshow(resized, cmap='gray')
750
axes[0, 1].set_title('Resized')
751
axes[1, 0].imshow(rotated, cmap='gray')
752
axes[1, 0].set_title('Rotated 45°')
753
axes[1, 1].imshow(scaled, cmap='gray')
754
axes[1, 1].set_title('Scaled 0.5x')
755
plt.show()
756
```
757
758
### Image Registration
759
760
```python
761
from skimage import data, transform
762
import numpy as np
763
764
# Create reference and transformed images
765
reference = data.camera()
766
angle = 30
767
tform = transform.SimilarityTransform(rotation=np.deg2rad(angle))
768
shifted = transform.warp(reference, tform.inverse)
769
770
# Estimate transformation
771
src = np.array([[100, 100], [200, 100], [100, 200]])
772
dst = tform(src)
773
774
# Estimate transformation from correspondences
775
estimated_tform = transform.estimate_transform('similarity', src, dst)
776
recovered = transform.warp(shifted, estimated_tform.inverse)
777
778
print(f"Original angle: {angle}°")
779
print(f"Estimated angle: {np.rad2deg(estimated_tform.rotation):.1f}°")
780
```
781
782
### Hough Line Detection
783
784
```python
785
from skimage import data, feature, transform
786
import matplotlib.pyplot as plt
787
import numpy as np
788
789
# Load and detect edges
790
image = data.camera()
791
edges = feature.canny(image, sigma=2, low_threshold=0.1, high_threshold=0.2)
792
793
# Classical Hough transform
794
tested_angles = np.linspace(-np.pi/2, np.pi/2, 360, endpoint=False)
795
h, theta, d = transform.hough_line(edges, theta=tested_angles)
796
797
# Find peaks
798
hough_peaks = transform.hough_line_peaks(h, theta, d, num_peaks=5)
799
800
# Display results
801
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
802
axes[0].imshow(image, cmap='gray')
803
axes[0].set_title('Original')
804
axes[1].imshow(edges, cmap='gray')
805
axes[1].set_title('Edges')
806
axes[2].imshow(h, extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]], cmap='hot', aspect='auto')
807
axes[2].set_title('Hough Transform')
808
plt.show()
809
```
810
811
## Types
812
813
```python { .api }
814
from typing import Union, Optional, Tuple, Callable, Generator
815
from numpy.typing import NDArray
816
import numpy as np
817
818
# Transformation parameters
819
TransformMatrix = NDArray[np.floating]
820
CoordinateArray = NDArray[np.floating]
821
TransformType = str
822
823
# Transformation classes
824
Transform = Union[EuclideanTransform, SimilarityTransform, AffineTransform, ProjectiveTransform, PiecewiseAffineTransform, PolynomialTransform]
825
826
# Hough transform results
827
HoughAccumulator = NDArray[np.floating]
828
HoughPeaks = Tuple[NDArray[np.integer], NDArray[np.floating], NDArray[np.floating]]
829
830
# Pyramid generator
831
PyramidGenerator = Generator[NDArray[np.floating], None, None]
832
833
# Geometric shapes
834
LineSegment = Tuple[Tuple[int, int], Tuple[int, int]]
835
Circle = Tuple[int, int, float] # (center_x, center_y, radius)
836
Ellipse = Tuple[float, float, float, float, float] # (center_x, center_y, a, b, orientation)
837
```