gpt4 book ai didi

python - 测试图像中的像素

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:41 25 4
gpt4 key购买 nike

我试图通过使用以下循环对特定欧几里德距离内的所有像素进行平均来对图像进行降噪。目前这个循环需要 65 秒,可能需要执行数千次。有没有一种方法可以在 python 中实现这一点,而不需要令人望而却步的运行时间?任何帮助将不胜感激。

for row in range(0, width):
for column in range(0, height):
if euclid_dist(point, pix[row,column]) <= threshold:
to_average[(row,column)] = pix[row, column]

euclid_dist 定义如下:

def euclid_dist(tuple1, tuple2):

tot_sq = 0
for num1, num2 in zip(tuple1, tuple2):
tot_sq += (num1 + num2)**2
return math.sqrt(tot_sq)

最佳答案

如果您只想对圆中的所有内容进行平均(相等,而不是使用高斯),您可以制作一个硬圆核,然后将图像与它进行卷积,如下所示。

# make a noisy circle thing
img = np.random.randint(100, 200, (200, 200), dtype=np.uint8)
xx, yy = np.meshgrid(np.arange(-100, 100), np.arange(-100, 100))
img = img + 10 * (xx**2 + yy**2 < 50**2)
plt.imshow(img)

enter image description here

# make a non-standard kernel
radius = 10
kernel_size = 2 * int(radius) + 1 # odd
xy = np.arange(-kernel_size//2 + 1, kernel_size//2 + 1)
xx, yy = np.meshgrid(xy, xy)

kernel = np.zeros((kernel_size,) * 2)
kernel[xx**2 + yy**2 <= radius**2] = 1
plt.imshow(kernel)

enter image description here

# convolve the two, depending on the mode, it will change the dimensions of the output
plt.imshow(sig.convolve2d(img, kernel, mode='valid'))

enter image description here

如果你想去噪,你也可以使用高斯核,这更常见一些。那么它更简单地称为“高斯模糊”。

关于python - 测试图像中的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46186310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com