Advertisement
Korotkodul

Задача 3. Не вижу разницы

Feb 24th, 2025
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def get_dominant_color_info(
  4.     image: np.ndarray[np.uint8],
  5.     threshold: int = 5,
  6. ) -> tuple[np.uint8, float]:
  7.     if threshold < 1:
  8.         raise ValueError
  9.     different_colours, different_colours_cnt = np.unique(image, return_counts=True)
  10.     different_colours_cnt_max =  []
  11.     for  i in range(different_colours.shape[0]):
  12.         mask = abs(different_colours - different_colours[i]) < threshold
  13.         cnt = np.sum(different_colours_cnt[mask])
  14.         different_colours_cnt_max.append(cnt)
  15.     max_id = np.argmax(different_colours_cnt_max)
  16.     the_most_common_color = different_colours[max_id]
  17.     the_most_common_color = np.uint8(the_most_common_color)
  18.     percentage = different_colours_cnt_max[max_id] / (image.shape[0] * image.shape[1]) * 100
  19.     return the_most_common_color, percentage
  20.  
  21. res = get_dominant_color_info(np.array([ [1, 2, 3], [4, 5, 6] ]), 6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement