Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def get_dominant_color_info(
- image: np.ndarray[np.uint8],
- threshold: int = 5,
- ) -> tuple[np.uint8, float]:
- if threshold < 1:
- raise ValueError
- different_colours, different_colours_cnt = np.unique(image, return_counts=True)
- different_colours_cnt_max = []
- for i in range(different_colours.shape[0]):
- mask = abs(different_colours - different_colours[i]) < threshold
- cnt = np.sum(different_colours_cnt[mask])
- different_colours_cnt_max.append(cnt)
- max_id = np.argmax(different_colours_cnt_max)
- the_most_common_color = different_colours[max_id]
- the_most_common_color = np.uint8(the_most_common_color)
- percentage = different_colours_cnt_max[max_id] / (image.shape[0] * image.shape[1]) * 100
- return the_most_common_color, percentage
- res = get_dominant_color_info(np.array([ [1, 2, 3], [4, 5, 6] ]), 6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement