【问题标题】:Efficient way to split R, G and B values from a file containing RGB values (Without NumPy)从包含 RGB 值的文件中拆分 R、G 和 B 值的有效方法(没有 NumPy)
【发布时间】:2014-06-23 16:43:42
【问题描述】:

我有一个包含 RGB 值的文件。喜欢,

示例图像 Data.txt 文件

每行包含由空格分隔的三元组(如 255,255,255)。
每个三元组都有三个逗号分隔的整数。这些整数对应于 R ('RED')、G ('GREEN') 和 B ('BLUE') 值。所有整数都小于 256。

255,255,255 250,250,250 254,254,254 250,250,250 
255,255,255 253,253,253 255,255,255 255,255,255 
251,251,251 247,247,247 251,251,251 250,250,250
195,195,195 191,191,191 195,195,195 195,195,195
255,255,255 253,253,253 254,254,254 255,255,255 
255,255,255 254,254,254 239,239,239 240,240,240
238,238,238 254,254,254 255,255,255 255,255,255

处理后的输出应如下所示:
红色 = ['255','250','254','250','255','253','255',............,'254','255','255']
绿色 = ['255','250','254','250','255','253','255',............,'254','255','255']
蓝色 = ['255','250','254','250','255','253','255',............,'254','255','255']
RGB_Nx3_MATRIX = [['255','255','255'],['250','250','250'],['254','254','254'].....['255','255','255']]

我的代码运行良好。

import re

file_object = open('Image Data.txt','r') 

RED_VECTOR = []         #SEQUENTIALLY STORES ALL 'R' VALUES
GREEN_VECTOR = []       #SEQUENTIALLY STORES ALL 'G' VALUES
BLUE_VECTOR = []        #SEQUENTIALLY STORES ALL 'B' VALUES

RGB_Nx3_MATRIX = []     #Nx3 MATRIX i.e. ['R','G','B'] N times

for line in file_object:
    SPACE_split_LIST = line.split()

    for pixel in SPACE_split_LIST:
        RGB = re.findall(r'\,?(\d+)\,?',pixel)
        RED_VECTOR += [RGB[0]]
        GREEN_VECTOR += [RGB[1]]
        BLUE_VECTOR += [RGB[2]]

        RGB_Nx3_MATRIX += [RGB]




#RESULTS

#print RED_VECTOR
#print GREEN_VECTOR
#print BLUE_VECTOR

#print "------------------"

#print RGB_Nx3_MATRIX

我在寻找什么?

我需要一种更好、更有效的方法来做到这一点。我想避免使用两个 for 循环。

【问题讨论】:

标签: python regex python-2.7


【解决方案1】:

你可以避免使用正则表达式

f =open('Image Data.txt','r')                 

R=[]                                 
G=[]                                 
B=[]                                 
for line in f:                       
    for color_set in line.split():       
        r,g,b = color_set.split(',')     
        R+=[r]                       
        G+=[g]                       
        B+=[b]                       

print B

输出

['255', '250', '254', '250', '255', '253', '255', '255', '251', '247', '251', '250', '195', '191', '195', '195', '255', '253', '254', '255', '255', '254', '239', '240', '238', '254', '255', '255']

【讨论】:

    【解决方案2】:

    如果您主要对矩阵感兴趣,您几乎可以在一行中做到这一点:

    with open('Image Data.txt','r') as file_h:
        rgb_matrix = [triple.split(',') for line in file_h for triple in line.strip().split()]
    

    这应该是相当有效的。您还可以通过另一个循环来扩展它以将它们转换为整数。

    with open('Image Data.txt','r') as file_h:
        rgb_matrix = [[int(num) for num in triple.split(',')] for line in file_h for triple in line.strip().split()]
    

    如果你真的需要单独的颜色,你可以很容易地得到它们:

    red = [row[0] for row in rgb_matrix]
    green = [row[1] for row in rgb_matrix]
    blue = [row[2] for row in rgb_matrix]
    

    【讨论】:

      【解决方案3】:

      为什么要避免使用两个 for 循环? For 循环本身并不是低效的。但是,对每一行(例如 re.findall)进行函数调用可能会变得非常低效。

      尤其是在处理大文件或处理像素时,最好坚持使用简单的函数和算术,而不是昂贵的函数调用。您可能想要做的是以下内容:

      for line in file:
          split = line.split(' ')
          for s in split:
              r,g,b = s.split(',')
              r_vector.append(r)
              g_vector.append(g)
              b_vector.append(b.split('\')[0]) <<<<Keep in mind, every line will have a '\n' newline char
      

      编辑:感谢@Ashoka Lella 指出每一行都有多个 rgb 集。

      【讨论】:

      • 以空格分隔的第一列不完全是红色。它的 r,g,b。每行的格式为 r,g,b r,g,b r,g,b
      猜你喜欢
      • 2017-09-13
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2022-12-22
      • 1970-01-01
      相关资源
      最近更新 更多