MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / haralick_descriptors

Function haralick_descriptors

computer_vision/haralick_descriptors.py:256–310  ·  view source on GitHub ↗

Calculates all 8 Haralick descriptors based on co-occurrence input matrix. All descriptors are as follows: Maximum probability, Inverse Difference, Homogeneity, Entropy, Energy, Dissimilarity, Contrast and Correlation Args: matrix: Co-occurrence matrix to use as base for cal

(matrix: np.ndarray)

Source from the content-addressed store, hash-verified

254
255
256def haralick_descriptors(matrix: np.ndarray) -> list[float]:
257 """Calculates all 8 Haralick descriptors based on co-occurrence input matrix.
258 All descriptors are as follows:
259 Maximum probability, Inverse Difference, Homogeneity, Entropy,
260 Energy, Dissimilarity, Contrast and Correlation
261
262 Args:
263 matrix: Co-occurrence matrix to use as base for calculating descriptors.
264
265 Returns:
266 Reverse ordered list of resulting descriptors
267
268 Example:
269 >>> img = np.array([[[108, 201, 72], [255, 11, 127]],
270 ... [[56, 56, 56], [128, 255, 107]]])
271 >>> gray = grayscale(img)
272 >>> binary = binarize(gray)
273 >>> morphological = opening_filter(binary)
274 >>> mask_1 = binary_mask(gray, morphological)[0]
275 >>> concurrency = matrix_concurrency(mask_1, (0, 1))
276 >>> [float(f) for f in haralick_descriptors(concurrency)]
277 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
278 """
279 # Function np.indices could be used for bigger input types,
280 # but np.ogrid works just fine
281 i, j = np.ogrid[0 : matrix.shape[0], 0 : matrix.shape[1]] # np.indices()
282
283 # Pre-calculate frequent multiplication and subtraction
284 prod = np.multiply(i, j)
285 sub = np.subtract(i, j)
286
287 # Calculate numerical value of Maximum Probability
288 maximum_prob = np.max(matrix)
289 # Using the definition for each descriptor individually to calculate its matrix
290 correlation = prod * matrix
291 energy = np.power(matrix, 2)
292 contrast = matrix * np.power(sub, 2)
293
294 dissimilarity = matrix * np.abs(sub)
295 inverse_difference = matrix / (1 + np.abs(sub))
296 homogeneity = matrix / (1 + np.power(sub, 2))
297 entropy = -(matrix[matrix > 0] * np.log(matrix[matrix > 0]))
298
299 # Sum values for descriptors ranging from the first one to the last,
300 # as all are their respective origin matrix and not the resulting value yet.
301 return [
302 maximum_prob,
303 correlation.sum(),
304 energy.sum(),
305 contrast.sum(),
306 dissimilarity.sum(),
307 inverse_difference.sum(),
308 homogeneity.sum(),
309 entropy.sum(),
310 ]
311
312
313def get_descriptors(

Callers 1

get_descriptorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected