用于改进颜色检测的图像处理 (python)

原文标题Image processing for improved color detection (python)

我使用深度学习算法来检测图像中的元素。一旦检测到这些元素,我会尝试恢复该图像中的两种颜色。

这是我处理的图像的示例:

非对比图像

为了更容易,我对比图像以改善颜色这里是一个例子:

对比图像

我的目标是在这张图片中找到蓝色和红色,正是在这个精确的时刻,我阻止了。当图像质量很好时,我设法找到颜色,但在其他质量较差的图像上很难找到取得好成绩。

知道我想要找到的颜色如下:红色、绿色、蓝色、黄色、灰色、棕色、紫色、绿松石色、橙色、粉色

你知道任何可以解决我的问题的图像处理方法或机器学习模型吗?

更多图片例如:

好图 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

原文链接:https://stackoverflow.com//questions/71673254/image-processing-for-improved-color-detection-python

回复

我来回复
  • Olli的头像
    Olli 评论

    也许 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(饱和度,值)。

    你能发布更多图片:好的、坏的和预期的输出吗?

    2年前 0条评论
  • Pavel的头像
    Pavel 评论

    将您的图像转换为 HSVcv2.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)
    
    2年前 0条评论