如何从列表列表中识别出现的坐标 (x,y) 并将其等离子化到数据框中
How to identify from a list of list which are the coordinates (x,y) that appear and plasm it into a data frame
我有一个列表列表,其中包含不同图像的不同坐标。我想创建一个包含两列的数据框:坐标和值是我们可以找到该像素的次数(如果在整个列表中找不到,则该值必须为 0;如果我们可以找到 x 次,则该值必须为具体数字).
我所做的是:
p = (0,0)
x = 0
y = 1
z = 0
d= {'Coordinates': [p], 'Value': [z]}
df = pd.DataFrame(data=d)
for _ in range(img.size[0]*img.size[1]-1):
new_row = {"p":(x,y), "z":z}
df = df.append(new_row, ignore_index = True)
y = y+1
if y = img.size[1]+1:
y = 0
x= x+1
print(df)
标记的像素在列表的列表中,此代码应该更改的是 z 值。
我想要得到的数据框是这样的:
我拥有的坐标列表示例是:
谢谢你的帮助!! <3
您可以使用 Counter
获取所有计数不等于 0 的唯一像素,然后为了添加计数也为 0 的像素,您可以遍历所有像素 (0 ,0)... (max_x, max_y) 并将它们的计数与 Counter 对象进行比较。
import numpy as np
import pandas as pd
from collections import Counter
coordinates = [[(0,1),(0,2),(0,3)],[(0,1),(0,2),(1,1)],[(1,1),(2,2),(3,3)]]
# this should return a value of 2 for (0,1),(0,2),(1,1)
# value of 1 for (0,3),(2,2),(3,3)
# values of 0 for all other pixels from (0,0)... (3,3) not in coordinates
# flatten the list of coordinates
all_coordinates = [item for img in coordinates for item in img]
c = Counter(all_coordinates)
# you want to look at the counts for pixels (0,0), ... (3,3) in c
max_x = max([item[0] for item in all_coordinates])
max_y = max([item[1] for item in all_coordinates])
coordinates_dict = dict()
for i in range(max_x + 1):
for j in range(max_y + 1):
coordinates_dict.update({(i,j): c[(i,j)]})
df = pd.DataFrame(coordinates_dict.items(), columns=['Coordinates','Value'])
输出:
>>> df
Coordinates Value
0 (0, 0) 0
1 (0, 1) 2
2 (0, 2) 2
3 (0, 3) 1
4 (1, 0) 0
5 (1, 1) 2
6 (1, 2) 0
7 (1, 3) 0
8 (2, 0) 0
9 (2, 1) 0
10 (2, 2) 1
11 (2, 3) 0
12 (3, 0) 0
13 (3, 1) 0
14 (3, 2) 0
15 (3, 3) 1
我有一个列表列表,其中包含不同图像的不同坐标。我想创建一个包含两列的数据框:坐标和值是我们可以找到该像素的次数(如果在整个列表中找不到,则该值必须为 0;如果我们可以找到 x 次,则该值必须为具体数字). 我所做的是:
p = (0,0)
x = 0
y = 1
z = 0
d= {'Coordinates': [p], 'Value': [z]}
df = pd.DataFrame(data=d)
for _ in range(img.size[0]*img.size[1]-1):
new_row = {"p":(x,y), "z":z}
df = df.append(new_row, ignore_index = True)
y = y+1
if y = img.size[1]+1:
y = 0
x= x+1
print(df)
标记的像素在列表的列表中,此代码应该更改的是 z 值。 我想要得到的数据框是这样的:
我拥有的坐标列表示例是:
您可以使用 Counter
获取所有计数不等于 0 的唯一像素,然后为了添加计数也为 0 的像素,您可以遍历所有像素 (0 ,0)... (max_x, max_y) 并将它们的计数与 Counter 对象进行比较。
import numpy as np
import pandas as pd
from collections import Counter
coordinates = [[(0,1),(0,2),(0,3)],[(0,1),(0,2),(1,1)],[(1,1),(2,2),(3,3)]]
# this should return a value of 2 for (0,1),(0,2),(1,1)
# value of 1 for (0,3),(2,2),(3,3)
# values of 0 for all other pixels from (0,0)... (3,3) not in coordinates
# flatten the list of coordinates
all_coordinates = [item for img in coordinates for item in img]
c = Counter(all_coordinates)
# you want to look at the counts for pixels (0,0), ... (3,3) in c
max_x = max([item[0] for item in all_coordinates])
max_y = max([item[1] for item in all_coordinates])
coordinates_dict = dict()
for i in range(max_x + 1):
for j in range(max_y + 1):
coordinates_dict.update({(i,j): c[(i,j)]})
df = pd.DataFrame(coordinates_dict.items(), columns=['Coordinates','Value'])
输出:
>>> df
Coordinates Value
0 (0, 0) 0
1 (0, 1) 2
2 (0, 2) 2
3 (0, 3) 1
4 (1, 0) 0
5 (1, 1) 2
6 (1, 2) 0
7 (1, 3) 0
8 (2, 0) 0
9 (2, 1) 0
10 (2, 2) 1
11 (2, 3) 0
12 (3, 0) 0
13 (3, 1) 0
14 (3, 2) 0
15 (3, 3) 1