如何创建一个具有0到255强度的正态分布图像?

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

How to create an image with intensities 0 to 255 with normal distribution?

问题

以下是您要翻译的内容:

"为了解决我的上一个问题如何使用正态分布作为参考进行直方图匹配?,我想创建一个具有正态分布的图像。为此,对于新图像的每个像素,我想随机选择一个介于0到255之间的数字,并且符合正态分布。我已经完成了这个步骤:

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

但这个图像的数据类型是float64。所以然后我进行了以下操作:

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

但我不确定这是否是正确的方法。我应该根据正态分布选择介于0到255之间的整数随机数吗?(我不知道如何做到这一点!)

您能指导我吗?

英文:

To solve my last question How to do histogram matching with normal distribution as reference? I want to create an image with normal distribution. For that for every pixel of the new image I want to choose a number from 0 to 255 randomly and with normal distribution. I've done this:

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

But the dtype of this image is float64. So then I did this:

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

But I'm not sure if this is a correct approach. Should I choose random numbers from the integers 0 to 255 based on normal distribution?(Which I don't know how to do this!)

Would you please guide me?

答案1

得分: 2

以下是代码部分的中文翻译:

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()

图片部分不可翻译,你可以查看图片的链接来查看相关图片。

英文:

You can normalize values from normal between 0 and 1 then multiply by 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)

Output:

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

For this array:

import matplotlib.pyplot as plt

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

如何创建一个具有0到255强度的正态分布图像?

huangapple
  • 本文由 发表于 2023年3月31日 16:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896240.html
匿名

发表评论

匿名网友

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

确定