我使用深度学习算法来检测图像中的元素。
一旦这些元素被检测到,我会尝试恢复这张图片中的两种颜色。
以下是我处理的图像示例:
Non-contrasted image
为了更方便,我对比了图像以改善颜色,以下是一个例子:
Contrasted image
我的目标是在这张图片中找到蓝色和红色,正是在这个时候我阻止了它。
当图像质量好时,我设法找到颜色,但在其他质量不太好的图像上,很难获得好的结果。
我想知道以下几种颜色:
红色、绿色、蓝色、黄色、灰色、棕色、紫色、绿松石色、橙色、粉色
你知道有什么图像处理方法或机器学习模型可以解决我的问题吗?
更多图片例如:
Good image 1
Good image 2
Bad image 1
Bad image 2
我使用的代码是:
import cv2
import copy
from sklearn import multioutput
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
from collections import Counter
from skimage.color import rgb2lab, deltaE_cie76
import os
from PIL import Image, ImageEnhance
class ImageColorDetection(object):
origineFrame : list = []
imageFrame : list = []
hsvFrame : list = []
colorList : dict = {}
def __init__(self, array=None, path=None, rotated=0):
self.colorList = {}
if path is not None:
self.origineFrame = Image.open(path).convert('RGB').rotate(rotated)
im_output = Image.open(path).convert('RGB').rotate(rotated)
elif array is not None:
self.origineFrame = Image.fromarray(array).convert('RGB').rotate(rotated)
im_output = Image.fromarray(array).convert('RGB').rotate(rotated)
else:
raise Exception('Aucune image n\'est renseigner dans le constructeur')
#im_output = im_output.filter(ImageFilter.BLUR)
#im_output = im_output.filter(ImageFilter.EDGE_ENHANCE_MORE)
#im_output = ImageOps.autocontrast(im_output, cutoff = 5, ignore = 5)
enhancer = ImageEnhance.Color(im_output)
im_output = enhancer.enhance(3)
enhancer = ImageEnhance.Contrast(im_output)
im_output = enhancer.enhance(0.9)
enhancer = ImageEnhance.Sharpness(im_output)
im_output = enhancer.enhance(2)
enhancer = ImageEnhance.Brightness(im_output)
im_output = enhancer.enhance(1.6)
im_output = np.array(im_output)
self.imageFrame = cv2.cvtColor(im_output, cv2.COLOR_RGB2BGR)
self.hsvFrame = cv2.cvtColor(self.imageFrame, cv2.COLOR_BGR2HSV)
def findColor(self, color_rgb, color_title, color_upper, color_lower):
kernal = np.ones((5, 5), "uint8")
color_mask = cv2.inRange(self.hsvFrame, color_lower, color_upper)
color_mask = cv2.dilate(color_mask, kernal)
res_red = cv2.bitwise_and(self.imageFrame, self.imageFrame,
mask = color_mask)
current_area = 0
x, y, w, h, (r,g,b) = 0, 0, 0, 0, color_rgb
# Creating contour to track blue color
im, contours, hierarchy = cv2.findContours(color_mask,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if(area > 1000 and current_area < area):
x, y, w, h = cv2.boundingRect(contour)
self.colorList[color_title] = x, y, w, h, color_rgb
current_area = area
return color_title in self.colorList.keys()
def ShowImage(self):
tmp_img = np.asarray(copy.copy(self.origineFrame))
for color in self.colorList:
cv2.rectangle(
tmp_img,
(self.colorList[color][0], self.colorList[color][1]),
((self.colorList[color][0] + self.colorList[color][2]), (self.colorList[color][1] + self.colorList[color][3])),
self.colorList[color][4], 2)
cv2.putText(
tmp_img,
color,
(self.colorList[color][0], self.colorList[color][1]),
cv2.FONT_HERSHEY_SIMPLEX,
1.0,
self.colorList[color][4])
#plt.imshow(tmp_img, multioutput=True)
return tmp_img
def ShowImageContrast(self):
tmp_img = copy.copy(self.imageFrame)
tmp_img = cv2.cvtColor(tmp_img, cv2.COLOR_BGR2RGB)
for color in self.colorList:
cv2.rectangle(
tmp_img,
(self.colorList[color][0], self.colorList[color][1]),
((self.colorList[color][0] + self.colorList[color][2]), (self.colorList[color][1] + self.colorList[color][3])),
self.colorList[color][4], 3)
cv2.putText(
tmp_img,
color,
(self.colorList[color][0], self.colorList[color][1]),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
self.colorList[color][4])
#plt.imshow(tmp_img, multioutput=True)
return tmp_img
def RGB2HEX(self, color):
return "#{:02x}{:02x}{:02x}".format(int(color[0]), int(color[1]), int(color[2]))
def get_colors(self, contrasted, number_of_colors, show_chart):
if contrasted:
modified_image = cv2.resize(np.asarray(self.imageFrame), (600, 400), interpolation = cv2.INTER_AREA)
else:
modified_image = cv2.resize(np.asarray(self.origineFrame), (600, 400), interpolation = cv2.INTER_AREA)
#modified_image = cv2.resize(np.asarray(self.origineFrame), (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 = [self.RGB2HEX(ordered_colors[i]) for i in counts.keys()]
rgb_colors = [ordered_colors[i] for i in counts.keys()]
print("Nombre de couleur : ", len(hex_colors))
if (show_chart):
plt.figure(figsize = (8, 6))
plt.pie(counts.values(), labels = hex_colors, colors = hex_colors)
return counts, hex_colors, rgb_colors
尝试使用HSV:
有了HSV,我的问题依然存在,我找到了一个教程,设置了我试图解决的问题,但它对我试图处理的图像不起作用,你对这个主题有任何知识(课程、youtube视频、文章)吗?
Voici l’article en question:
https://towardsdatascience.com/color-identification-in-images-machine-learning-application-b26e770c4c71