<blockquote>
<p>... determine the standard deviation in a certain area of a x-ray picture using Python/Numpy/SKimage.</p>
</blockquote>
<p>让我们首先生成一个模拟图像:</p>
<pre><code>In [18]: import numpy as np
In [19]: rows, cols = 4, 4
In [20]: image = np.linspace(0, 1, rows*cols).reshape(rows, cols)
In [21]: np.set_printoptions(precision=2)
In [22]: image
Out[22]:
array([[0. , 0.07, 0.13, 0.2 ],
[0.27, 0.33, 0.4 , 0.47],
[0.53, 0.6 , 0.67, 0.73],
[0.8 , 0.87, 0.93, 1. ]])
</code></pre>
<p>让我们通过双阈值来定义感兴趣的区域:</p>
^{pr2}$
<p><a href="https://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays" rel="nofollow noreferrer">Boolean indexing</a>是计算感兴趣区域标准差的一种可能方法:</p>
<pre><code>In [29]: image[mask]
Out[29]: array([0.33, 0.4 , 0.47, 0.53])
In [30]: np.std(image[mask])
Out[30]: 0.07453559924999299
</code></pre>
<p>将不需要的像素设置为<code>np.nan</code>,并使用NumPy的<a href="https://www.numpy.org/devdocs/reference/generated/numpy.nanstd.html" rel="nofollow noreferrer">^{<cd2>}</a>来计算标准偏差,这是另一种方法:</p>
<pre><code>In [32]: test1 = np.where(mask, image, np.nan)
In [33]: test1
Out[33]:
array([[ nan, nan, nan, nan],
[ nan, 0.33, 0.4 , 0.47],
[0.53, nan, nan, nan],
[ nan, nan, nan, nan]])
In [34]: np.nanstd(test1)
Out[34]: 0.07453559924999299
</code></pre>
<blockquote>
<p>... the problem of implementing the noise to the image segments.</p>
</blockquote>
<p>您可以使用scikit images的<a href="https://scikit-image.org/docs/dev/api/skimage.util.html#skimage.util.random_noise" rel="nofollow noreferrer">^{<cd3>}</a>生成一个噪声图像,然后通过NumPy的<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html" rel="nofollow noreferrer">^{<cd4>}</a>过滤掉感兴趣区域之外的像素:</p>
<pre><code>In [36]: from skimage.util import random_noise
In [37]: noisy = random_noise(image)
In [38]: noisy
Out[38]:
array([[0.14, 0.07, 0.17, 0.29],
[0.34, 0.39, 0.38, 0.53],
[0.66, 0.73, 0.66, 0.67],
[0.73, 0.98, 1. , 0.88]])
In [39]: np.where(mask, noisy, image)
Out[39]:
array([[0. , 0.07, 0.13, 0.2 ],
[0.27, 0.39, 0.38, 0.53],
[0.66, 0.6 , 0.67, 0.73],
[0.8 , 0.87, 0.93, 1. ]])
</code></pre>