Is there any python (numpy) synonym to i-variables (e.g., irow, iradius, itheta, etc.) in DM scripting? -
i work electron microscopy image processing digital microscopy (dm) scripting, , start learn python because of wider versatility, rich open libraries, , cross-platform ability.
does know if there similar tools in python (numpy) index 2d (image), or 3d (spectrum image) arrays similar dm's i-variables?
the i-variables briefly introduced on page 11 of tutorial dm-scripting: http://portal.tugraz.at/portal/page/portal/files/felmi/images/dm-script/dm-basic-scripting_bs.pdf
they easy way index image-like 2d or 3d object, convenient image processing, e.g., generate mask functions
for example, following dm-script
image t1 := realimage ("test1", 4, 5, 5) image t2 := realimage ("test2", 4, 5, 5) image t3 := realimage ("test3", 4, 5, 5) t1 = irow // value in each pixel equals row index t2 = iradius // value in each pixel equals radius // (i.e., distance center pixel) t3 = itheta // value in each pixel quals angle (radian) // center pixel (i.e., angle in polar representation) t1.showimage(); t2.showimage(); t3.showimage()
result in following images (expressed here in spreadsheet, or matrix form):
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
the equivalent way in numpy use numpy.indices
function.
so same in numpy did in dm (remember coordinates (y,x) in numpy, , irow, say, index of y-coordinates):
from __future__ import division import numpy np test1 = np.random.random((5,5)) irow, icol = np.indices(test1.shape) # use iradius , itheta need 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)
then
>>> 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]])
it's possible using mgrid
, ogrid
functions. mgrid
return coordinates in populated array (the same shape source image, identical numpy.indices) while ogrid
returns row , column vectors of right shape automatically broadcast correctly.
if you're using create masks fourier-transformed images there's np.fft.fftfreq function give frequencies of pixels in fourier transform. use following frequency squared @ each pixel given image shape:
def get_freq_squared(shape, packed_complex=true): """return frequency squared n-d array. returned image match shape, if packed_complex true last dimension treated 0,f,...,nf rather 0,f,..., nf,...,-f """ vecs = [np.fft.fftfreq(s, 1.0 / s) ** 2 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)
Comments
Post a Comment