0
# Feature Detection
1
2
Feature detection and description algorithms for computer vision applications. Includes corner detection, blob detection, texture analysis, keypoint descriptors, and feature matching capabilities.
3
4
## Capabilities
5
6
### Corner Detection
7
8
Detect and analyze corner points using various corner detection algorithms.
9
10
```python { .api }
11
def corner_harris(image, method='k', k=0.05, eps=1e-6, sigma=1):
12
"""
13
Apply Harris corner detection.
14
15
Parameters:
16
image : array_like
17
Input grayscale image
18
method : str, optional
19
Method for corner response ('k' or 'eps')
20
k : float, optional
21
Harris detector sensitivity parameter
22
eps : float, optional
23
Epsilon for method='eps'
24
sigma : float, optional
25
Standard deviation of Gaussian kernel
26
27
Returns:
28
ndarray
29
Harris corner response image
30
"""
31
32
def corner_shi_tomasi(image, sigma=1):
33
"""
34
Apply Shi-Tomasi corner detection.
35
36
Parameters:
37
image : array_like
38
Input grayscale image
39
sigma : float, optional
40
Standard deviation of Gaussian kernel
41
42
Returns:
43
ndarray
44
Shi-Tomasi corner response image
45
"""
46
47
def corner_foerstner(image, sigma=1):
48
"""
49
Apply Foerstner corner detection.
50
51
Parameters:
52
image : array_like
53
Input grayscale image
54
sigma : float, optional
55
Standard deviation of Gaussian kernel
56
57
Returns:
58
tuple
59
(corner_response, roundness, accuracy)
60
"""
61
62
def corner_kitchen_rosenfeld(image, mode='constant', cval=0):
63
"""
64
Apply Kitchen-Rosenfeld corner detection.
65
66
Parameters:
67
image : array_like
68
Input grayscale image
69
mode : str, optional
70
Boundary condition mode
71
cval : float, optional
72
Value for constant mode
73
74
Returns:
75
ndarray
76
Corner response image
77
"""
78
79
def corner_fast(image, n=12, threshold=0.15, nonmax_suppression=True):
80
"""
81
Apply FAST corner detection.
82
83
Parameters:
84
image : array_like
85
Input grayscale image
86
n : int, optional
87
Number of consecutive pixels for corner detection
88
threshold : float, optional
89
Intensity threshold for corner detection
90
nonmax_suppression : bool, optional
91
Whether to apply non-maximum suppression
92
93
Returns:
94
ndarray
95
Corner coordinates array
96
"""
97
98
def corner_moravec(image, window_size=1):
99
"""
100
Apply Moravec corner detection.
101
102
Parameters:
103
image : array_like
104
Input grayscale image
105
window_size : int, optional
106
Size of sliding window
107
108
Returns:
109
ndarray
110
Moravec corner response image
111
"""
112
113
def corner_peaks(image, min_distance=1, threshold_abs=None, threshold_rel=None, exclude_border=True, num_peaks=np.inf, footprint=None, labels=None):
114
"""
115
Find corner peaks in corner response image.
116
117
Parameters:
118
image : array_like
119
Corner response image
120
min_distance : int, optional
121
Minimum distance between peaks
122
threshold_abs : float, optional
123
Minimum absolute intensity
124
threshold_rel : float, optional
125
Minimum relative intensity
126
exclude_border : bool, optional
127
Whether to exclude border
128
num_peaks : int, optional
129
Maximum number of peaks
130
footprint : array_like, optional
131
Footprint for peak detection
132
labels : array_like, optional
133
Labeled regions
134
135
Returns:
136
ndarray
137
Corner coordinates array
138
"""
139
140
def corner_subpix(image, corners, window_size=11, alpha=0.99):
141
"""
142
Refine corner coordinates to subpixel accuracy.
143
144
Parameters:
145
image : array_like
146
Input grayscale image
147
corners : array_like
148
Corner coordinates
149
window_size : int, optional
150
Size of neighborhood window
151
alpha : float, optional
152
Significance level for termination
153
154
Returns:
155
ndarray
156
Refined corner coordinates
157
"""
158
159
def corner_orientations(image, corners, mask):
160
"""
161
Compute corner orientations.
162
163
Parameters:
164
image : array_like
165
Input grayscale image
166
corners : array_like
167
Corner coordinates
168
mask : array_like
169
Circular mask for orientation computation
170
171
Returns:
172
ndarray
173
Corner orientations in radians
174
"""
175
```
176
177
### Blob Detection
178
179
Detect blob-like structures using scale-space methods.
180
181
```python { .api }
182
def blob_dog(image, min_sigma=1, max_sigma=50, sigma_ratio=1.6, threshold=0.1, overlap=0.5, exclude_border=True):
183
"""
184
Apply Difference of Gaussian blob detection.
185
186
Parameters:
187
image : array_like
188
Input grayscale image
189
min_sigma : float, optional
190
Minimum standard deviation
191
max_sigma : float, optional
192
Maximum standard deviation
193
sigma_ratio : float, optional
194
Ratio between successive sigma values
195
threshold : float, optional
196
Detection threshold
197
overlap : float, optional
198
Maximum overlap between blobs
199
exclude_border : bool, optional
200
Whether to exclude border
201
202
Returns:
203
ndarray
204
Blob coordinates and scales (row, col, sigma)
205
"""
206
207
def blob_log(image, min_sigma=1, max_sigma=50, num_sigma=10, threshold=0.1, overlap=0.5, log_scale=False, exclude_border=True):
208
"""
209
Apply Laplacian of Gaussian blob detection.
210
211
Parameters:
212
image : array_like
213
Input grayscale image
214
min_sigma : float, optional
215
Minimum standard deviation
216
max_sigma : float, optional
217
Maximum standard deviation
218
num_sigma : int, optional
219
Number of sigma values
220
threshold : float, optional
221
Detection threshold
222
overlap : float, optional
223
Maximum overlap between blobs
224
log_scale : bool, optional
225
Whether to use logarithmic scale
226
exclude_border : bool, optional
227
Whether to exclude border
228
229
Returns:
230
ndarray
231
Blob coordinates and scales (row, col, sigma)
232
"""
233
234
def blob_doh(image, min_sigma=1, max_sigma=30, num_sigma=10, threshold=0.01, overlap=0.5, log_scale=False, exclude_border=True):
235
"""
236
Apply Determinant of Hessian blob detection.
237
238
Parameters:
239
image : array_like
240
Input grayscale image
241
min_sigma : float, optional
242
Minimum standard deviation
243
max_sigma : float, optional
244
Maximum standard deviation
245
num_sigma : int, optional
246
Number of sigma values
247
threshold : float, optional
248
Detection threshold
249
overlap : float, optional
250
Maximum overlap between blobs
251
log_scale : bool, optional
252
Whether to use logarithmic scale
253
exclude_border : bool, optional
254
Whether to exclude border
255
256
Returns:
257
ndarray
258
Blob coordinates and scales (row, col, sigma)
259
"""
260
```
261
262
### Feature Descriptor Classes
263
264
Object-oriented interface for feature detection and description.
265
266
```python { .api }
267
class ORB:
268
"""
269
Oriented FAST and Rotated BRIEF feature detector and descriptor.
270
271
Parameters:
272
n_keypoints : int, optional
273
Maximum number of keypoints
274
fast_n : int, optional
275
FAST parameter n
276
fast_threshold : float, optional
277
FAST intensity threshold
278
harris_k : float, optional
279
Harris corner detection parameter
280
pyramid_levels : int, optional
281
Number of pyramid levels
282
downscale : float, optional
283
Downscaling factor between pyramid levels
284
"""
285
286
def __init__(self, n_keypoints=500, fast_n=9, fast_threshold=0.08, harris_k=0.04, pyramid_levels=8, downscale=1.2):
287
pass
288
289
def detect(self, image):
290
"""
291
Detect keypoints.
292
293
Parameters:
294
image : array_like
295
Input grayscale image
296
297
Returns:
298
ndarray
299
Keypoint coordinates
300
"""
301
pass
302
303
def extract(self, image, keypoints):
304
"""
305
Extract descriptors.
306
307
Parameters:
308
image : array_like
309
Input grayscale image
310
keypoints : array_like
311
Keypoint coordinates
312
313
Returns:
314
ndarray
315
Binary descriptors
316
"""
317
pass
318
319
def detect_and_extract(self, image):
320
"""
321
Detect keypoints and extract descriptors.
322
323
Parameters:
324
image : array_like
325
Input grayscale image
326
327
Returns:
328
tuple
329
(keypoints, descriptors)
330
"""
331
pass
332
333
class BRIEF:
334
"""
335
Binary Robust Independent Elementary Features descriptor.
336
337
Parameters:
338
descriptor_size : int, optional
339
Length of descriptor in bits
340
patch_size : int, optional
341
Size of sampling patch
342
mode : str, optional
343
Sampling pattern mode
344
sample_seed : int, optional
345
Random seed for sampling pattern
346
sigma : float, optional
347
Standard deviation for Gaussian sampling
348
"""
349
350
def __init__(self, descriptor_size=256, patch_size=49, mode='normal', sample_seed=1, sigma=1):
351
pass
352
353
def extract(self, image, keypoints):
354
"""
355
Extract BRIEF descriptors.
356
357
Parameters:
358
image : array_like
359
Input grayscale image
360
keypoints : array_like
361
Keypoint coordinates
362
363
Returns:
364
ndarray
365
Binary descriptors
366
"""
367
pass
368
369
class SIFT:
370
"""
371
Scale-Invariant Feature Transform detector and descriptor.
372
373
Parameters:
374
upsampling : int, optional
375
Upsampling factor for input image
376
n_octaves : int, optional
377
Number of octaves
378
n_scales : int, optional
379
Number of scales per octave
380
sigma_min : float, optional
381
Minimum scale
382
sigma_in : float, optional
383
Assumed blur of input image
384
c_dog : float, optional
385
Threshold for DoG response
386
c_edge : float, optional
387
Threshold for edge response
388
n_bins : int, optional
389
Number of bins in orientation histogram
390
lambda_ori : float, optional
391
Peak threshold for orientation assignment
392
c_max : float, optional
393
Maximum corner response
394
lambda_descr : float, optional
395
Non-maximum suppression threshold
396
n_hist : int, optional
397
Number of histograms
398
n_ori : int, optional
399
Number of orientations
400
"""
401
402
def __init__(self, upsampling=2, n_octaves=8, n_scales=3, sigma_min=1.6, sigma_in=0.5, c_dog=0.013333333333333334, c_edge=10, n_bins=36, lambda_ori=1.5, c_max=0.8, lambda_descr=6, n_hist=4, n_ori=8):
403
pass
404
405
def detect_and_extract(self, image):
406
"""
407
Detect keypoints and extract descriptors.
408
409
Parameters:
410
image : array_like
411
Input grayscale image
412
413
Returns:
414
tuple
415
(keypoints, descriptors)
416
"""
417
pass
418
419
class CENSURE:
420
"""
421
CENter SURround Extremas feature detector.
422
423
Parameters:
424
min_scale : int, optional
425
Minimum octave scale
426
max_scale : int, optional
427
Maximum octave scale
428
mode : str, optional
429
Detection mode
430
threshold : float, optional
431
Feature response threshold
432
non_max_threshold : float, optional
433
Non-maximum suppression threshold
434
line_threshold : float, optional
435
Line response threshold
436
"""
437
438
def __init__(self, min_scale=1, max_scale=7, mode='DoB', threshold=0.15, non_max_threshold=0.15, line_threshold=10):
439
pass
440
441
def detect(self, image):
442
"""
443
Detect keypoints.
444
445
Parameters:
446
image : array_like
447
Input grayscale image
448
449
Returns:
450
ndarray
451
Keypoint coordinates and scales
452
"""
453
pass
454
```
455
456
### Feature Matching
457
458
Match feature descriptors between images for correspondence and registration.
459
460
```python { .api }
461
def match_descriptors(descriptors1, descriptors2, metric='euclidean', p=2, max_distance=np.inf, cross_check=True, max_ratio=1.0):
462
"""
463
Match feature descriptors between two images.
464
465
Parameters:
466
descriptors1 : array_like
467
First set of descriptors
468
descriptors2 : array_like
469
Second set of descriptors
470
metric : str, optional
471
Distance metric ('euclidean', 'cityblock', 'minkowski', 'hamming')
472
p : float, optional
473
Parameter for Minkowski metric
474
max_distance : float, optional
475
Maximum distance for matches
476
cross_check : bool, optional
477
Whether to apply cross-check filtering
478
max_ratio : float, optional
479
Maximum ratio for Lowe's ratio test
480
481
Returns:
482
ndarray
483
Match indices
484
"""
485
486
def plot_matches(ax, image1, image2, keypoints1, keypoints2, matches, keypoints_color='k', matches_color=None, only_matches=False):
487
"""
488
Plot matched features between two images.
489
490
Parameters:
491
ax : matplotlib axis
492
Matplotlib axis for plotting
493
image1 : array_like
494
First image
495
image2 : array_like
496
Second image
497
keypoints1 : array_like
498
Keypoints from first image
499
keypoints2 : array_like
500
Keypoints from second image
501
matches : array_like
502
Match indices
503
keypoints_color : color, optional
504
Color for keypoint markers
505
matches_color : color, optional
506
Color for match lines
507
only_matches : bool, optional
508
Whether to show only matched keypoints
509
"""
510
pass
511
```
512
513
### Texture Analysis
514
515
Analyze texture patterns using statistical and structural methods.
516
517
```python { .api }
518
def local_binary_pattern(image, P, R, method='default'):
519
"""
520
Compute Local Binary Pattern for texture analysis.
521
522
Parameters:
523
image : array_like
524
Input grayscale image
525
P : int
526
Number of sample points
527
R : float
528
Radius of sample circle
529
method : str, optional
530
LBP method ('default', 'ror', 'uniform', 'nri_uniform', 'var')
531
532
Returns:
533
ndarray
534
LBP pattern image
535
"""
536
537
def graycomatrix(image, distances, angles, levels=None, symmetric=False, normed=False):
538
"""
539
Compute Gray-Level Co-occurrence Matrix.
540
541
Parameters:
542
image : array_like
543
Input integer image
544
distances : array_like
545
Pixel pair distance offsets
546
angles : array_like
547
Pixel pair angles in radians
548
levels : int, optional
549
Number of gray levels
550
symmetric : bool, optional
551
Whether matrix is symmetric
552
normed : bool, optional
553
Whether to normalize matrix
554
555
Returns:
556
ndarray
557
Gray-level co-occurrence matrix
558
"""
559
560
def graycoprops(P, prop='contrast'):
561
"""
562
Compute texture properties from Gray-Level Co-occurrence Matrix.
563
564
Parameters:
565
P : array_like
566
Gray-level co-occurrence matrix
567
prop : str, optional
568
Property to compute ('contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', 'correlation')
569
570
Returns:
571
ndarray
572
Texture property values
573
"""
574
575
def multiblock_lbp(image, r, c, width, height):
576
"""
577
Compute Multi-block Local Binary Pattern.
578
579
Parameters:
580
image : array_like
581
Input grayscale image
582
r : int
583
Row coordinate of top-left corner
584
c : int
585
Column coordinate of top-left corner
586
width : int
587
Width of blocks
588
height : int
589
Height of blocks
590
591
Returns:
592
int
593
Multi-block LBP value
594
"""
595
```
596
597
### Advanced Feature Descriptors
598
599
Generate advanced feature descriptors for complex recognition tasks.
600
601
```python { .api }
602
def daisy(image, step=4, radius=15, rings=2, histograms=8, orientations=8, normalization='l1', sigmas=None, ring_radii=None, visualize=False):
603
"""
604
Extract DAISY feature descriptors.
605
606
Parameters:
607
image : array_like
608
Input grayscale image
609
step : int, optional
610
Distance between descriptor centers
611
radius : int, optional
612
Radius of outermost ring
613
rings : int, optional
614
Number of rings
615
histograms : int, optional
616
Number of histograms per ring
617
orientations : int, optional
618
Number of orientations per histogram
619
normalization : str, optional
620
Normalization method ('l1', 'l2', 'daisy', 'off')
621
sigmas : array_like, optional
622
Standard deviations for smoothing
623
ring_radii : array_like, optional
624
Ring radii
625
visualize : bool, optional
626
Whether to return visualization
627
628
Returns:
629
ndarray
630
DAISY descriptors
631
"""
632
633
def hog(image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(3, 3), block_norm='L2-Hys', visualize=False, transform_sqrt=False, feature_vector=True, multichannel=None, channel_axis=None):
634
"""
635
Extract Histogram of Oriented Gradients (HOG) features.
636
637
Parameters:
638
image : array_like
639
Input image
640
orientations : int, optional
641
Number of orientation bins
642
pixels_per_cell : tuple, optional
643
Size of cell in pixels
644
cells_per_block : tuple, optional
645
Number of cells per block
646
block_norm : str, optional
647
Block normalization method
648
visualize : bool, optional
649
Whether to return HOG visualization
650
transform_sqrt : bool, optional
651
Apply power law compression
652
feature_vector : bool, optional
653
Return feature vector
654
multichannel : bool, optional
655
Whether last axis is channels (deprecated)
656
channel_axis : int, optional
657
Axis for color channels
658
659
Returns:
660
ndarray or tuple
661
HOG features and optionally visualization
662
"""
663
```
664
665
### Peak Detection
666
667
Detect local maxima for keypoint extraction and feature analysis.
668
669
```python { .api }
670
def peak_local_maxima(image, min_distance=1, threshold_abs=None, threshold_rel=None, exclude_border=True, num_peaks=np.inf, footprint=None, labels=None, num_peaks_per_label=np.inf, p_norm=np.inf):
671
"""
672
Find local maxima in an image.
673
674
Parameters:
675
image : array_like
676
Input image
677
min_distance : int, optional
678
Minimum distance between peaks
679
threshold_abs : float, optional
680
Minimum absolute intensity
681
threshold_rel : float, optional
682
Minimum relative intensity
683
exclude_border : bool, optional
684
Whether to exclude border
685
num_peaks : int, optional
686
Maximum number of peaks
687
footprint : array_like, optional
688
Footprint for peak detection
689
labels : array_like, optional
690
Labeled regions
691
num_peaks_per_label : int, optional
692
Maximum peaks per label
693
p_norm : float, optional
694
P-norm for distance calculation
695
696
Returns:
697
ndarray
698
Peak coordinates
699
"""
700
```
701
702
### Template Matching
703
704
Match template patterns within images for object detection.
705
706
```python { .api }
707
def match_template(image, template, pad_input=False, mode='constant', constant_values=0):
708
"""
709
Match template using normalized cross-correlation.
710
711
Parameters:
712
image : array_like
713
Input image
714
template : array_like
715
Template image
716
pad_input : bool, optional
717
Whether to pad input image
718
mode : str, optional
719
Padding mode
720
constant_values : scalar, optional
721
Constant value for padding
722
723
Returns:
724
ndarray
725
Cross-correlation result
726
"""
727
```
728
729
## Usage Examples
730
731
### Corner Detection and Analysis
732
733
```python
734
from skimage import data, feature
735
import matplotlib.pyplot as plt
736
import numpy as np
737
738
# Load image
739
image = data.checkerboard()
740
741
# Apply different corner detection methods
742
harris_corners = feature.corner_harris(image)
743
shi_tomasi_corners = feature.corner_shi_tomasi(image)
744
745
# Find corner peaks
746
harris_peaks = feature.corner_peaks(harris_corners, min_distance=5, threshold_rel=0.02)
747
shi_tomasi_peaks = feature.corner_peaks(shi_tomasi_corners, min_distance=5, threshold_rel=0.02)
748
749
# Display results
750
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
751
axes[0, 0].imshow(image, cmap='gray')
752
axes[0, 0].set_title('Original')
753
axes[0, 1].imshow(harris_corners, cmap='hot')
754
axes[0, 1].set_title('Harris Response')
755
axes[1, 0].imshow(image, cmap='gray')
756
axes[1, 0].plot(harris_peaks[:, 1], harris_peaks[:, 0], 'r+', markersize=10)
757
axes[1, 0].set_title(f'Harris Corners ({len(harris_peaks)})')
758
axes[1, 1].imshow(image, cmap='gray')
759
axes[1, 1].plot(shi_tomasi_peaks[:, 1], shi_tomasi_peaks[:, 0], 'b+', markersize=10)
760
axes[1, 1].set_title(f'Shi-Tomasi Corners ({len(shi_tomasi_peaks)})')
761
plt.show()
762
```
763
764
### Blob Detection
765
766
```python
767
from skimage import data, feature
768
import matplotlib.pyplot as plt
769
770
# Load image
771
image = data.hubble_deep_field()[0:500, 0:500]
772
773
# Apply different blob detection methods
774
blobs_log = feature.blob_log(image, max_sigma=30, num_sigma=10, threshold=0.1)
775
blobs_dog = feature.blob_dog(image, max_sigma=30, threshold=0.1)
776
blobs_doh = feature.blob_doh(image, max_sigma=30, threshold=0.01)
777
778
# Scale radii for visualization
779
blobs_log[:, 2] = blobs_log[:, 2] * np.sqrt(2)
780
blobs_dog[:, 2] = blobs_dog[:, 2] * np.sqrt(2)
781
782
print(f"LoG detected {len(blobs_log)} blobs")
783
print(f"DoG detected {len(blobs_dog)} blobs")
784
print(f"DoH detected {len(blobs_doh)} blobs")
785
```
786
787
### Feature Matching
788
789
```python
790
from skimage import data, feature, transform
791
import numpy as np
792
793
# Load and transform images
794
image1 = data.camera()
795
tform = transform.AffineTransform(scale=(1.3, 1.1), rotation=0.5, translation=(0, -200))
796
image2 = transform.warp(image1, tform)
797
798
# Extract features using ORB
799
detector = feature.ORB(n_keypoints=200)
800
keypoints1, descriptors1 = detector.detect_and_extract(image1)
801
keypoints2, descriptors2 = detector.detect_and_extract(image2)
802
803
# Match features
804
matches = feature.match_descriptors(descriptors1, descriptors2, cross_check=True)
805
806
print(f"Image 1: {len(keypoints1)} keypoints")
807
print(f"Image 2: {len(keypoints2)} keypoints")
808
print(f"Matches: {len(matches)}")
809
810
# Estimate transformation from matches
811
src = keypoints1[matches[:, 0]]
812
dst = keypoints2[matches[:, 1]]
813
tform_est = transform.estimate_transform('affine', src, dst)
814
print(f"Estimated transformation matrix:\n{tform_est.params}")
815
```
816
817
### Texture Analysis
818
819
```python
820
from skimage import data, feature
821
import numpy as np
822
823
# Load image
824
image = data.brick()
825
826
# Compute Local Binary Pattern
827
lbp = feature.local_binary_pattern(image, P=8, R=1, method='uniform')
828
829
# Compute Gray-Level Co-occurrence Matrix
830
distances = [1, 2, 3]
831
angles = [0, np.pi/4, np.pi/2, 3*np.pi/4]
832
glcm = feature.graycomatrix(image, distances=distances, angles=angles,
833
levels=256, symmetric=True, normed=True)
834
835
# Extract texture properties
836
contrast = feature.graycoprops(glcm, 'contrast')
837
energy = feature.graycoprops(glcm, 'energy')
838
homogeneity = feature.graycoprops(glcm, 'homogeneity')
839
840
print(f"LBP patterns: {len(np.unique(lbp))}")
841
print(f"Mean contrast: {np.mean(contrast):.3f}")
842
print(f"Mean energy: {np.mean(energy):.3f}")
843
print(f"Mean homogeneity: {np.mean(homogeneity):.3f}")
844
```
845
846
## Types
847
848
```python { .api }
849
from typing import Union, Optional, Tuple, List
850
from numpy.typing import NDArray
851
import numpy as np
852
853
# Feature detection results
854
Keypoints = NDArray[np.floating]
855
Descriptors = NDArray[Union[np.floating, np.bool_]]
856
Corners = NDArray[np.integer]
857
Blobs = NDArray[np.floating]
858
859
# Feature matching
860
Matches = NDArray[np.integer]
861
MatchDistances = NDArray[np.floating]
862
863
# Texture analysis
864
LBPPattern = NDArray[np.integer]
865
GLCMatrix = NDArray[np.floating]
866
TextureProperties = NDArray[np.floating]
867
868
# Feature detector classes
869
FeatureDetector = Union[ORB, BRIEF, SIFT, CENSURE]
870
871
# Template matching
872
CorrelationMap = NDArray[np.floating]
873
```