Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何将RGB或十六进制颜色代码分组为更大的颜色组?_Image Processing_Computer Vision_Classification - Fatal编程技术网

Image processing 如何将RGB或十六进制颜色代码分组为更大的颜色组?

Image processing 如何将RGB或十六进制颜色代码分组为更大的颜色组?,image-processing,computer-vision,classification,Image Processing,Computer Vision,Classification,我正在分析大量的图像并提取主色代码 我想把它们分成一些通用的颜色名称,比如绿色、深绿色、浅绿色、蓝色、深蓝色、浅蓝色等等 我正在寻找一种语言不可知的方法,以便自己实现一些东西,如果有我可以研究的例子来实现这一点,我将不胜感激。在机器学习领域,你想做的是,在这种方法中,目标是为每个观察指定一个类(颜色)的标签(图片)。 为此,必须预定义类。假设这些是我们要分配给图像的颜色: 要确定图像的主色,必须计算其每个像素与表格中所有颜色之间的距离。请注意,此距离是在RGB颜色空间中计算的。要计算图像的第i

我正在分析大量的图像并提取主色代码

我想把它们分成一些通用的颜色名称,比如绿色、深绿色、浅绿色、蓝色、深蓝色、浅蓝色等等


我正在寻找一种语言不可知的方法,以便自己实现一些东西,如果有我可以研究的例子来实现这一点,我将不胜感激。

在机器学习领域,你想做的是,在这种方法中,目标是为每个观察指定一个类(颜色)的标签(图片)。 为此,必须预定义类。假设这些是我们要分配给图像的颜色:

要确定图像的主色,必须计算其每个像素与表格中所有颜色之间的距离。请注意,此距离是在RGB颜色空间中计算的。要计算图像的第ij个像素与表格的第k个颜色之间的距离,可以使用以下等式:

d_ijk = sqrt((r_ij-r_k)^2+(g_ij-g_k)^2+(b_ij-b_k)^2)
在下一步中,对于每个像素,选择表中最接近的颜色。这是用于使用压缩图像的概念(除了此处调色板对于所有图像都相同,并且不会对每个像素进行计算,以最小化原始图像和索引图像之间的差异).现在,正如@Jairor所指出的,我们可以得到图像的直方图(不要与RGB直方图或强度直方图混淆),并确定重复性最大的颜色。
为了显示这些步骤的结果,我使用了我的这件艺术品的随机裁剪:


这是索引前后图像的外观(左:原始,右:索引): 这些是最重复的颜色(左:索引,右:主色):

但是既然你说图像的数量很大,你应该知道这些计算相对来说比较耗时。但好消息是,有一些方法可以提高性能。例如,不使用(上面的公式),您可以使用或距离。您也可以只计算一小部分像素的距离,而不是计算图像中所有像素的距离。为此,您可以先将图像缩放到更小的大小(例如,32 x 32)并对缩小后的图像的像素进行计算。如果决定调整图像大小,请不要费心使用双线性或双三次插值,这不值得额外计算。相反,请选择,它实际上会对原始图像执行采样。

虽然上述更改将大大提高计算速度,但没有什么好东西是免费的。这是性能与准确性的折衷。例如,在前两张图片中,我们看到最初被识别为橙色(代码20)的图像在调整大小后被识别为粉红色(代码26)。

确定算法参数(距离测量、缩小图像大小和缩放算法),您必须首先以尽可能高的精度对大量图像执行分类操作,并将结果作为基本事实。然后,通过多次实验,获得不会使分类误差超过最大容许值的参数组合。

@saastn的奇妙答案假设您要对图像进行排序的一组预定义颜色。如果只想将图像分类为某组X等距颜色(la直方图)中的一种颜色,则实现更简单

总而言之,将图像中每个像素的颜色四舍五入到某组等距颜色栏中最接近的颜色。这会将颜色精度降低到所需的任何颜色。然后计算图像中的所有颜色,并选择最常见的颜色作为该图像的分类

以下是我在Python中的实现:

import cv2
import numpy as np

#Set this to the number of colors that you want to classify the images to
number_of_colors = 8

#Verify that the number of colors chosen is between the minimum possible and maximum possible for an RGB image.
assert 8 <= number_of_colors <= 16777216

#Get the cube root of the number of colors to determine how many bins to split each channel into.
number_of_values_per_channel = number_of_colors ** ( 1 / 3 )

#We will divide each pixel by its maximum value divided by the number of bins we want to divide the values into (minus one for the zero bin).
divisor = 255 / (number_of_values_per_channel - 1)

#load the image and convert it to float32 for greater precision. cv2 loads the image in BGR (as opposed to RGB) format.
image = cv2.imread("image.png", cv2.IMREAD_COLOR).astype(np.float32)

#Divide each pixel by the divisor defined above, round to the nearest bin, then convert float32 back to uint8.
image = np.round(image / divisor).astype(np.uint8)

#Flatten the columns and rows into just one column per channel so that it will be easier to compare the columns across the channels.
image = image.reshape(-1, image.shape[2])

#Find and count matching rows (pixels), where each row consists of three values spread across three channels (Blue column, Red column, Green column).
uniques = np.unique(image, axis=0, return_counts=True)

#The first of the two arrays returned by np.unique is an array compromising all of the unique colors.
colors = uniques[0]

#The second of the two arrays returend by np.unique is an array compromising the counts of all of the unique colors.
color_counts = uniques[1]

#Get the index of the color with the greatest frequency
most_common_color_index = np.argmax(color_counts)

#Get the color that was the most common
most_common_color = colors[most_common_color_index]

#Multiply the channel values by the divisor to return the values to a range between 0 and 255
most_common_color = most_common_color * divisor

#If you want to name each color, you could also provide a list sorted from lowest to highest BGR values comprising of
#the name of each possible color, and then use most_common_color_index to retrieve the name.
print(most_common_color)
导入cv2
将numpy作为np导入
#将此设置为要对图像进行分类的颜色数
颜色的数量=8
#验证所选颜色数是否介于RGB图像的最小可能颜色数和最大可能颜色数之间。

断言8我建议您在查看每个频道后,检查RGB颜色空间中图像的直方图,您可以得出图像颜色的结论。我假设您使用
image.open('image.png')。convert('RGB')。getcolors()
要获得实际的颜色数?颜色数是要将图像分类到的颜色类别数。这是一个由您决定的数字。例如,如果要将图像分类到以下8个类别:[“黑色”、“红色”、“绿色”、“蓝色”、“青色”、“紫色”、“黄色”、“白色”],您将输入8.PIL的getcolors()用于计算直方图的函数仅适用于黑白图像,它不允许您设置存储箱的数量。CV2的直方图函数允许您设置存储箱的数量,但不允许您使用彩色图像。使用CV2,您必须拆分通道,然后合并生成的直方图,这似乎更难实现比我所做的更重要。某些库中应该有一个函数可以实现我所做的,但我不知道。我还担心过多地依赖库会使它难以用另一种语言实现。我明白了,所以可以使用颜色量化()减少图像信息,然后使用欧几里德距离将颜色与预定义颜色匹配list@GeorgeKaf如果“减少图像信息”只是指调整图像大小,那么是的。正如您链接的页面中所述,“大多数标准技术将颜色量化视为三维空间中聚类点的问题”。我建议将其视为一个分类问题。我们不需要知道“RGB空间”中什么颜色最能描述图像,相反,我们感兴趣的是