英文:
Fastest way to make an image from a list of 2D points coordinates
问题
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
x = 10np.random.randn(2000)+32
y = 10np.random.randn(2000)+32
创建一个由二维坐标(x,y)定义的点列表
im = np.zeros((64, 64))
dx = np.arange(64)
dy = np.arange(64)
for i in range(len(x)):
xt, yt = x[i], y[i]
ind_x = np.argmin((abs(dx-xt)))
ind_y = np.argmin((abs(dy-yt)))
im[ind_x, ind_y] += 1
通过使用这些点填充二维数组来形成图像
plt.imshow(im)
对于以下情况,我正在寻找最快的实现:
情况1)大量的点
情况2)图像中有大量的像素
谢谢!
英文:
I have a list of points defined by their 2D coordinates (x, y).
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
x = 10*np.random.randn(2000)+32
y = 10*np.random.randn(2000)+32
I am looking for a fast python implementation of the following code, which forms an image by filling a 2D array with these points:
im = np.zeros((64, 64))
dx = np.arange(64)
dy = np.arange(64)
for i in range(len(x)):
xt, yt = x[i], y[i]
ind_x = np.argmin((abs(dx-xt)))
ind_y = np.argmin((abs(dy-yt)))
im[ind_x, ind_y] += 1
plt.imshow(im)
I am looking for the fastest implementation for:
- Case 1) A large number of points
- Case 2) A large number of pixels in the image
Thanks!
答案1
得分: 4
基本上,您正在尝试为 x 和 y 创建一个二维直方图。
np.random.seed(123)
x = 10*np.random.randn(2000)+32
y = 10*np.random.randn(2000)+32
# 创建 x 和 y 值的二维直方图
hist, xedges, yedges = np.histogram2d(x, y, bins=64, range=[[0, 64], [0, 64]])
plt.imshow(hist)
英文:
Basically you are trying to create a 2D histogram for x and y.
np.random.seed(123)
x = 10*np.random.randn(2000)+32
y = 10*np.random.randn(2000)+32
# Create a 2D histogram of the x and y values
hist, xedges, yedges = np.histogram2d(x, y, bins=64, range=[[0, 64], [0, 64]])
plt.imshow(hist)
答案2
得分: 1
你可以尝试这样做:
import numpy as np
import matplotlib.pyplot as plt
N = 2000
M = 64
np.random.seed(123)
x = 10 * np.random.randn(N) + 32
y = 10 * np.random.randn(N) + 32
xint = np.clip(np.rint(x), 0, M - 1).astype(int)
yint = np.clip(np.rint(y), 0, M - 1).astype(int)
im4 = np.zeros((M, M))
np.add.at(im4, (xint, yint), 1)
plt.imshow(im4)
它产生的结果如下图所示:
英文:
You can try this:
import numpy as np
import matplotlib.pyplot as plt
N = 2000
M = 64
np.random.seed(123)
x = 10*np.random.randn(N)+32
y = 10*np.random.randn(N)+32
xint = np.clip(np.rint(x), 0, M - 1).astype(int)
yint = np.clip(np.rint(y), 0, M - 1).astype(int)
im4 = np.zeros((M, M))
np.add.at(im4, (xint, yint), 1)
plt.imshow(im4)
It gives:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论