|
| 1 | +"""Morphological operations implemented with numba to replace cv2 dependency.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from numba import njit, prange |
| 5 | + |
| 6 | + |
| 7 | +@njit |
| 8 | +def dilate(image, kernel, iterations=1): |
| 9 | + """Dilate an image using a structuring element. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + image : numpy.ndarray |
| 14 | + Input image to dilate. |
| 15 | + kernel : numpy.ndarray |
| 16 | + Structuring element (kernel) for dilation. Should contain 1s where |
| 17 | + the structuring element is active and 0s elsewhere. |
| 18 | + iterations : int, optional |
| 19 | + Number of times to apply the dilation. Default is 1. |
| 20 | +
|
| 21 | + Returns |
| 22 | + ------- |
| 23 | + numpy.ndarray |
| 24 | + Dilated image with the same shape and dtype as input. |
| 25 | +
|
| 26 | + """ |
| 27 | + result = image.copy() |
| 28 | + |
| 29 | + for _ in range(iterations): |
| 30 | + result = _single_dilate(result, kernel) |
| 31 | + |
| 32 | + return result |
| 33 | + |
| 34 | +@njit |
| 35 | +def erode(image, kernel, iterations=1): |
| 36 | + """Erode an image using a structuring element. |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + image : numpy.ndarray |
| 41 | + Input image to erode. |
| 42 | + kernel : numpy.ndarray |
| 43 | + Structuring element (kernel) for erosion. Should contain 1s where |
| 44 | + the structuring element is active and 0s elsewhere. |
| 45 | + iterations : int, optional |
| 46 | + Number of times to apply the erosion. Default is 1. |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + numpy.ndarray |
| 51 | + Eroded image with the same shape and dtype as input. |
| 52 | +
|
| 53 | + """ |
| 54 | + result = image.copy() |
| 55 | + |
| 56 | + for _ in range(iterations): |
| 57 | + result = _single_erode(result, kernel) |
| 58 | + |
| 59 | + return result |
| 60 | + |
| 61 | +@njit(parallel=True) |
| 62 | +def _single_dilate(image, kernel): |
| 63 | + """Single iteration of dilation operation.""" |
| 64 | + height, width = image.shape |
| 65 | + kh, kw = kernel.shape |
| 66 | + kh_half, kw_half = kh // 2, kw // 2 |
| 67 | + |
| 68 | + # Create output array |
| 69 | + result = np.zeros_like(image) |
| 70 | + |
| 71 | + # Apply dilation - for each output pixel, find max in kernel neighborhood |
| 72 | + for i in prange(height): |
| 73 | + for j in range(width): |
| 74 | + max_val = image[i, j] # Start with current pixel value |
| 75 | + |
| 76 | + for ki in range(kh): |
| 77 | + for kj in range(kw): |
| 78 | + if kernel[ki, kj] > 0: # Only consider active kernel elements |
| 79 | + # Calculate the source image coordinates |
| 80 | + img_i = i + ki - kh_half |
| 81 | + img_j = j + kj - kw_half |
| 82 | + |
| 83 | + # Check bounds |
| 84 | + if 0 <= img_i < height and 0 <= img_j < width: |
| 85 | + if image[img_i, img_j] > max_val: |
| 86 | + max_val = image[img_i, img_j] |
| 87 | + |
| 88 | + result[i, j] = max_val |
| 89 | + |
| 90 | + return result |
| 91 | + |
| 92 | +@njit(parallel=True) |
| 93 | +def _single_erode(image, kernel): |
| 94 | + """Single iteration of erosion operation.""" |
| 95 | + height, width = image.shape |
| 96 | + kh, kw = kernel.shape |
| 97 | + kh_half, kw_half = kh // 2, kw // 2 |
| 98 | + |
| 99 | + # Create output array |
| 100 | + result = np.zeros_like(image) |
| 101 | + |
| 102 | + # Apply erosion - for each output pixel, find min in kernel neighborhood |
| 103 | + for i in prange(height): |
| 104 | + for j in range(width): |
| 105 | + min_val = image[i, j] # Start with current pixel value |
| 106 | + |
| 107 | + for ki in range(kh): |
| 108 | + for kj in range(kw): |
| 109 | + if kernel[ki, kj] > 0: # Only consider active kernel elements |
| 110 | + # Calculate the source image coordinates |
| 111 | + img_i = i + ki - kh_half |
| 112 | + img_j = j + kj - kw_half |
| 113 | + |
| 114 | + # Check bounds - treat out of bounds as 0 for erosion |
| 115 | + if 0 <= img_i < height and 0 <= img_j < width: |
| 116 | + if image[img_i, img_j] < min_val: |
| 117 | + min_val = image[img_i, img_j] |
| 118 | + else: |
| 119 | + # Outside bounds treated as 0, so erosion result should be 0 |
| 120 | + min_val = 0 |
| 121 | + break |
| 122 | + |
| 123 | + result[i, j] = min_val |
| 124 | + |
| 125 | + return result |
0 commit comments