我使用深度學習演算法來檢測影像中的元素。一旦檢測到這些元素,我會嘗試在此影像中恢復兩種顏色。
這是我處理的影像的示例:
非對比影像
為了更容易,我對比影像以改善顏色這里是一個例子:
對比影像
我的目標是在這張圖片中找到藍色和紅色,正是在這個精確的時刻,我被擋住了。當影像質量很好時,我設法找到顏色,但在其他質量較差的影像上很難獲得好的結果。
知道我想找到的顏色如下:紅色、綠色、藍色、黃色、灰色、棕色、紫色、綠松石色、橙色、粉色
你知道任何可以解決我的問題的影像處理方法或機器學習模型嗎?
更多圖片例如:
好圖 1
好圖2
糟糕的形象 1
糟糕的形象 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
uj5u.com熱心網友回復:
也許 OpenCV 的 inRange() 可能會有所幫助?
import cv2
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_hsvcolorspace = np.array([hue_min, saturation_min, value_min])
upper_hsvcolorspace = np.array([hue_max, saturation_max, value_max])
mask = cv2.inRange(hsv_image, lower_hsvcolorspace, upper_hsvcolorspace)
例如,您可以在此處查找預期的 HSV 值。請注意,OpenCV 中的范圍是不同的:0-179(色調)和 0-255(飽和度,值)。
你能發布更多圖片:好的、壞的和預期的輸出嗎?
uj5u.com熱心網友回復:
將您的影像轉換為 HSV cv2.cvtColor(image, cv2.COLOR_BGR2HSV),而不是創建閾值向量,例如
lower_green = np.array([30, 0, 0])
upper_green = np.array([90, 255, 255])
使用此閾值,您可以過濾不同的顏色,閱讀有關 HSV 的更多資訊
mask = cv2.inRange(hsv_image, lower_green, upper_green)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452764.html
標籤:Python 图像处理 深度学习 计算机视觉 颜色检测
