从一组2D点坐标创建图像的最快方法

huangapple go评论64阅读模式
英文:

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 = 10
np.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!

从一组2D点坐标创建图像的最快方法

答案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)

从一组2D点坐标创建图像的最快方法

答案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)

它产生的结果如下图所示:

从一组2D点坐标创建图像的最快方法

英文:

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:

从一组2D点坐标创建图像的最快方法

huangapple
  • 本文由 发表于 2023年6月2日 02:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定