Python;确定并向图像段添加噪波

2024-05-14 20:44:16 发布

您现在位置:Python中文网/ 问答频道 /正文

为了掌握编码技巧,我决定使用Python/Numpy/SKimage确定x射线照片某个区域的标准偏差。首先,我决定使用阈值来获得图像的一部分,这并不难。在

然而,在这一点上,所有高于/低于阈值的东西都是零,因此我想做的测量包括在内。因此,我需要排除低于/高于阈值的数据。在

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

我的基本知识-

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

我在寻找一种排除高于/低于阈值的数据的方法。有人能帮我往正确的方向推一下吗?在

编辑:偶尔有一天的轻松工作很不错;我问题的一个次要解决方案是将所有大于或小于该值的值添加到列表中,并确定与列表的标准偏差。然而,这给我留下了一个问题,即如何将噪声实现到图像段。在

^{pr2}$

Tags: 数据图像imageimporttrue编码列表as
2条回答

我想问题是你把所有的像素都设为零,然后试着从中得到统计数据。相反,要意识到test1[image < 100]只指那些低于阈值的像素。。。所以我想你可以从中得到你的统计数据,比如用np.std(test1[image < 100])。在

您可能想看看^{},它包含了很多用于阈值分割、处理二进制图像、将它们用作遮罩(这实际上就是您正在做的)等工具

... determine the standard deviation in a certain area of a x-ray picture using Python/Numpy/SKimage.

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

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

让我们通过双阈值来定义感兴趣的区域:

^{pr2}$

Boolean indexing是计算感兴趣区域标准差的一种可能方法:

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的^{}来计算标准偏差,这是另一种方法:

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

... the problem of implementing the noise to the image segments.

您可以使用scikit images的^{}生成一个噪声图像,然后通过NumPy的^{}过滤掉感兴趣区域之外的像素:

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

相关问题 更多 >

    热门问题