0

我主要使用数字显微镜 (DM) 脚本进行电子显微镜图像处理,最近开始学习 Python,因为它具有更广泛的多功能性、丰富的开放库和跨平台能力。

有人知道 Python(numpy)中是否有类似的工具来索引 2D(图像)或 3D(光谱图像)数组,类似于 DM 的 i 变量?

i 变量在本教程的第 11 页上简要介绍了 DM 脚本: 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
4

2 回答 2

1

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

也可以使用mgridandogrid函数来做到这一点。mgrid将返回一个完全填充的数组中的坐标(与源图像相同的形状,与 numpy.indices 相同),同时ogrid返回通常会自动正确广播的正确形状的行和列向量。

如果您使用它为傅里叶变换图像创建蒙版,还有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)
于 2015-08-18T21:04:23.233 回答
0

Pillow使用 numpy 数组 IIRC。Scipy 有一些图像功能,但不擅长加载和保存图像。

于 2015-08-16T21:54:12.520 回答