蟒蛇;确定图像段并为其添加噪波

时间:2019-04-23 10:55:39

标签: python numpy scikit-image

为了掌握编码方法,我决定使用Python / Numpy / SKimage确定X射线图片某些区域的标准差。首先,我决定使用阈值获取图像的一部分,这并不难。

但是,在这一点上,阈值之上/之下的所有内容均为0,因此包含在我想要进行的测量中。因此,我需要排除低于阈值/高于阈值的数据。

我想它将能够创建地图或排除某些价值或更可能是更奇特的解决方案。但是,在这一点上,我认为我可能会朝错误的方向前进。

我的基础-

import numpy as np
import matplotlib.pyplot as plt
import imageio

image = imageio.imread('thx.jpg', as_gray=True)
image = image.astype('int32')

test1 = np.array(image, copy=True)
test1[image >= 100] = 0

我正在寻找一种排除阈值以上/以下数据的方法。有人可以为我提供一些正确方向的帮助吗?

编辑:偶尔有轻松的一天工作真是太好了;我的问题的一个子解决方案是将所有大于或小于该值的值都添加到列表中,并确定与列表的标准偏差。但是,这给我带来了在图像段中实施噪声 的问题。

im_row = image.shape[0]
im_col = image.shape[1]
grthen = []
smlthen = []

for i in range(0,im_col-1):
    for j in range(0,im_row-1):
        if (j > 100):
           grthen.append(j)
        else:
            smlthen.append(j)

print(np.std(smlthen))
print(np.std(grthen))

2 个答案:

答案 0 :(得分:2)

我认为问题在于您将所有这些像素设置为零,然后尝试从中获取统计信息。相反,请意识到test1[image < 100]仅指阈值以下的那些像素...因此,我认为您可以从中获取统计信息,例如与np.std(test1[image < 100])

您可能想看看scikit-image,其中包含许多用于阈值处理,处理二进制图像,将其用作遮罩的工具(本质上就是您要做的事情)等。

答案 1 :(得分:1)

  

...使用Python / Numpy / SKimage确定X射线照片特定区域的标准偏差。

让我们首先生成一个模拟图像:

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

让我们通过双重阈值定义关注区域:

In [25]: low, high = .3, .6

In [26]: mask = np.logical_and(image > low, image < high)

In [27]: mask
Out[27]: 
array([[False, False, False, False],
       [False,  True,  True,  True],
       [ True, False, False, False],
       [False, False, False, False]])

SpeechRecognizedEventArgs.Result.Audio是一种在感兴趣区域上计算标准偏差的可能方法:

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

将不想要的像素设置为np.nan并使用NumPy的Boolean indexing计算标准差将是另一种方法:

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
  

...对图像片段产生噪声的问题。

您可以使用scikit-images的nanstd生成嘈杂的图像,然后通过NumPy的random_noise过滤掉感兴趣区域之外的那些像素:

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