2015-06-02 20 views
-1

我有一個矩形數據與txt文件中的圖像相關聯。
每一行是用於差異圖像。
第一列是圖像編號。
用Python從txt文件加載矩形數據?

8 17 30 70 80 
9 49 25 72 83 
10 13 21 75 82 74 25 16 21 

每一行都是由下式表示的矩形:與圖像相關聯
img_number lefttopcorner.Xcoord lefttopcorner.Ycoord width height

這些數據是空格分開的。 第三行顯示此圖像有兩個矩形,但可能有很多。

同一行上的矩形是用製表符分隔的。
所以在同一條線上兩個矩形的書面例子是這樣的: img_num<space>lefttopcorner.X<space>lefttopcorner.Y<space>width<space>height<tab>...
我怎麼會這些矩形加載到蟒蛇瓦爾,把他們在一些收集結構。
也許有像元組的平行數組或rectangles?上午尋找最簡單的實施。

+0

你能解釋一下行如何表示矩形? – tytk

+0

編輯謝謝。錯過了。 – jsky

+0

謝謝。所以這些行的格式爲「X Y dX dY」......第5個數字呢? – tytk

回答

3

最簡單的實施將與行號的dictionary關鍵和[[x,y,w,h]]成爲該鍵的值,在與製表符分隔同一行的多個矩形的情況下,你會得到作爲[[x1,y1,w1,h1], [x2,y2,w2,h2]]關鍵。

rectangles = {} 
with open("sample.txt", "r") as rects: 
    for rect in rects: 
     rectangles[int(rect.split()[0])] = [map(int, rect.split()[1:][i:i+4]) for i in range(0, len(rect.split()[1:]), 4)] 

    print rectangles 

輸出:

{8: [[17, 30, 70, 80]], 9: [[49, 25, 72, 83]], 10: [[13, 21, 75, 82], [74, 25, 16, 21]]} 

爲了取回rectangles字典中的相關數據,你可以使用:

row_number = 8 
for rect in rectangles[8]: #Accessing a specific row 
    print rect, #Will print multiple rectangles if present. 

或者檢索所有的數據:

for key in rectangles: 
    print rectangles[key] 
+0

如何迭代特定鍵的矩形? – jsky

+0

這是因爲,你使用'Imagenum'是一個整數來訪問值,但'rectangles'中的鍵是字符串,我修復了代碼以將數據保存爲'int'。 – ZdaR

0

我總是首先想到我的數據模型。在這裏你可能有一個字典,其中圖像數字爲關鍵字,矩形列表爲值。每個矩形可以是一個Rectangle對象(供您實現)。

您也可以在塊分割你的解析,開始與各行的工作:

image_number, rects = line[0], line[1:] 

然後分析你rects,在另一個功能更好,得到rects作爲參數,通過4提取值4,給他們到您的Rectangle對象的構造函數。最後你的矩形對象必須以正確的順序將4個值放入4個命名成員中。

是的,你必須實現Rectangle及其ctor。你可以選擇使用4個整數的元組而不是一個Rectangle類,這取決於你。

存儲器的另一種想法可能是擁有一個Image類,在其構造函數中使用rects並保存矩形列表。所以你的字典存儲image_number作爲鍵和圖像作爲價值。

這樣,您可以實現Image.draw(),可以訪問self.rectangles。

-2
with read("<filename>.txt", "r"): 
    content = f.read() # may need to do something smarter is the file is big; this reads everything in memory 
content = content.replace("\t", "\n") 
lines = content.split("\n") 
for line in lines: 
    rect = map(int, line.split()) 
+0

我不認爲這是有效的,每行當前以「圖像編號」開頭,然後矩形座標爲4組。分割線條將矩形與原始圖像編號分隔開。 – Marius

+0

從開頭文章:「每一行代表的矩形爲: img_number lefttopcorner.Xcoord lefttopcorner.Ycoord width height」。如果你在'\ n'上分割,這些字段將保持在一起。 '\ n'不在圖片的後面,而是在前面。 –

+0

由於圖像編號只出現在行首(第一個矩形之前),所以當行上有兩個或更多個矩形時,您會丟失圖像編號。這個問題並不是非常明確,示例數據格式不正確。 – Marius

-2

我會去與元組列表字典。

images = { 
    8 : [(17, 30, 70, 80)], 
    9 : [(49, 25, 72, 83)], 
    10 : [(13, 21, 75, 82), (74, 25, 16, 21)] 
} 

print "Image 9 => Width={} Height={}\n".format(images[9][0][2], images[9][0][3]) 
0
with open('path/to/file') as infile: 
    for line in infile: 
     data = [int(i) for i in line.split()] 
     if len(data) == 5: 
      imageNum = data[0] 
      data.pop(0) 
     x,y, width,height = data 
     print("Rectangle", imageNum, "starts at coordinates (", x,y, ") and has width", width, "and has height", height) 
2

class Rectangle和使用dictionary爲包含如下它。

class Rectangle: 
    def __init__(self, number, x, y, width, height): 
     self.img_number = number 
     self.x = x 
     self.y = y 
     self.width = width 
     self.height = height 
    def __str__(self): 
     return "IMG_NUMBER {0}; lefttopcorner.X {1}; lefttopcorner.Y {2}; width {3}; height {4}".format(self.img_number, 
                   self.x, self.y, 
                   self.width, self.y) 

rectangles = {} 
with open("1.txt") as f: 
    for data in f.readlines(): 

     data = data.split()[:5] #get first rectangle if lines wrong like 10 13 21 75 82 74 25 16 21 22 
           #It simple way, but good for concept demonstration 
     rectangle = Rectangle(data[0], data[1], data[2], data[3], data[4]) 
     rectangles[data[0]] = rectangle #Add rectangle to container 
for i in rectangles: 
    print i, ":", rectangles[i] 

測試:

9 : IMG_NUMBER 9; lefttopcorner.X 49; lefttopcorner.Y 25; width 72; height 25 
8 : IMG_NUMBER 8; lefttopcorner.X 17; lefttopcorner.Y 30; width 70; height 30 
10 : IMG_NUMBER 10; lefttopcorner.X 13; lefttopcorner.Y 21; width 75; height 21 
+0

如何在這種數據結構中訪問行號爲8的數據,使用以行爲鍵和矩形作爲值的字典,不是字典列表,不是很有幫助 – ZdaR

+0

'rectangles [number]' –

+0

但是我們會要記住某個地方,如果數據很大,那麼'rectangle [0]'會爲我們提供第8行的數據,那麼如何確定存儲指定行號數據的索引? – ZdaR