DM 脚本中是否有 i 变量(例如 irow、iradius、itheta 等)的 python (numpy) 同义词?
Is there any python (numpy) synonym to i-variables (e.g., irow, iradius, itheta, etc.) in DM scripting?
我主要使用数字显微镜 (DM) 脚本进行电子显微镜图像处理,并且最近开始学习 Python 因为它具有更广泛的通用性、丰富的开放库和跨平台能力。
有人知道 Python (numpy) 中是否有类似的工具来索引 2D(图像)或 3D(光谱图像)数组,类似于 DM 的 i 变量?
本教程关于 DM 脚本的第 11 页简要介绍了 i 变量:
http://portal.tugraz.at/portal/page/portal/Files/felmi/images/DM-Script/DM-basic-scripting_bs.pdf
它们是索引任何类似图像的 2D 或 3D 对象的简单方法,这对于图像处理非常方便,例如生成掩码函数
例如,下面的 DM 脚本
image t1 := RealImage ("test1", 4, 5, 5)
image t2 := RealImage ("test2", 4, 5, 5)
image t3 := RealImage ("test3", 4, 5, 5)
t1 = irow
// the value in each pixel equals to the row index
t2 = iradius
// the value in each pixel equals to the radius
// (i.e., distance to the center pixel)
t3 = itheta
// the value in each pixel quals to the angle (radian)
// to the center pixel (i.e., angle in polar representation)
t1.showimage(); t2.showimage(); t3.showimage()
生成以下图像(此处以电子表格或矩阵形式表示):
t1 =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
t2=
3.5355339 2.9154758 2.5495098 2.5495098 2.9154758
2.9154758 2.1213202 1.5811388 1.5811388 2.1213202
2.5495098 1.5811388 0.70710677 0.70710677 1.5811388
2.5495098 1.5811388 0.70710677 0.70710677 1.5811388
2.9154758 2.1213202 1.5811388 1.5811388 2.1213202
t3=
-2.3561945 -2.1112158 -1.7681919 -1.3734008 -1.0303768
-2.6011732 -2.3561945 -1.8925469 -1.2490457 -0.78539819
-2.9441972 -2.8198421 -2.3561945 -0.78539819 -0.32175055
2.9441972 2.8198421 2.3561945 0.78539819 0.32175055
2.6011732 2.3561945 1.8925469 1.2490457 0.78539819
Pillow 使用 numpy 数组 IIRC。 Scipy有一些图片功能,但不擅长加载和保存图片。
在 NumPy 中执行此操作的等效方法是使用 numpy.indices
函数。
因此,在 NumPy 中执行与在 DM 中相同的操作(记住在 numpy 中坐标始终为 (y,x),并且 irow 是 y 坐标的索引):
from __future__ import division
import numpy as np
test1 = np.random.random((5,5))
irow, icol = np.indices(test1.shape)
# to use iradius and itheta we need to get icol, irow centered
irow_centered = irow - test1.shape[0] / 2.0
icol_centered = icol - test1.shape[1] / 2.0
iradius = (icol_centered**2 + irow_centered**2)**0.5
itheta = np.arctan2(irow_centered, icol_centered)
然后
>>> irow
array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]])
>>> iradius
array([[ 3.53553391, 2.91547595, 2.54950976, 2.54950976, 2.91547595],
[ 2.91547595, 2.12132034, 1.58113883, 1.58113883, 2.12132034],
[ 2.54950976, 1.58113883, 0.70710678, 0.70710678, 1.58113883],
[ 2.54950976, 1.58113883, 0.70710678, 0.70710678, 1.58113883],
[ 2.91547595, 2.12132034, 1.58113883, 1.58113883, 2.12132034]])
>>> itheta
array([[-2.35619449, -2.11121583, -1.76819189, -1.37340077, -1.03037683],
[-2.60117315, -2.35619449, -1.89254688, -1.24904577, -0.78539816],
[-2.94419709, -2.8198421 , -2.35619449, -0.78539816, -0.32175055],
[ 2.94419709, 2.8198421 , 2.35619449, 0.78539816, 0.32175055],
[ 2.60117315, 2.35619449, 1.89254688, 1.24904577, 0.78539816]])
也可以使用 mgrid
and ogrid
函数来做到这一点。 mgrid
将 return 完全填充数组中的坐标(与源图像相同的形状,与 numpy.indices 相同),而 ogrid
return 的行和列正确形状的向量通常会自动正确广播。
如果您使用它为傅立叶变换图像创建蒙版,还有 np.fft.fftfreq 函数可以为您提供傅立叶变换中像素的频率。我使用以下方法获取给定图像形状的每个像素的频率平方:
def get_freq_squared(shape, packed_complex=True):
"""Return the frequency squared for an n-d array.
The returned image will match shape, if packed_complex is true the last
dimension will be treated as 0,f,...,nf rather than 0,f,..., nf,...,-f
"""
vecs = [np.fft.fftfreq(s, 1.0 / s) ** 2 for s in shape]
if packed_complex:
s = shape[-1]
olds = (s-1)*2
vecs[-1] = rfftfreq(olds, 1.0/olds)**2
return reduce(np.add.outer, vecs)
我主要使用数字显微镜 (DM) 脚本进行电子显微镜图像处理,并且最近开始学习 Python 因为它具有更广泛的通用性、丰富的开放库和跨平台能力。
有人知道 Python (numpy) 中是否有类似的工具来索引 2D(图像)或 3D(光谱图像)数组,类似于 DM 的 i 变量?
本教程关于 DM 脚本的第 11 页简要介绍了 i 变量: http://portal.tugraz.at/portal/page/portal/Files/felmi/images/DM-Script/DM-basic-scripting_bs.pdf
它们是索引任何类似图像的 2D 或 3D 对象的简单方法,这对于图像处理非常方便,例如生成掩码函数
例如,下面的 DM 脚本
image t1 := RealImage ("test1", 4, 5, 5)
image t2 := RealImage ("test2", 4, 5, 5)
image t3 := RealImage ("test3", 4, 5, 5)
t1 = irow
// the value in each pixel equals to the row index
t2 = iradius
// the value in each pixel equals to the radius
// (i.e., distance to the center pixel)
t3 = itheta
// the value in each pixel quals to the angle (radian)
// to the center pixel (i.e., angle in polar representation)
t1.showimage(); t2.showimage(); t3.showimage()
生成以下图像(此处以电子表格或矩阵形式表示):
t1 =
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
t2=
3.5355339 2.9154758 2.5495098 2.5495098 2.9154758
2.9154758 2.1213202 1.5811388 1.5811388 2.1213202
2.5495098 1.5811388 0.70710677 0.70710677 1.5811388
2.5495098 1.5811388 0.70710677 0.70710677 1.5811388
2.9154758 2.1213202 1.5811388 1.5811388 2.1213202
t3=
-2.3561945 -2.1112158 -1.7681919 -1.3734008 -1.0303768
-2.6011732 -2.3561945 -1.8925469 -1.2490457 -0.78539819
-2.9441972 -2.8198421 -2.3561945 -0.78539819 -0.32175055
2.9441972 2.8198421 2.3561945 0.78539819 0.32175055
2.6011732 2.3561945 1.8925469 1.2490457 0.78539819
Pillow 使用 numpy 数组 IIRC。 Scipy有一些图片功能,但不擅长加载和保存图片。
在 NumPy 中执行此操作的等效方法是使用 numpy.indices
函数。
因此,在 NumPy 中执行与在 DM 中相同的操作(记住在 numpy 中坐标始终为 (y,x),并且 irow 是 y 坐标的索引):
from __future__ import division
import numpy as np
test1 = np.random.random((5,5))
irow, icol = np.indices(test1.shape)
# to use iradius and itheta we need to get icol, irow centered
irow_centered = irow - test1.shape[0] / 2.0
icol_centered = icol - test1.shape[1] / 2.0
iradius = (icol_centered**2 + irow_centered**2)**0.5
itheta = np.arctan2(irow_centered, icol_centered)
然后
>>> irow
array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]])
>>> iradius
array([[ 3.53553391, 2.91547595, 2.54950976, 2.54950976, 2.91547595],
[ 2.91547595, 2.12132034, 1.58113883, 1.58113883, 2.12132034],
[ 2.54950976, 1.58113883, 0.70710678, 0.70710678, 1.58113883],
[ 2.54950976, 1.58113883, 0.70710678, 0.70710678, 1.58113883],
[ 2.91547595, 2.12132034, 1.58113883, 1.58113883, 2.12132034]])
>>> itheta
array([[-2.35619449, -2.11121583, -1.76819189, -1.37340077, -1.03037683],
[-2.60117315, -2.35619449, -1.89254688, -1.24904577, -0.78539816],
[-2.94419709, -2.8198421 , -2.35619449, -0.78539816, -0.32175055],
[ 2.94419709, 2.8198421 , 2.35619449, 0.78539816, 0.32175055],
[ 2.60117315, 2.35619449, 1.89254688, 1.24904577, 0.78539816]])
也可以使用 mgrid
and ogrid
函数来做到这一点。 mgrid
将 return 完全填充数组中的坐标(与源图像相同的形状,与 numpy.indices 相同),而 ogrid
return 的行和列正确形状的向量通常会自动正确广播。
如果您使用它为傅立叶变换图像创建蒙版,还有 np.fft.fftfreq 函数可以为您提供傅立叶变换中像素的频率。我使用以下方法获取给定图像形状的每个像素的频率平方:
def get_freq_squared(shape, packed_complex=True):
"""Return the frequency squared for an n-d array.
The returned image will match shape, if packed_complex is true the last
dimension will be treated as 0,f,...,nf rather than 0,f,..., nf,...,-f
"""
vecs = [np.fft.fftfreq(s, 1.0 / s) ** 2 for s in shape]
if packed_complex:
s = shape[-1]
olds = (s-1)*2
vecs[-1] = rfftfreq(olds, 1.0/olds)**2
return reduce(np.add.outer, vecs)