首页 > 编程问答 >像素值与 2D NPS 的标准差 - NPS 的总和还是平均值?

像素值与 2D NPS 的标准差 - NPS 的总和还是平均值?

时间:2024-07-26 06:19:34浏览次数:8  
标签:python nps

我正在尝试根据 Python 中的 2D NPS 估计空间域中像素值的 SD。

我希望需要 NPS 值的总和除以像素总数。然而,我只能通过平均值除以像素总数来达到正确的估计。

任何人都可以指出我为什么会这样吗?

请参阅下面的代码示例。

“SUM: “

import numpy as np
from scipy.fftpack import fft2, fftshift

# Generate a random 128x128 image
image = np.random.rand(128, 128)*100

# Subtract the mean of the image
image = image - np.mean(image)

# Compute the 2D Fourier transform of the image
F = fft2(image)

# Shift the zero frequency component to the center
F_shifted = fftshift(F)

# Calculate the Noise Power Spectrum (NPS)
NPS = np.abs(F_shifted)**2

# Calculate the total power in the NPS
total_power = np.sum(NPS)

# The total power in the NPS corresponds to the sum of the squared deviations
# from the mean in the spatial domain. For an image of size NxN:
N = image.shape[0]
variance_from_nps = total_power / (N * N)

# Compute the standard deviation from the variance
sigma_from_nps = np.sqrt(variance_from_nps)

# Compute the standard deviation directly from the image
sd_direct = np.std(image)

print(f"Standard Deviation from NPS: {sigma_from_nps}")
print(f"Standard Deviation directly from the image: {sd_direct}")

NPS 的标准偏差:3692.2142651255785 直接来自图像的标准偏差:28.84542394629358

“平均值:”


import numpy as np
from scipy.fftpack import fft2, fftshift

# Generate a random 128x128 image
image = np.random.rand(128, 128)*100

# Subtract the mean of the image
image = image - np.mean(image)

# Compute the 2D Fourier transform of the image
F = fft2(image)

# Shift the zero frequency component to the center
F_shifted = fftshift(F)

# Calculate the Noise Power Spectrum (NPS)
NPS = np.abs(F_shifted)**2

# Calculate the total power in the NPS
total_power = np.mean(NPS)

# The total power in the NPS corresponds to the sum of the squared deviations
# from the mean in the spatial domain. For an image of size NxN:
N = image.shape[0]
variance_from_nps = total_power / (N * N)

# Compute the standard deviation from the variance
sigma_from_nps = np.sqrt(variance_from_nps)

# Compute the standard deviation directly from the image
sd_direct = np.std(image)

print(f"Standard Deviation from NPS: {sigma_from_nps}")
print(f"Standard Deviation directly from the image: {sd_direct}")

来自 NPS 的标准偏差:28.894608971414826 直接来自图像的标准偏差:28.894608971414826


的理解几乎是正确的,混淆源于如何处理二维 NPS 中的功率谱。是对的,空间域中方差(进而得到标准差)与 NPS 中的 功率有关。但是,仅仅取 np.mean(NPS) 并不能给出总功率,因为它代表了图像中 平均 噪声功率。

让我们分解一下原因:

  1. 帕塞瓦尔定理: 对于像图像这样的离散信号,帕塞瓦尔定理指出空间域中信号的总功率等于傅里叶域中信号的总功率(按合适的归一化因子)。这意味着图像像素值的平方和等于其傅里叶变换的平方幅值的总和(即 NPS)。

  2. 从 NPS 到方差:

    • np.sum(NPS) 给出了傅里叶域中的总功率。
    • 要回到空间域中的方差,需要将该总功率除以像素总数 ( N * N )。这是因为方差是这些平方偏差的 平均值
  3. 代码中的问题: 使用 np.mean(NPS) 时,实际上是在计算每个频率分量处的平均噪声功率,而不是总功率。这就是为什么的“平均值”版本偶然给出接近正确标准差值的原因,因为它有效地对 NPS 值求和,然后除以它们的总数(即 NPS 矩阵中的元素数)。

要更正代码并使之更清晰,应该坚持使用 np.sum(NPS) 来计算总功率,然后按照原始方法计算方差和标准差。

以下是更正后的代码:

import numpy as np
from scipy.fftpack import fft2, fftshift

# Generate a random 128x128 image
image = np.random.rand(128, 128) * 100

# Subtract the mean of the image
image = image - np.mean(image)

# Compute the 2D Fourier transform of the image
F = fft2(image)

# Shift the zero frequency component to the center
F_shifted = fftshift(F)

# Calculate the Noise Power Spectrum (NPS)
NPS = np.abs(F_shifted)**2

# Calculate the total power in the NPS
total_power = np.sum(NPS)

# Calculate the variance from the total power
N = image.shape[0]
variance_from_nps = total_power / (N * N)

# Compute the standard deviation from the variance
sigma_from_nps = np.sqrt(variance_from_nps)

# Compute the standard deviation directly from the image
sd_direct = np.std(image)

print(f"Standard Deviation from NPS: {sigma_from_nps}")
print(f"Standard Deviation directly from the image: {sd_direct}")

这将确保根据 NPS 正确计算标准差,并且应该与直接从图像中计算出的标准差相匹配。

标签:python,nps
From: 78795596

相关文章

  • 84 我正在使用 Python 开发 selenium 自动化项目。我收到错误 .NoSuchElementExceptio
    场景是这样的,我将打开一个网页,在使用selenium单击该网页后,它会要求位置访问权限,屏幕上会出现一堆按钮,我正在尝试定位其中一个按钮,但即使正确给出了Xpath地址,我得到.NoSuchElementException:错误能够单击目标按钮你正在使用Selenium在Python中开发自动化项目,并遇到......
  • 在Python 3中删除两个指定字符串之间的字符串
    我正在从事一个NLP项目,该项目要求我从一段文本中删除计算机代码。代码包含在标签<pre><code>和</code></pre>之间。现在我可以做一个简单的正则表达式匹配,但我想概括这个函数,以便它可以删除任何两个指定字符串之间的文本,即使它们是嵌套的。例如,如果我有一个......
  • Azure Open AI - Python 和 Java API 之间 gpt4o 的结果截然不同
    我使用Java和PythonAPI对AzureOpenAI进行相同的调用,但收到截然不同的结果:相同的系统提示相同的用户提示适用于Java和Python的azureai包的相同(最新)版本尽管输入的用户和系统提示完全相同,但响应却非常不同-python提示是“正确的”并......
  • leetcode 输出错误? (Python)
    我的VSCode/本地终端给出了[1,4,1,5,1,6]的正确输出,但不知何故leetcode给了我完全不同的输出。我在这里错过了什么吗?这怎么可能?顺便说一下,这是wigglesort2将我的本地代码复制粘贴到leetcode中给出了不同的输出数组很难在没有看到你的代码的情况下......
  • 当 python 窗口的一部分不在屏幕上时,如何让它自己被记录?
    在Windows10中,大多数应用程序窗口都可以使用OBS等程序进行记录。当窗口被拖动以致其部分内容在显示屏上不可见时,通常OBS仍会接收窗口的内容,即使它在屏幕上不可见。但是,在编写python应用程序时,这似乎不以相同的方式工作。我尝试了几种不同的类似GUI的模块......
  • 使用 aws cdk 设置用户池客户端属性以具有读/写访问权限 - Python
    我试图根据属性给予一些自定义属性特定的读/写访问权限。我收到此错误。资源处理程序返回消息:“无效写入创建客户端时指定的属性(服务:CognitoIdentityProvider,状态代码:400,请求ID:<request_id>)”(RequestToken:<request_token>,HandlerErrorCode:InvalidRequest)任何人都可以为......
  • 试图找出此页面的逻辑:存储了大约 ++ 100 个结果 - 并使用 Python 和 BS4 进行了解析
    试图找出此页面背后的逻辑:我们已将一些结果存储在以下数据库中:https://www.raiffeisen.ch/rch/de/ueber-uns/raiffeisen-gruppe/Organization/raiffeisenbanken/deutsche-schweiz.html#accordionitem_18104049731620873397从a到z大约:120个结果或更多:......
  • 如何在 Numpy Python 中将 4 维数组的下三角形复制到上三角形?
    目标是将下三角形复制到上三角形。根据OP中提出的建议,起草了以下代码。importnumpyasnplw_up_pair=np.tril_indices(4,-1)arr=np.zeros((4,4,1,1))arr[1,:1,:,0]=1arr[2,:2,0,0]=2arr[3,:3,0,0]=3arr=arr+arr.T-np.diag(np.diag(arr))但是,它......
  • 如何在 Python 中对多行使用单个 INSERT INTO 语句?
    我目前正在开发一个DiscordPython机器人,我在其中循环遍历ForumTags列表,并为每个对象生成INSERTINTOSQL语句以将数据插入MySQL数据库。但是,我想要通过将所有这些单独的INSERTINTO语句组合到单个查询中来优化我的代码,如下所示:INSERTINTO......
  • 双 for 循环的 Pythonic 方式
    我有以下代码:importnumpyasnpepsilon=np.array([[0.,0.00172667,0.00071437,0.00091779,0.00154501],[0.00128983,0.,0.00028139,0.00215905,0.00094862],[0.00035811,0.00018714,0.,0.00029365,0.00036993......