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 _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 remap(img, mapy, mapx, interp=2): """ transform image using coordinate x,y Interpolation method: 0 = CV_INTER_NN nearest-neigbor interpolation 1 = CV_INTER_LINEAR bilinear interpolation (used by default) 2 = CV_INTER_CUBIC bicubic interpolation 3 = CV_INTER_AREA resampling using pixel area relation. It is the preferred method for image decimation that gives moire-free results. In terms of zooming it is similar to the CV_INTER_NN method return resulting array """ des = N.empty_like(img) # cv.fromarray: array can be 2D or 3D only if cv2.__version__.startswith('2'): cimg = cv.fromarray(img) cdes = cv.fromarray(des) cmapx = cv.fromarray(mapx.astype(N.float32)) cmapy = cv.fromarray(mapy.astype(N.float32)) cv.Remap(cimg, cdes, cmapx, cmapy, flags=interp+cv.CV_WARP_FILL_OUTLIERS) else: cimg = img cdes = des cmapx = mapx.astype(N.float32) cmapy = mapy.astype(N.float32) cdes = cv2.remap(cimg, cmapx, cmapy, interp) return N.asarray(cdes)
def logpolar(img, center=None, mag=1): des = N.empty_like(img) if center is None: center = N.divide(img.shape, 2) # cv.fromarray: array can be 2D or 3D only cimg = cv.fromarray(img) cdes = cv.fromarray(des) cv.LogPolar(cimg, cdes, tuple(center), mag)#, cv.CV_WARP_FILL_OUTLIERS) return N.array(cdes)
def shiftZ(af): """ af: 3D array in fourier space return Z shifted array """ nz = af.shape[0] cz = nz // 2 bf = N.empty_like(af) bf[cz:] = af[:(nz-cz)] bf[:(nz-cz)] = af[cz:] return bf
def shiftZ(af): """ af: 3D array in fourier space return Z shifted array """ nz = af.shape[0] cz = nz // 2 bf = N.empty_like(af) bf[cz:] = af[:(nz - cz)] bf[:(nz - cz)] = af[cz:] return bf
def logpolar(image, center=None, angles=None, radii=None): """Return log-polar transformed image and log base.""" shape = image.shape if center is None: center = shape[0] / 2, shape[1] / 2 if angles is None: angles = shape[0] if radii is None: radii = shape[1] theta = N.zeros((angles, radii), dtype=N.float64) theta.T[:] = -N.linspace(0, N.pi, angles, endpoint=False) #d = radii d = N.hypot(shape[0] - center[0], shape[1] - center[1]) log_base = 10.0**(N.log10(d) / (radii)) radius = N.empty_like(theta) radius[:] = N.power(log_base, N.arange(radii, dtype=N.float64)) - 1.0 x = radius * N.sin(theta) + center[0] y = radius * N.cos(theta) + center[1] output = N.zeros_like(x) ndii.map_coordinates(image, [x, y], output=output) return output, log_base
def logpolar(image, center=None, angles=None, radii=None): """Return log-polar transformed image and log base.""" shape = image.shape if center is None: center = shape[0] / 2, shape[1] / 2 if angles is None: angles = shape[0] if radii is None: radii = shape[1] theta = N.zeros((angles, radii), dtype=N.float64) theta.T[:] = -N.linspace(0, N.pi, angles, endpoint=False) #d = radii d = N.hypot(shape[0]-center[0], shape[1]-center[1]) log_base = 10.0 ** (N.log10(d) / (radii)) radius = N.empty_like(theta) radius[:] = N.power(log_base, N.arange(radii, dtype=N.float64)) - 1.0 x = radius * N.sin(theta) + center[0] y = radius * N.cos(theta) + center[1] output = N.zeros_like(x) ndii.map_coordinates(image, [x, y], output=output) return output, log_base
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), rzy=0, mr=0, ncpu=1, **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) #b2d = N.empty_like(a[0]) #dzyx = N.asarray(dzyx) #tzyx = N.asarray(tzyx) #tzyx[-2:] += dzyx[-2:] # mag 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 # vertical axis if ndim == 3 and (magz != 1 or tzyx[-3]): #print 'vertical' at = a.T # zyx -> xyz mag = 1 anismag = magz canvas = N.empty_like(at) old = """ 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))""" if ndim == 3: # and ncpu > 1 and mp: ret = ppro.pmap(_doBilinear2D, at, ncpu, (0, tzyx[-3]), rzy, mag, anismag, (0, dzyx[-3])) for z, a2d in enumerate(ret): canvas[z] = a2d else: target = N.empty_like(a.T[0]) #N.ascontiguousarray(b2d.T) for z, a2d in enumerate(at): canvas[z] = _doBilinear2D(a2d, (0, tzyx[-3]), rzy, mag, anismag, (0, dzyx[-3]), b2d=target) a = canvas.T #return a # Horizontal axis mag = magy anismag = magx / magy oldcode = """ 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, (-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 N.any(tzyx[-2:]) or mr: U.trans2d(target, temp, (tzyx[-1], tzyx[-2], mr, 1, 0, 1)) else: temp[:] = target[:] a[z] = temp[:]""" if ndim == 3 and ncpu > 1 and mp: ret = ppro.pmap(_doBilinear2D, a, ncpu, tzyx[-2:], r, mag, anismag, dzyx[-2:], mr) for z, a2d in enumerate(ret): a[z] = a2d else: b2d = N.empty_like(a[0]) for z, a2d in enumerate(a): a[z] = _doBilinear2D(a2d, tzyx[-2:], r, mag, anismag, dzyx[-2:], mr, b2d) if ndim == 2: a = a[0] return a
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 arr_invert(arr): canvas = N.empty_like(arr) canvas[:] = U.max(arr) return canvas - arr
def trans3D_bilinear(a, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0,0), rzy=0, mr=0, ncpu=1, **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) #b2d = N.empty_like(a[0]) #dzyx = N.asarray(dzyx) #tzyx = N.asarray(tzyx) #tzyx[-2:] += dzyx[-2:] # mag 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 # vertical axis if ndim == 3 and (magz != 1 or tzyx[-3]): #print 'vertical' at = a.T # zyx -> xyz mag = 1 anismag = magz canvas = N.empty_like(at) old=""" 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))""" if ndim == 3:# and ncpu > 1 and mp: ret = ppro.pmap(_doBilinear2D, at, ncpu, (0,tzyx[-3]), rzy, mag, anismag, (0,dzyx[-3])) for z, a2d in enumerate(ret): canvas[z] = a2d else: target = N.empty_like(a.T[0])#N.ascontiguousarray(b2d.T) for z, a2d in enumerate(at): canvas[z] = _doBilinear2D(a2d, (0,tzyx[-3]), rzy, mag, anismag, (0,dzyx[-3]), b2d=target) a = canvas.T #return a # Horizontal axis mag = magy anismag = magx / magy oldcode=""" 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, (-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 N.any(tzyx[-2:]) or mr: U.trans2d(target, temp, (tzyx[-1], tzyx[-2], mr, 1, 0, 1)) else: temp[:] = target[:] a[z] = temp[:]""" if ndim == 3 and ncpu > 1 and mp: ret = ppro.pmap(_doBilinear2D, a, ncpu, tzyx[-2:], r, mag, anismag, dzyx[-2:], mr) for z, a2d in enumerate(ret): a[z] = a2d else: b2d = N.empty_like(a[0]) for z, a2d in enumerate(a): a[z] = _doBilinear2D(a2d, tzyx[-2:], r, mag, anismag, dzyx[-2:], mr, b2d) if ndim == 2: a = a[0] return a
def trans3D_affine(arr, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0,0), rzy=0, ncpu=NCPU, order=ORDER):#**kwds): """ return array """ dtype = arr.dtype.type arr = arr.astype(N.float32) ndim = arr.ndim if ndim == 2: arr = arr.reshape((1,)+arr.shape) elif ndim == 3: if len(tzyx) < ndim: tzyx = (0,)*(ndim-len(tzyx)) + tuple(tzyx) if len(dzyx) < ndim: dzyx = (0,)*(ndim-len(dzyx)) + tuple(dzyx) dzyx = N.asarray(dzyx) magz = 1 try: if len(mag) == 3: magz = mag[0] mag = mag[1:] except TypeError: pass if ndim == 3 and (magz != 1 or tzyx[-3]): #print magz, arr.shape # because, mergins introduced after 2D transformation may interfere the result of this vertical transform, vertical axis was processed first, since rzy is 0 usually. arrT = arr.T # zyx -> xyz magzz = (1,magz) canvas = N.empty_like(arrT) tzy = (0,tzyx[-3]) dzy = (dzyx[-2], dzyx[-3]) if ncpu > 1 and mp: ret = ppro.pmap(_dothat, arrT, ncpu, tzy, rzy, magzz, dzy, order) for x, a in enumerate(ret): canvas[x] = a else: for x, a in enumerate(arrT): canvas[x] = _dothat(a, tzy, rzy, magzz, dzy, order) arr = canvas.T #del arrT if N.any(tzyx[-2:]) or r or N.any(mag): #print ndim, arr.shape canvas = N.empty_like(arr) if ndim == 3 and ncpu > 1 and mp: # dividing XY into pieces did not work for rotation and magnification # here parallel processing is done section-wise since affine works only for 2D ret = ppro.pmap(_dothat, arr, ncpu, tzyx[-2:], r, mag, dzyx[-2:], order) for z, a in enumerate(ret): canvas[z] = a else: for z, a in enumerate(arr): canvas[z] = _dothat(a, tzyx[-2:], r, mag, dzyx[-2:], order) if ndim == 2: canvas = canvas[0] arr = canvas if dtype in (N.int, N.uint8, N.uint16, N.uint32): arr = N.where(arr < 0, 0, arr) return arr.astype(dtype)
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 arr_invert(arr): canvas = N.empty_like(arr) canvas[:] = U.max(arr) return canvas - arr