def rotateIndices2DNew(shape, rot, orig=None, dtype=N.float64): """ shape: 2D rot: anti-clockwise orig: (y, x) return: yi, xi """ # FIX ME Rot is something wrong!! 081027 shape = N.asarray(shape, N.int) if orig is None: y, x = shape / 2. else: y, x = orig print(y, x) if not rot: yi, xi = N.indices(shape, dtype=N.float64) yi -= y - 0.5 # remove pix center xi -= x - 0.5 return yi, xi # twice as large window # mo = N.abs(N.mod(shape, 2) + [-1,-1]) s2 = shape * 2 #+ mo # always odd for even shape, even for odd shape yi, xi = N.indices(s2, dtype=N.float32) mm = N.ceil(shape / 2.) #(s2 -1 - shape)//2 # offset always int yi -= mm[0] xi -= mm[1] pxc = imgGeo.RotateXY((0.5, 0.5), rot) # remove pix center yi += pxc[0] xi += pxc[1] y0, x0 = shape / 2. #N.ceil(shape / 2) # img center yc = y0 - y # delta rotation center xc = x0 - x yi = U.trans2d(yi, None, (xc, yc, rot, 1, 0, 1)) xi = U.trans2d(xi, None, (xc, yc, rot, 1, 0, 1)) yi = U.trans2d(yi, None, (-xc, -yc, 0, 1, 0, 1)) xi = U.trans2d(xi, None, (-xc, -yc, 0, 1, 0, 1)) yi = yi.astype(dtype) xi = xi.astype(dtype) yi -= y xi -= x yi = imgFilters.cutOutCenter(yi, shape) xi = imgFilters.cutOutCenter(xi, shape) return yi, xi
def rotateIndices2DNew(shape, rot, orig=None, dtype=N.float64): """ shape: 2D rot: anti-clockwise orig: (y, x) return: yi, xi """ # FIX ME Rot is something wrong!! 081027 shape = N.asarray(shape, N.int) if orig is None: y, x = shape / 2. else: y, x = orig print(y,x) if not rot: yi,xi = N.indices(shape, dtype=N.float64) yi -= y - 0.5 # remove pix center xi -= x - 0.5 return yi,xi # twice as large window # mo = N.abs(N.mod(shape, 2) + [-1,-1]) s2 = shape * 2 #+ mo # always odd for even shape, even for odd shape yi, xi = N.indices(s2, dtype=N.float32) mm = N.ceil(shape / 2.)#(s2 -1 - shape)//2 # offset always int yi -= mm[0] xi -= mm[1] pxc = imgGeo.RotateXY((0.5,0.5), rot) # remove pix center yi += pxc[0] xi += pxc[1] y0, x0 = shape / 2. #N.ceil(shape / 2) # img center yc = y0 - y # delta rotation center xc = x0 - x yi = U.trans2d(yi, None, (xc,yc,rot,1,0,1)) xi = U.trans2d(xi, None, (xc,yc,rot,1,0,1)) yi = U.trans2d(yi, None, (-xc,-yc,0,1,0,1)) xi = U.trans2d(xi, None, (-xc,-yc,0,1,0,1)) yi = yi.astype(dtype) xi = xi.astype(dtype) yi -= y xi -= x yi = imgFilters.cutOutCenter(yi, shape) xi = imgFilters.cutOutCenter(xi, shape) return yi, xi
def _doBilinear2D(a2d, tyx=(0,0), r=0, mag=1, anismag=1, dyx=(0,0), mr=0, b2d=None): if b2d is None: b2d = N.empty_like(a2d) else: b2d = N.ascontiguousarray(b2d) a2d = a2d.copy() # otherwise, the following code will mess up the input if N.any(dyx[-2:]) or mr: temp = b2d target = a2d U.trans2d(target, temp, (dyx[-1], dyx[-2], mr, 1, 0, 1)) else: temp = a2d target = b2d # rot mag first to make consistent with affine magaxis = 1 # only this axis works if r or mag != 1 or anismag != 1: U.trans2d(temp, target, (0, 0, r, mag, magaxis, anismag)) else: target[:] = temp[:] # then translate tyx2 = N.array(tyx) # copy tyx2[-2:] -= dyx[-2:] if N.any(tyx2[-2:]) or mr: U.trans2d(target, temp, (tyx2[-1], tyx2[-2], -mr, 1, 0, 1)) else: temp[:] = target[:] #a[z] = temp[:] return temp
def trans3D_bilinear(a, tzyx=(0, 0, 0), r=0, mag=1, dzyx=(0, 0, 0), b=None, rzy=0, **kwds): """ magyx: scalar or [y,x] or [y,x, direction in degrees] """ a = a.copy() ndim = a.ndim if ndim == 2: a = a.reshape((1, ) + a.shape) try: if len(magyx) == 3: mr = magyx[-1] magyx = magyx[:2] else: mr = 0 except: mr = 0 if b is None: b2d = N.empty_like(a[0]) dzyx = N.asarray(dzyx) tzyx = N.asarray(tzyx) tzyx[-2:] += dzyx[-2:] magaxis = 1 # only this axis works magz = 1 try: if len(mag) == 3: magz, magy, magx = mag elif len(mag) == 2: magy, magx = mag else: magy = magx = mag[0] except: magy = magx = mag mag = magy anismag = magx / magy for z, a2d in enumerate(a): if N.any(dzyx[-2:]) or mr: temp = N.ascontiguousarray(b2d) target = N.ascontiguousarray(a2d) #U.trans2d(target, temp, (-dyx[1], -dyx[0], -mr, 1, 0, 1)) U.trans2d(target, temp, (-dzyx[-1], -dzyx[-2], -mr, 1, 0, 1)) else: temp = N.ascontiguousarray(a2d) target = N.ascontiguousarray(b2d) if r or mag != 1 or anismag != 1: U.trans2d(temp, target, (0, 0, r, mag, magaxis, anismag)) else: target[:] = temp[:] #if rzx: # is this correct?? havn't tried yet # target = U.nd.rotate(target, rzx, axes=(0,2), order=1, prefilter=False) if N.any(tzyx[-2:]) or mr: #N.any(dyx) or mr: #U.trans2d(target, temp, (dyx[1], dyx[0], mr, 1, 0, 1)) U.trans2d(target, temp, (tzyx[-1], tzyx[-2], mr, 1, 0, 1)) else: temp[:] = target[:] a[z] = temp[:] if ndim == 2: a = a[0] elif ndim == 3 and (magz != 1 or tzyx[-3]): at = a.T # zyx -> xyz mag = 1 #magz anismag = magz #magy / magz canvas = N.empty_like(at) target = b2d.T for x, a2d in enumerate(at): if dzyx[-3]: U.trans2d(a2d, target, (0, -dyzx[-3], 0, 1, 1, 1)) else: target = a2d canvas[x] = U.trans2d(target, None, (tzyx[-3], 0, rzy, mag, magaxis, anismag)) #canvas = canvas.T a = canvas.T return a
def rotateIndicesND(slicelist, dtype=N.float64, rot=0, mode=2): """ slicelist: even shape works much better than odd shape rot: counter-clockwise, xy-plane mode: testing different ways of doing, (1 or 2 and the same result) return inds, LD """ global INDS_DIC shape = [] LD = [] for sl in slicelist: if isinstance(sl, slice): shape.append(sl.stop - sl.start) LD.append(sl.start) shapeTuple = tuple(shape + [rot]) if shapeTuple in INDS_DIC: inds = INDS_DIC[shapeTuple] else: shape = N.array(shape) ndim = len(shape) odd_even = shape % 2 s2 = N.ceil(shape * (2**0.5)) if mode == 1: # everything is even s2 = N.where(s2 % 2, s2 + 1, s2) elif mode == 2: # even & even or odd & odd for d, s in enumerate(shape): if (s % 2 and not s2[d] % 2) or (not s % 2 and s2[d] % 2): s2[d] += 1 cent = s2 / 2. dif = (s2 - shape) / 2. dm = dif % 1 # print s2, cent, dif, dm slc = [Ellipsis] + [ slice(int(d), int(d) + shape[i]) for i, d in enumerate(dif) ] # This slice is float which shift array when cutting out!! s2 = tuple([int(ss) for ss in s2]) # numpy array cannot use used for slice inds = N.indices(s2, N.float32) ind_shape = inds.shape nz = N.product(ind_shape[:-2]) nsec = nz / float(ndim) if ndim > 2: inds = N.reshape(inds, (nz, ) + ind_shape[-2:]) irs = N.empty_like(inds) for d, ind in enumerate(inds): idx = int(d // nsec) c = cent[idx] if rot and inds.ndim > 2: U.trans2d(ind - c, irs[d], (0, 0, rot, 1, 0, 1)) irs[d] += c - dif[idx] else: irs[d] = ind - dif[idx] if len(ind_shape) > 2: irs = N.reshape(irs, ind_shape) irs = irs[slc] if mode == 1 and N.sometrue(dm): inds = N.empty_like(irs) # print 'translate', dm for d, ind in enumerate(irs): U.trans2d(ind, inds[d], (-dm[1], -dm[0], 0, 1, 0, 1)) else: inds = irs INDS_DIC[shapeTuple] = inds r_inds = N.empty_like(inds) for d, ld in enumerate(LD): r_inds[d] = inds[d] + ld return r_inds, LD
def trans3D_bilinear(a, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0,0), b=None, rzy=0, **kwds): """ magyx: scalar or [y,x] or [y,x, direction in degrees] """ a = a.copy() ndim = a.ndim if ndim == 2: a = a.reshape((1,)+a.shape) try: if len(magyx) == 3: mr = magyx[-1] magyx = magyx[:2] else: mr = 0 except: mr = 0 if b is None: b2d = N.empty_like(a[0]) dzyx = N.asarray(dzyx) tzyx = N.asarray(tzyx) tzyx[-2:] += dzyx[-2:] magaxis = 1 # only this axis works magz = 1 try: if len(mag) == 3: magz, magy, magx = mag elif len(mag) == 2: magy, magx = mag else: magy = magx = mag[0] except: magy = magx = mag mag = magy anismag = magx / magy for z, a2d in enumerate(a): if N.any(dzyx[-2:]) or mr: temp = N.ascontiguousarray(b2d) target = N.ascontiguousarray(a2d) #U.trans2d(target, temp, (-dyx[1], -dyx[0], -mr, 1, 0, 1)) U.trans2d(target, temp, (-dzyx[-1], -dzyx[-2], -mr, 1, 0, 1)) else: temp = N.ascontiguousarray(a2d) target = N.ascontiguousarray(b2d) if r or mag != 1 or anismag != 1: U.trans2d(temp, target, (0, 0, r, mag, magaxis, anismag)) else: target[:] = temp[:] #if rzx: # is this correct?? havn't tried yet # target = U.nd.rotate(target, rzx, axes=(0,2), order=1, prefilter=False) if N.any(tzyx[-2:]) or mr:#N.any(dyx) or mr: #U.trans2d(target, temp, (dyx[1], dyx[0], mr, 1, 0, 1)) U.trans2d(target, temp, (tzyx[-1], tzyx[-2], mr, 1, 0, 1)) else: temp[:] = target[:] a[z] = temp[:] if ndim == 2: a = a[0] elif ndim == 3 and (magz != 1 or tzyx[-3]): at = a.T # zyx -> xyz mag = 1#magz anismag = magz #magy / magz canvas = N.empty_like(at) target = b2d.T for x, a2d in enumerate(at): if dzyx[-3]: U.trans2d(a2d, target, (0, -dyzx[-3], 0, 1, 1, 1)) else: target = a2d canvas[x] = U.trans2d(target, None, (tzyx[-3], 0, rzy, mag, magaxis, anismag)) #canvas = canvas.T a = canvas.T return a
def rotateIndicesND(slicelist, dtype=N.float64, rot=0, mode=2): """ slicelist: even shape works much better than odd shape rot: counter-clockwise, xy-plane mode: testing different ways of doing, (1 or 2 and the same result) return inds, LD """ global INDS_DIC shape = [] LD = [] for sl in slicelist: if isinstance(sl, slice): shape.append(sl.stop - sl.start) LD.append(sl.start) shapeTuple = tuple(shape+[rot]) if shapeTuple in INDS_DIC: inds = INDS_DIC[shapeTuple] else: shape = N.array(shape) ndim = len(shape) odd_even = shape % 2 s2 = N.ceil(shape * (2**0.5)) if mode == 1: # everything is even s2 = N.where(s2 % 2, s2 + 1, s2) elif mode == 2: # even & even or odd & odd for d, s in enumerate(shape): if (s % 2 and not s2[d] % 2) or (not s % 2 and s2[d] % 2): s2[d] += 1 cent = s2 / 2. dif = (s2 - shape) / 2. dm = dif % 1 # print s2, cent, dif, dm slc = [Ellipsis] + [slice(int(d), int(d)+shape[i]) for i, d in enumerate(dif)] # This slice is float which shift array when cutting out!! s2 = tuple([int(ss) for ss in s2]) # numpy array cannot use used for slice inds = N.indices(s2, N.float32) ind_shape = inds.shape nz = N.product(ind_shape[:-2]) nsec = nz / float(ndim) if ndim > 2: inds = N.reshape(inds, (nz,)+ind_shape[-2:]) irs = N.empty_like(inds) for d, ind in enumerate(inds): idx = int(d//nsec) c = cent[idx] if rot and inds.ndim > 2: U.trans2d(ind - c, irs[d], (0,0,rot,1,0,1)) irs[d] += c - dif[idx] else: irs[d] = ind - dif[idx] if len(ind_shape) > 2: irs = N.reshape(irs, ind_shape) irs = irs[slc] if mode == 1 and N.sometrue(dm): inds = N.empty_like(irs) # print 'translate', dm for d, ind in enumerate(irs): U.trans2d(ind, inds[d], (-dm[1], -dm[0], 0, 1, 0, 1)) else: inds = irs INDS_DIC[shapeTuple] = inds r_inds = N.empty_like(inds) for d, ld in enumerate(LD): r_inds[d] = inds[d] + ld return r_inds, LD