将在OpenCV中找到的像素颜色映射到预定的颜色列表 [英] Map a pixel color found in OpenCV to a pre-determined list of colors

查看:90
本文介绍了将在OpenCV中找到的像素颜色映射到预定的颜色列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一种情况下,我已经从图像中获得一种或多种颜色,但是现在我需要确定它与我现有的颜色选项中最接近的一种.

例如,我可能选择红色(255,0,0),绿色(0,255,0)和蓝色(0,0,255)作为我的三个选择,但图像可能包含橙色(255,165,0).

然后我需要一种方法来确定我应该选择这三个值中的哪一个作为输出颜色来替换橙色.

我考虑过的一种方法是从这三个值中测量范围,并查看哪一个是最小的&值.选择该颜色.

示例:

橙色->红色

abs(255 - 255) = 0, abs(165 - 0) = 165, abs(0 - 0) = 0 
0 + 165 + 0 = 165

橙色->绿色

abs(255 - 0) = 255, abs(165 - 255) = 90, abs(0 - 0) = 0
255 + 90 + 0 = 345

橙色->蓝色

abs(255 - 0) = 255, abs(165 - 0) = 165, abs(0 - 255) = 255 
255 + 165 + 255 = 675 

在这种方法下,我会选择红色.

但是,我不确定这是否是最好的,甚至是特别有效的,所以我想知道是否存在更准确的&信息.可以更好地缩放以增加颜色的调色板.

更新此处链接的归约答案无济于事,因为它可以全面减少事物.我需要能够将多种颜色链接到几个特定的​​选项.

解决方案

我认为您应该代表并比较不同颜色空间中的颜色.我建议代表人类色彩感知的空间.因此,L * a * b色彩空间将是最好的.

https://en.wikipedia.org/wiki/Lab_color_space/

该坐标空间中的色距由 delta e 值表示.您可以在以下找到 delta e 的不同标准:

https://en.wikipedia.org/wiki/Color_difference#CIELAB_Delta_E. 2A/

要更改色彩空间,必须使用cv::cvtColor()方法.单个像素的颜色转换如下所述:

https://stackoverflow.com/a/35737319/8682088/

计算L * a * b空间中的像素坐标后,您可以轻松计算 delta e 并将颜色与任何参考进行比较,然后选择误差最小的颜色.

I have a scenario where I have obtained one or more colors from an image, but now I need to determine which one of my existing color options it is closest to.

For example, I may have red(255,0,0), green(0,255,0) and blue(0,0,255) as my three choices, but the image may contain orange(255,165,0).

What I need then is a way to determine which one of those three values I should choose as my output color to replace orange.

One approach I have considered is to measure the range from those three values and see which one is the smallest & select that color.

Example:

orange -> red

abs(255 - 255) = 0, abs(165 - 0) = 165, abs(0 - 0) = 0 
0 + 165 + 0 = 165

orange -> green

abs(255 - 0) = 255, abs(165 - 255) = 90, abs(0 - 0) = 0
255 + 90 + 0 = 345

orange -> blue

abs(255 - 0) = 255, abs(165 - 0) = 165, abs(0 - 255) = 255 
255 + 165 + 255 = 675 

Under this approach, I would pick red.

However, I am not sure if this is the best, or even a particularly valid, one so was wondering if there is something out there that is more accurate & would scale better to an increased color pallete.

Update The reduction answer linked in here does not help as it reduces things across the board. I need the ability to link a broad range of colors to several specific options.

解决方案

I think you should represent and compare colors in different color space. I suggest space, that represent human color perception. Therefore L*a*b color space will be the best.

https://en.wikipedia.org/wiki/Lab_color_space/

Color distances in that coordinate space are represented by delta e value. You could find different standards for delta e below:

https://en.wikipedia.org/wiki/Color_difference#CIELAB_Delta_E.2A/

In order to change color space you have to use cv::cvtColor() method. Color conversion for single pixel is described below:

https://stackoverflow.com/a/35737319/8682088/

After calculating pixel coordinates in L*a*b space, you could easily calculate delta e and compare colors with any reference and pick the one with the smallest error.

这篇关于将在OpenCV中找到的像素颜色映射到预定的颜色列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆