@@ -167,39 +167,29 @@ def extract_properties_and_object_masks(
167
167
168
168
def find_dominant_color (
169
169
image : np .ndarray , black_threshold : int = 50
170
- ) -> Tuple [Union [ int , str ], Union [ int , str ], Union [ int , str ] ]:
170
+ ) -> Tuple [int , int , int ]:
171
171
"""Determines the dominant color in a given image.
172
172
173
- The function performs the following steps:
174
- Filters out black or near-black pixels based on a threshold.
175
- Uses k-means clustering to identify the dominant color among the remaining
176
- pixels.
177
-
178
173
Args:
179
174
image: An array representation of the image.
180
- black_threshold: pixel value of black color
181
-
182
- shape is (height, width, 3) for RGB channels.
183
175
black_threshold: The intensity threshold below which pixels
184
- are considered 'black' or near-black. Default is 50 .
176
+ are considered 'black' or near-black.
185
177
186
178
Returns:
187
- The dominant RGB color in the format (R, G, B). If no non-black
188
- pixels are found, returns ('Na', 'Na', 'Na').
179
+ The dominant RGB color in the format (R, G, B).
189
180
"""
190
181
pixels = image .reshape (- 1 , 3 )
191
182
192
183
# Filter out black pixels based on the threshold
193
184
non_black_pixels = pixels [(pixels > black_threshold ).any (axis = 1 )]
194
185
195
- if non_black_pixels .size != 0 :
186
+ if non_black_pixels .size :
196
187
kmeans = sklearn_cluster .KMeans (
197
188
n_clusters = 1 , n_init = 10 , random_state = 0
198
189
).fit (non_black_pixels )
199
190
dominant_color = kmeans .cluster_centers_ [0 ].astype (int )
200
-
201
191
else :
202
- dominant_color = [ 'Na' , 'Na' , 'Na' ]
192
+ dominant_color = np . array ([ 0 , 0 , 0 ], dtype = int )
203
193
return tuple (dominant_color )
204
194
205
195
0 commit comments