如何创建uint16高斯噪声图像? [英] How to create uint16 gaussian noise image?

查看:121
本文介绍了如何创建uint16高斯噪声图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建具有定义的均值和标准偏差的高斯噪声的 uint16 图像.

I want to create a uint16 image of gaussian noise with a defined mean and standard deviation.

我尝试为此使用numpy的random.normal,但它返回一个float64数组:

I've tried using numpy's random.normal for this but it returns a float64 array:

mu = 10
sigma = 100
shape = (1024,1024)
gauss_img = np.random.normal(mu, sigma, shape)

print(gauss_img.dtype)

>>> dtype('float64')

是否有一种方法可以将gauss_img转换为uint16数组,同时保留原始均值和标准差?还是有另一种完全创建uint16噪点图像的方法?

Is there a way to convert gauss_img to a uint16 array while preserving the original mean and standard deviation? Or is there another approach entirely to creating a uint16 noise image?

如评论中所述,np.random.normal在给定sd>均值的情况下将不可避免地对负值进行采样,这对于转换为uint16是一个问题.

As was mentioned in the comments, np.random.normal will inevitably sample negative values given a sd > mean, which is a problem for converting to uint16.

所以我认为我需要一种可以直接创建 unsigned 高斯图像的方法.

So I think I need a different method that will create an unsigned gaussian image directly.

推荐答案

所以我认为这与您要查找的内容很接近.

So I think this is close to what you're looking for.

导入库并欺骗一些偏斜的数据.在这里,由于输入来源不明,因此我使用np.expm1(np.random.normal())创建了偏斜数据.您也可以使用skewnorm().rvs(),但这有点作弊,因为这也是您用来表征它的库.

Import libraries and spoof some skewed data. Here, since the input is of unknown origin, I created skewed data using np.expm1(np.random.normal()). You could use skewnorm().rvs() as well, but that's kind of cheating since that's also the lib you'll use to characterize it.

我将原始样本弄平,以使绘制直方图更加容易.

I flatten the raw samples to make plotting histograms easier.

import numpy as np
from scipy.stats import skewnorm

# generate dummy raw starting data
# smaller shape just for simplicity
shape = (100, 100)
raw_skewed = np.maximum(0.0, np.expm1(np.random.normal(2, 0.75, shape))).astype('uint16')

# flatten to look at histograms and compare distributions
raw_skewed = raw_skewed.reshape((-1))

现在找到表征偏斜数据的参数,并使用这些参数创建一个新的分布以从希望与原始数据很好地匹配的样本中进行采样.

Now find the params that characterize your skewed data, and use those to create a new distribution to sample from that hopefully matches your original data well.

这两行代码正是我想得到的.

# find params
a, loc, scale = skewnorm.fit(raw_skewed)

# mimick orig distribution with skewnorm
new_samples = skewnorm(a, loc, scale).rvs(10000).astype('uint16')

现在绘制每个要比较的分布.

Now plot the distributions of each to compare.

plt.hist(raw_skewed, bins=np.linspace(0, 60, 30), hatch='\\', label='raw skewed')
plt.hist(new_samples, bins=np.linspace(0, 60, 30), alpha=0.65, color='green', label='mimic skewed dist')
plt.legend()

直方图非常接近.如果看起来足够好,请将新数据重塑为所需的形状.

The histograms are pretty close. If that looks good enough, reshape your new data to the desired shape.

# final result
new_samples.reshape(shape)

现在...这是我认为可能不足的地方.看一下每个的热图.原始分布在右侧的尾巴更长(skewnorm()所没有的异常值).

Now... here's where I think it probably falls short. Take a look at the heatmap of each. The original distribution had a longer tail to the right (more outliers that skewnorm() didn't characterize).

这将绘制每个图的热图.

This plots a heatmap of each.

# plot heatmaps of each
fig = plt.figure(2, figsize=(18,9))
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)

im1 = ax1.imshow(raw_skewed.reshape(shape), vmin=0, vmax=120)
ax1.set_title("raw data - mean: {:3.2f}, std dev: {:3.2f}".format(np.mean(raw_skewed), np.std(raw_skewed)), fontsize=20)

im2 = ax2.imshow(new_samples.reshape(shape), vmin=0, vmax=120)
ax2.set_title("mimicked data - mean: {:3.2f}, std dev: {:3.2f}".format(np.mean(new_samples), np.std(new_samples)), fontsize=20)

plt.tight_layout()

# add colorbar
fig.subplots_adjust(right=0.85)
cbar_ax = fig.add_axes([0.88, 0.1, 0.08, 0.8])  # [left, bottom, width, height]
fig.colorbar(im1, cax=cbar_ax)

看一下它...您会看到偶尔出现的黄色斑点,表明原始分布中的值很高,而没有进入输出.这也显示在输入数据的较高std dev中(请参见每个热图中的标题,但同样,如对原始问题的评论中所示...意味着& std不能真正表征分布,因为它们是不正常的. ..但它们只是作为相对比较).

Looking at it... you can see occasional flecks of yellow indicating very high values in the original distribution that didn't make it into the output. This also shows up in the higher std dev of the input data (see titles in each heatmap, but again, as in comments to original question... mean & std don't really characterize the distributions since they're not normal... but they're in as a relative comparison).

但是...这只是我创建的非常具体的偏斜样本所面临的问题.希望这里有足够的东西可以进行弄乱和调整,直到它适合您的需求和您的特定数据集为止.祝你好运!

But... that's just the problem it has with the very specific skewed sample i created to get started. There's hopefully enough here to mess around with and tune until it suits your needs and your specific dataset. Good luck!

这篇关于如何创建uint16高斯噪声图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆