import numpy as np
np.random.seed(10)
img = np.random.randint(0,10,(10,10))
h,w= img.shape
points= [[x-w/2,y-h/2,img[y,x]] for y in range(h) for x in range(w) if img[y,x]!=0]
points = np.array(points)
与努比
np_points = np.vstack([np.where(img != 0), img[img != 0].ravel()]).T[:,[1,0,2]] - [w/2 , h/2, 0]
assert np.array_equal(np_points,points), 'solutions are not equal'
如果我正确理解了你的问题,你可以使用纯numpy解决方案
设置示例数据
与努比
对这两种解决方案进行基准测试没有太大区别。纯numpy解决方案可能会受益于一次性对所有336幅图像进行矢量化计算
输出:
输出:
相关问题 更多 >
编程相关推荐