如何创建强度为 0 到 255 的正态分布图像?

问题描述 投票:0回答:1

解决我的最后一个问题如何以正态分布为参考进行直方图匹配?我想创建一个具有正态分布的图像。对于新图像的每个像素,我想随机选择一个从 0 到 255 的数字,并且服从正态分布。我这样做过:

normal_image = np.random.normal(0, 1, size = (M,N))

但是这张图片的dtype是float64。所以然后我这样做了:

normal_image = np.random.normal(0, 1, size = (M,N)).astype('uint8')

但我不确定这是否是正确的做法。我应该根据正态分布从 0 到 255 的整数中选择随机数吗?(我不知道该怎么做!)

你能指导我吗?

python image-processing types normal-distribution
1个回答
0
投票

您可以将

normal
的值归一化到 0 和 1 之间,然后乘以 255:

import numpy as np

M = 10
N = 10
n = np.random.normal(0, 1, size=(M, N))
s = (n - n.min()) / (n.max() - n.min())
i = (255 * s).astype(np.uint8)

输出:

>>> i
array([[146, 117, 141, 120,  64, 105, 155, 154,  89,  81],
       [109,  86, 152, 105, 168,  51, 195,  50, 117,  65],
       [112,   0,  95, 102,  82,  74,  79,  98,  27, 183],
       [131, 172, 102, 220, 255,  94,  96, 138, 111, 106],
       [131, 170, 151,  97, 169, 138,  28,  74, 125, 151],
       [119, 170,  83, 190,  65, 184,  40, 183, 121, 104],
       [191, 193,  91,  80, 145,  49,  92,  87, 160, 132],
       [141,  76, 131,  65,  93,  98, 187,  66,  98, 168],
       [185,  81, 182, 210,  90, 151,  39,  99, 104, 123],
       [ 98, 109, 154, 215, 130,  93, 146, 156, 121,  37]], dtype=uint8)

对于这个数组:

import matplotlib.pyplot as plt

plt.hist(i.ravel(), bins=10)
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.