Created
October 24, 2021 10:00
-
-
Save aashishrbhandari/3fc8f76cfb995341d1c0df7728f0f625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Python3 | |
How to Install | |
apt-get update && apt-get install -y python3-opencv | |
pip3 install --upgrade pip # Important | |
pip3 install sklearn matplotlib numpy opencv-python scikit-image | |
""" | |
from sklearn.cluster import KMeans | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import cv2 | |
from collections import Counter | |
from skimage.color import rgb2lab, deltaE_cie76 | |
import os | |
''' Comment: ''' | |
def RGB2HEX(color): | |
return "#{:02x}{:02x}{:02x}".format(int(color[0]), int(color[1]), int(color[2])) | |
''' Comment: Get Converted Image''' | |
def get_image(image_path): | |
image = cv2.imread(image_path) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
return image | |
''' Get Image Colors ''' | |
def get_colors(image, number_of_colors=4, show_chart): | |
modified_image = cv2.resize(image, (600, 400), interpolation = cv2.INTER_AREA) | |
modified_image = modified_image.reshape(modified_image.shape[0]*modified_image.shape[1], 3) | |
clf = KMeans(n_clusters = number_of_colors) | |
labels = clf.fit_predict(modified_image) | |
counts = Counter(labels) | |
# sort to ensure correct color percentage | |
counts = dict(sorted(counts.items())) | |
center_colors = clf.cluster_centers_ | |
# We get ordered colors by iterating through the keys | |
ordered_colors = [center_colors[i] for i in counts.keys()] | |
hex_colors = [RGB2HEX(ordered_colors[i]) for i in counts.keys()] | |
rgb_colors = [ordered_colors[i] for i in counts.keys()] | |
if (show_chart): | |
plt.figure(figsize = (8, 6)) | |
plt.pie(counts.values(), labels = hex_colors, colors = hex_colors) | |
return rgb_colors | |
''' ''' | |
def match_image_by_color(image, color, threshold = 60, number_of_colors = 10): | |
image_colors = get_colors(image, number_of_colors, False) | |
selected_color = rgb2lab(np.uint8(np.asarray([[color]]))) | |
select_image = False | |
for i in range(number_of_colors): | |
curr_color = rgb2lab(np.uint8(np.asarray([[image_colors[i]]]))) | |
diff = deltaE_cie76(selected_color, curr_color) | |
if (diff < threshold): | |
select_image = True | |
return select_image | |
def show_selected_images(images, color, threshold, colors_to_match): | |
index = 1 | |
for i in range(len(images)): | |
selected = match_image_by_color(images[i], color, | |
threshold, | |
colors_to_match) | |
if (selected): | |
plt.subplot(1, 5, index) | |
plt.imshow(images[i]) | |
index += 1 | |
def recognize_color(R,G,B): | |
minimum = 10000 | |
for i in range(len(csv)): | |
d = abs(R- int(csv.loc[i,"R"])) + abs(G- int(csv.loc[i,"G"]))+ abs(B- int(csv.loc[i,"B"])) | |
if(d <= minimum): | |
minimum = d | |
cname = csv.loc[i,"color_name"] | |
return cname | |
import pandas as pd | |
import cv2 | |
#img = cv2.imread("ball1_green1.jpg") | |
#index=["color", "color_name", "hex", "R", "G", "B"] | |
#csv = pd.read_csv('colors.csv', names=index, header=None) | |
rgb_colors = {0: "RED", 1: "GREEN", 2: "BLUE"} | |
def get_image_color(image_file): | |
img = cv2.imread(image_file) | |
blue_color, green_color, red_color = (img[300, 300]) | |
index = max_index(red_color, green_color, blue_color) | |
return rgb_colors[index] | |
def max_index(a, b, c): | |
largest = 0 | |
if (b >= c): | |
largest = 1 | |
else: | |
largest = 2 | |
return largest | |
## Provide Image File Path | |
img_file1 = "ball1_green1.jpg" | |
## Check & Result | |
get_image_color(img_file1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment