首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有python (numpy)同义词与i-变量(例如irow、iradius、itheta等)在DM脚本中?

是否有python (numpy)同义词与i-变量(例如irow、iradius、itheta等)在DM脚本中?
EN

Stack Overflow用户
提问于 2015-08-16 21:38:37
回答 2查看 282关注 0票数 0

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

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

在本教程的第11页简要介绍了i-变量:DM-脚本:bs.pdf

它们是索引任何图像类2D或3D对象的简单方法,非常方便图像处理,例如生成掩码函数。

例如,下面的DM-脚本

代码语言:javascript
运行
复制
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()

结果显示以下图像(在此以电子表格表示,或以矩阵形式表示):

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-18 21:04:23

在NumPy中执行此操作的等效方法是使用numpy.indices函数。

因此,要在numpy中执行与在DM中相同的操作(请记住,NumPy中的坐标总是(y,x),irow是y坐标的索引):

代码语言:javascript
运行
复制
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)

然后

代码语言:javascript
运行
复制
>>> 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]])

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

如果你用这个来为傅里叶变换的图像创建掩码,还有np.fft.fftfreq函数,它会给出傅里叶变换中像素的频率。我使用以下方法获得给定图像形状的每个像素的频率平方:

代码语言:javascript
运行
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2015-08-16 21:54:12

枕头使用numpy数组IIRC。Scipy有一些映像功能,但在加载和保存映像方面却很差。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32040198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档