Python 二值图像数据帧到欧氏坐标
我希望获取一个图像的数据帧,它是假/真的二进制布尔值,并将其转换为一个坐标数组,其中数据帧为真Python 二值图像数据帧到欧氏坐标,python,image,pandas,Python,Image,Pandas,我希望获取一个图像的数据帧,它是假/真的二进制布尔值,并将其转换为一个坐标数组,其中数据帧为真 例如,如果索引[4]和列[8]为true,则会将4,8添加到数组中 IIUC您可以这样做: In [70]: df Out[70]: a b c 0 True False True 1 True True False 2 False True True 3 False True True 4 True False Fal
例如,如果索引[4]和列[8]为true,则会将4,8添加到数组中 IIUC您可以这样做:
In [70]: df
Out[70]:
a b c
0 True False True
1 True True False
2 False True True
3 False True True
4 True False False
5 False True False
6 True False False
7 False True False
8 False False True
9 True False True
In [71]: np.dstack(np.nonzero(df.values))[0]
Out[71]:
array([[0, 0],
[0, 2],
[1, 0],
[1, 1],
[2, 1],
[2, 2],
[3, 1],
[3, 2],
[4, 0],
[5, 1],
[6, 0],
[7, 1],
[8, 2],
[9, 0],
[9, 2]], dtype=int64)
或:
设置:
df = pd.DataFrame(np.random.choice([True, False], (10, 3)), columns=list('abc'))
设置
使用@MaxU的示例数据帧
df = pd.DataFrame({
'a': [True, True, False, False, True, False, True, False, False, True],
'b': [False, True, True, True, False, True, False, True, False, False],
'c': [True, False, True, True, False, False, False, False, True, True]})
我猜您需要数据帧的
索引
和列的坐标`
选项1堆栈
+掩码
s = df.stack()
s[s].index.values
array([(0, 'a'), (0, 'c'), (1, 'a'), (1, 'b'), (2, 'b'), (2, 'c'),
(3, 'b'), (3, 'c'), (4, 'a'), (5, 'b'), (6, 'a'), (7, 'b'),
(8, 'c'), (9, 'a'), (9, 'c')], dtype=object)
选项2仅使用
np.where
np.stack(np.where(df.values)).reshape(-1, 2)
array([[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 5],
[6, 7],
[8, 9],
[9, 0],
[2, 0],
[1, 1],
[2, 1],
[2, 0],
[1, 0],
[1, 2],
[0, 2]])
r, c = np.where(df.values)
list(zip(df.index[r], df.columns[c]))
[(0, 'a'),
(0, 'c'),
(1, 'a'),
(1, 'b'),
(2, 'b'),
(2, 'c'),
(3, 'b'),
(3, 'c'),
(4, 'a'),
(5, 'b'),
(6, 'a'),
(7, 'b'),
(8, 'c'),
(9, 'a'),
(9, 'c')]
选项3使用
np.where
np.stack(np.where(df.values)).reshape(-1, 2)
array([[0, 0],
[1, 1],
[2, 2],
[3, 3],
[4, 5],
[6, 7],
[8, 9],
[9, 0],
[2, 0],
[1, 1],
[2, 1],
[2, 0],
[1, 0],
[1, 2],
[0, 2]])
r, c = np.where(df.values)
list(zip(df.index[r], df.columns[c]))
[(0, 'a'),
(0, 'c'),
(1, 'a'),
(1, 'b'),
(2, 'b'),
(2, 'c'),
(3, 'b'),
(3, 'c'),
(4, 'a'),
(5, 'b'),
(6, 'a'),
(7, 'b'),
(8, 'c'),
(9, 'a'),
(9, 'c')]