2014-12-22 44 views
11

我有一张图像,看起来相当于这张图片:从弯曲线挑选颜色

this

这是一系列弯曲线条中的一个页面上的圆圈,每一行都是不同的。

from PIL import Image 
im = Image.open("bride.jpg") 

导入后,我想知道是否有一种区分颜色的方法。对于例如,这可能是:

[Purple, Cyan, Purple, Cyan, Purple Cyan...] 

该行没有模式的,并且是几千圈长,这意味着它不能被手动完成。另外请注意,我实际测试的图像质量更高,实际图像可以找到here

+1

这有点不清楚你正在尝试做什么。请显示一些代码?这张图片的来源是什么? –

+0

啊,好吧,我会添加一些代码并尝试扩展,介意给我一分钟。 –

+1

我很好奇:-)你是否能够说出你在做什么 - 如何产生它们以及如何使用它们? –

回答

1

只需创建一行沿着该行的RGB值列表,然后过滤该列表以删除类似相邻的值。

Get pixel's RGB using PIL

#find RGB values 
rgb_values = [] 
for point in line: 
    r, g, b = rgb_im.getpixel(point) 
    rgb_values += [ (r,g,b)] 
#remove similar adjacent values 
for i in range(len(rgb_values)-1): 
    if rgb_values[i] == rgb_values[i+1]: 
     rgb_values.pop(i) 
     i -= 1 
if len(rgb_values) > 1: 
    if rgb_values[-1] == rgb_values[-2]: 
     rgb_values.pop(-1) 
1

我认为你的主要问题是要找到从曲线到其他的​​一端遵循像素的良好路径。一旦你决定了这个路径,你可以按照它来检查像素颜色。

现在,我建议你自己输入路径,通过指定一组路标。以下是您示例图的一些代码。我把第一个点放在左边的紫色光盘上,第二个放在角度上,最后一个放在右下方的紫色光盘上。

from PIL import Image 
from numpy import array, arange 

im = Image.open("aqoGkjs.png") 
rgb_im = im.convert('RGB') 

def get_color_at(p): # color function as mattsap suggested 
    r, g, b = rgb_im.getpixel(tuple(p)) 
    if r > g and r > b: 
     return 'Purple' 
    elif r < 10 and g < 10: 
     return 'Blue' 
    return 'Cyan' 

colors = [] 
via_points = [array([25, 65]), array([185, 44]), array([240, 210])] 

for i in xrange(len(via_points) - 1): 
    for x in arange(0, 1, 0.01): 
     p = x * via_points[i] + (1 - x) * via_points[i + 1] # linear interpolation 
     cur_color = get_color_at(p) 
     if cur_color == 'Blue': # ignore borders 
      continue 
     if not colors or cur_color != colors[-1]: 
      colors.append(cur_color) 

print colors 

它打印:

['Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple', 'Cyan', 'Purple'] 

你的大图像,你可以输入所有的航点由手。或者尝试制作一段聪明的代码来自动找到它们)