簡體   English   中英

從包含RGB值的文件中拆分R,G和B值的有效方法(無NumPy)

[英]Efficient way to split R, G and B values from a file containing RGB values (Without NumPy)

我有一個包含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']
GREEN = ['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循環。

您可以避免使用正則表達式

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']

如果您主要對矩陣感興趣,則幾乎可以在一行中完成:

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]

為什么要避免使用兩個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集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM