python找色,Python-查找相似的颜色,最好的方法

I've made a function to find a color within a image, and return x, y. Now I need to add a new function, where I can find a color with a given tolerence. Should be easy?

Code to find color in image, and return x, y:

def FindColorIn(r,g,b, xmin, xmax, ymin, ymax):

image = ImageGrab.grab()

for x in range(xmin, xmax):

for y in range(ymin,ymax):

px = image.getpixel((x, y))

if px[0] == r and px[1] == g and px[2] == b:

return x, y

def FindColor(r,g,b):

image = ImageGrab.grab()

size = image.size

pos = FindColorIn(r,g,b, 1, size[0], 1, size[1])

return pos

Outcome:

Taken from the answers the normal methods of comparing two colors are in Euclidean distance, or Chebyshev distance.

I decided to mostly use (squared) euclidean distance, and multiple different color-spaces. LAB, deltaE (LCH), XYZ, HSL, and RGB. In my code, most color-spaces use squared euclidean distance to compute the difference.

For example with LAB, RGB and XYZ a simple squared euc. distance does the trick:

if ((X-X1)^2 + (Y-Y1)^2 + (Z-Z1)^2) <= (Tol^2) then

...

LCH, and HSL is a little more complicated as both have a cylindrical hue, but some piece of math solves that, then it's on to using squared eucl. here as well.

In most these cases I've added "separate parameters" for tolerance for each channel (using 1 global tolerance, and alternative "modifiers" HueTol := Tolerance * hueMod or LightTol := Tolerance * LightMod).

It seems like colorspaces built on top of XYZ (LAB, LCH) does perform best in many of my scenarios. Tho HSL yields very good results in some cases, and it's much cheaper to convert to from RGB, RGB is also great tho, and fills most of my needs.

解决方案

Computing distances between RGB colours, in a way that's meaningful to the eye, isn't as easy a just taking the Euclidian distance between the two RGB vectors.

There is an interesting article about this here: http://www.compuphase.com/cmetric.htm

The example implementation in C is this:

typedef struct {

unsigned char r, g, b;

} RGB;

double ColourDistance(RGB e1, RGB e2)

{

long rmean = ( (long)e1.r + (long)e2.r ) / 2;

long r = (long)e1.r - (long)e2.r;

long g = (long)e1.g - (long)e2.g;

long b = (long)e1.b - (long)e2.b;

return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));

}

It shouldn't be too difficult to port to Python.

EDIT:

Alternatively, as suggested in this answer, you could use HLS and HSV. The colorsys module seems to have functions to make the conversion from RGB. Its documentation also links to these pages, which are worth reading to understand why RGB Euclidian distance doesn't really work:

EDIT 2:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值