From 2c675f4176d7d8ad19419deb3f8060821f16634a Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 4 Dec 2021 23:12:03 +0100 Subject: [PATCH] Extract most saturated color as probable lid color --- detect.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/detect.py b/detect.py index 726a580..162452f 100755 --- a/detect.py +++ b/detect.py @@ -7,8 +7,11 @@ import matplotlib.pyplot as plt import cv2 import numpy as np from skimage import io +import colorsys -img = io.imread('https://i.stack.imgur.com/DNM65.png')[:, :, :-1] +img_path = 'IMG_20211204_220355.jpg' + +img = io.imread(img_path)[:, :, :] # Average color average = img.mean(axis=0).mean(axis=0) @@ -36,11 +39,23 @@ dom_patch = np.zeros(shape=img.shape, dtype=np.uint8) for i in range(len(rows) - 1): dom_patch[rows[i]:rows[i + 1], :, :] += np.uint8(palette[indices[i]]) -fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12,6)) -ax0.imshow(avg_patch) -ax0.set_title('Average color') +palette /= 255.0 + +def get_saturation(rgb_color): + return colorsys.rgb_to_hsv(*rgb_color)[1] + +# Lid color is likely the color with the highest saturation +lid_color = max(palette, key=get_saturation) + +fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(12,4)) +ax0.imshow(img) +ax0.set_title('Input') ax0.axis('off') ax1.imshow(dom_patch) ax1.set_title('Dominant colors') ax1.axis('off') -plt.show() +ax2.add_patch(matplotlib.patches.Rectangle((0, 0), 200, 200, color=lid_color)) +ax2.set_title('Probable Lid Color') +ax2.axis('off') + +plt.savefig(img_path + "_result.png")