MCPcopy
hub / github.com/TheAlgorithms/Python / get_distances

Function get_distances

computer_vision/haralick_descriptors.py:352–388  ·  view source on GitHub ↗

Calculate all Euclidean distances between a selected base descriptor and all other Haralick descriptors The resulting comparison is return in decreasing order, showing which descriptor is the most similar to the selected base. Args: descriptors: Haralick descriptors to

(descriptors: np.ndarray, base: int)

Source from the content-addressed store, hash-verified

350
351
352def get_distances(descriptors: np.ndarray, base: int) -> list[tuple[int, float]]:
353 """
354 Calculate all Euclidean distances between a selected base descriptor
355 and all other Haralick descriptors
356 The resulting comparison is return in decreasing order,
357 showing which descriptor is the most similar to the selected base.
358
359 Args:
360 descriptors: Haralick descriptors to compare with base index
361 base: Haralick descriptor index to use as base when calculating respective
362 euclidean distance to other descriptors.
363
364 Returns:
365 Ordered distances between descriptors
366
367 Example:
368 >>> index = 1
369 >>> img = np.array([[[108, 201, 72], [255, 11, 127]],
370 ... [[56, 56, 56], [128, 255, 107]]])
371 >>> gray = grayscale(img)
372 >>> binary = binarize(gray)
373 >>> morphological = opening_filter(binary)
374 >>> get_distances(get_descriptors(
375 ... binary_mask(gray, morphological), (0, 1)),
376 ... index)
377 [(0, 0.0), (1, 0.0), (2, 0.0), (3, 0.0), (4, 0.0), (5, 0.0), \
378(6, 0.0), (7, 0.0), (8, 0.0), (9, 0.0), (10, 0.0), (11, 0.0), (12, 0.0), \
379(13, 0.0), (14, 0.0), (15, 0.0)]
380 """
381 distances = np.array(
382 [euclidean(descriptor, descriptors[base]) for descriptor in descriptors]
383 )
384 # Normalize distances between range [0, 1]
385 normalized_distances: list[float] = normalize_array(distances, 1).tolist()
386 enum_distances = list(enumerate(normalized_distances))
387 enum_distances.sort(key=lambda tup: tup[1], reverse=True)
388 return enum_distances
389
390
391if __name__ == "__main__":

Callers 1

Calls 3

normalize_arrayFunction · 0.85
sortMethod · 0.80
euclideanFunction · 0.70

Tested by

no test coverage detected