Calculate sample co-occurrence matrix based on input image as well as selected coordinates on image. Implementation is made using basic iteration, as function to be performed (np.max) is non-linear and therefore not callable on the frequency domain. Example: >>> im
(image: np.ndarray, coordinate: tuple[int, int])
| 220 | |
| 221 | |
| 222 | def matrix_concurrency(image: np.ndarray, coordinate: tuple[int, int]) -> np.ndarray: |
| 223 | """ |
| 224 | Calculate sample co-occurrence matrix based on input image |
| 225 | as well as selected coordinates on image. |
| 226 | |
| 227 | Implementation is made using basic iteration, |
| 228 | as function to be performed (np.max) is non-linear and therefore |
| 229 | not callable on the frequency domain. |
| 230 | |
| 231 | Example: |
| 232 | >>> img = np.array([[[108, 201, 72], [255, 11, 127]], |
| 233 | ... [[56, 56, 56], [128, 255, 107]]]) |
| 234 | >>> gray = grayscale(img) |
| 235 | >>> binary = binarize(gray) |
| 236 | >>> morphological = opening_filter(binary) |
| 237 | >>> mask_1 = binary_mask(gray, morphological)[0] |
| 238 | >>> matrix_concurrency(mask_1, (0, 1)) |
| 239 | array([[0., 0.], |
| 240 | [0., 0.]]) |
| 241 | """ |
| 242 | matrix = np.zeros([np.max(image) + 1, np.max(image) + 1]) |
| 243 | |
| 244 | offset_x, offset_y = coordinate |
| 245 | |
| 246 | for x in range(1, image.shape[0] - 1): |
| 247 | for y in range(1, image.shape[1] - 1): |
| 248 | base_pixel = image[x, y] |
| 249 | offset_pixel = image[x + offset_x, y + offset_y] |
| 250 | |
| 251 | matrix[base_pixel, offset_pixel] += 1 |
| 252 | matrix_sum = np.sum(matrix) |
| 253 | return matrix / (1 if matrix_sum == 0 else matrix_sum) |
| 254 | |
| 255 | |
| 256 | def haralick_descriptors(matrix: np.ndarray) -> list[float]: |