def findMaxWithGFit(img, sigma=0.5, win=11): ''' find sub-pixel peaks from input img using n-dimensional Guassian fitting sigma: scaler or [simgaZ,sigmaY..] window: a window where the Guassian fit is performed on return [v, zyx, sigma] ''' vzyx = N.array(U.findMax(img)) ndim = img.ndim try: ret, check = imgFit.fitGaussianND(img, vzyx[-ndim:], sigma, win) except IndexError: # too close to the edge imgFit.fitFailedAppend("at %s" % str(vzyx[-ndim:])) sigma = imgFit._scalerToSeq(sigma, ndim) return vzyx[0:1], vzyx[-ndim:], list(sigma) if check == 5 or N.any(ret[2:2 + ndim] > (vzyx[-ndim:] + win)) or N.any( ret[2:2 + ndim] < (vzyx[-ndim:] - win)): imgFit.fitFailedAppend("at %s, %s, check=%i" % (str(vzyx[-ndim:]), str(ret), check)) sigma = imgFit._scalerToSeq(sigma, ndim) return [vzyx[0:1], vzyx[-ndim:], sigma] #x= (vzyx[0:1] + [vzyx[-ndim:]] + [sigma]) # print x # return x else: v = ret[1] zyx = ret[2:2 + ndim] sigma = ret[2 + ndim:2 + ndim * 2] return [v, zyx, sigma]
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 findMaxWithGFit(img, sigma=0.5, win=11): ''' find sub-pixel peaks from input img using n-dimensional Guassian fitting sigma: scaler or [simgaZ,sigmaY..] window: a window where the Guassian fit is performed on return [v, zyx, sigma] ''' vzyx = N.array(U.findMax(img)) ndim = img.ndim try: ret, check = imgFit.fitGaussianND(img, vzyx[-ndim:], sigma, win) except IndexError: # too close to the edge imgFit.fitFailedAppend("at %s" % str(vzyx[-ndim:])) sigma = imgFit._scalerToSeq(sigma, ndim) return vzyx[0:1], vzyx[-ndim:], list(sigma) if check == 5 or N.any(ret[2:2+ndim] > (vzyx[-ndim:] + win)) or N.any(ret[2:2+ndim] < (vzyx[-ndim:] - win)): imgFit.fitFailedAppend("at %s, %s, check=%i" % (str(vzyx[-ndim:]), str(ret), check)) sigma = imgFit._scalerToSeq(sigma, ndim) return [vzyx[0:1], vzyx[-ndim:], sigma] #x= (vzyx[0:1] + [vzyx[-ndim:]] + [sigma]) # print x # return x else: v = ret[1] zyx = ret[2:2+ndim] sigma = ret[2+ndim:2+ndim*2] return [v,zyx,sigma]
def _findMaxXcor(c, win, gFit=True, niter=2): if gFit: for i in range(niter): v, zyx, s = findMaxWithGFit(c, win=win + (i * 2)) if v: if N.any(zyx > N.array(c.shape)) or N.any(zyx < 0): vzyx = N.array(U.findMax(c)) #continue v = vzyx[0] zyx = vzyx[-c.ndim:] + 0.5 # pixel center s = 2.5 else: break if not v: v = U.findMax(c)[0] else: vzyx = U.findMax(c) v = vzyx[0] zyx = N.array(vzyx[-c.ndim:]) + 0.5 # pixel center s = 2.5 return v, zyx, s
def _findMaxXcor(c, win, gFit=True, niter=2): if gFit: for i in range(niter): v, zyx, s = findMaxWithGFit(c, win=win+(i*2)) if v: if N.any(zyx > N.array(c.shape)) or N.any(zyx < 0): vzyx = N.array(U.findMax(c))#continue v = vzyx[0] zyx = vzyx[-c.ndim:] + 0.5 # pixel center s = 2.5 else: break if not v: v = U.findMax(c)[0] else: vzyx = U.findMax(c) v = vzyx[0] zyx = N.array(vzyx[-c.ndim:]) + 0.5 # pixel center s = 2.5 return v, zyx, s
def findMaxWithGFitAll(img, thre=0, sigma_peak=0.5, win=11, mask_npxls=3): """ find peaks until either 1. any pxls becomes below thre 2. the same peak was found as the maximum mask_npxls: number of pixels to mask when Gaussian fitting failed return poslist """ img = img.copy() imgFit.fitFailedClear() ndim = img.ndim sigma_peak = imgFit._scalerToSeq(sigma_peak, ndim) poses = [] vzyx = U.findMax(img) while vzyx[0] > thre: prev = vzyx try: ret, check = imgFit.fitGaussianND(img, vzyx[-ndim:], sigma_peak, win) except IndexError: # too close to the edge imgFit.fitFailedAppend("at %s" % str(vzyx[-ndim:])) mask_value(img, vzyx[-ndim:], r=mask_npxls, value=img.min()) poses.append(list(vzyx)[0:1] + [vzyx[-ndim:]] + [list(sigma_peak)]) if check == 5: imgFit.fitFailedAppend("at %s, %s, check=%i" % (str(vzyx[-ndim:]), str(ret), check)) mask_value(img, vzyx[-ndim:], r=mask_npxls, value=img.min()) poses.append(list(vzyx)[0:1] + [vzyx[-ndim:]] + [sigma_peak]) else: v = ret[1] zyx = ret[2:2+ndim] if N.any(N.abs(zyx - vzyx[1:]) > win/2.):#zyx < 0 or zyx > img.shape or ): mask_value(img, vzyx[-ndim:], r=mask_npxls, value=img.min()) poses.append(list(vzyx)[0:1] + [vzyx[-ndim:]] + [sigma_peak]) else: sigma = ret[2+ndim:2+ndim*2] mask_gaussianND(img, zyx, v, sigma) poses.append([v,zyx,sigma]) vzyx = U.findMax(img) if N.all(vzyx == prev): break return poses#, img
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] or rzy): #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.zeros_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.zeros_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_spline(a, tzyx=(0, 0, 0), r=0, mag=1, dzyx=(0, 0), rzy=0, mr=0, reshape=False, ncpu=1, **splinekwds): """ mag: scalar_for_yx or [y,x] or [z,y,x] mr: rotational direction of yx-zoom in degrees ncpu: no usage """ splinekwds['prefilter'] = splinekwds.get('prefilter', True) splinekwds['order'] = splinekwds.get('order', 3) ndim = a.ndim shape = N.array(a.shape, N.float32) tzyx = N.asarray(tzyx, N.float32) # rotation axis if ndim == 3: axes = (1, 2) else: axes = (1, 0) # magnification try: if len(mag) == 1: # same as scalar mag = [1] * (ndim - 2) + list(mag) * 2 else: mag = [1] * (ndim - 2) * (3 - len(mag)) + list(mag) except: # scalar -> convert to yx mag only mag = [1] * (ndim - 2) + ([mag] * ndim)[:2] mag = N.asarray(mag) try: dzyx = N.array([0] * (ndim - 2) * (3 - len(dzyx)) + list(dzyx)) except: # scalar pass if mr: a = U.nd.rotate(a, mr, axes=axes, reshape=reshape, **splinekwds) splinekwds['prefilter'] = False if N.any(dzyx): a = U.nd.shift(a, -dzyx, **splinekwds) splinekwds['prefilter'] = False if r: a = U.nd.rotate(a, -r, axes=axes, reshape=reshape, **splinekwds) splinekwds['prefilter'] = False if N.any(mag != 1): a = U.nd.zoom(a, zoom=mag, **splinekwds) splinekwds['prefilter'] = False if not reshape: dif = (shape - N.array(a.shape, N.float32)) / 2. mod = N.ceil(N.mod(dif, 1)) tzyx[-ndim:] -= (mod / 2.) if rzy and ndim >= 3: # is this correct?? havn't tried yet a = U.nd.rotate(a, -rzy, axes=(0, 1), reshape=reshape, **splinekwds) if N.any(dzyx): a = U.nd.shift(a, dzyx, **splinekwds) if mr: a = U.nd.rotate(a, -mr, axes=axes, reshape=reshape, **splinekwds) if reshape: a = U.nd.shift(a, tzyx[-ndim:], **splinekwds) else: tzyx0 = N.where(mag >= 1, tzyx[-ndim:], 0) if N.any(tzyx0[-ndim:]): a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds) if N.any(mag != 1) and not reshape: a = keepShape(a, shape, (dif, mod)) old = """ canvas = N.zeros(shape, a.dtype.type) #dif = (shape - N.array(a.shape, N.float32)) / 2 #mod = N.ceil(N.mod(dif, 1)) dif = N.where(dif > 0, N.ceil(dif), N.floor(dif)) # smaller aoff = N.where(dif < 0, 0, dif) aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)] # larger coff = N.where(dif > 0, 0, -dif) cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)] canvas[aslc] = a[cslc] a = canvas""" if not reshape: tzyx0 = N.where(mag < 1, tzyx[-ndim:], 0) if N.any(mag != 1): tzyx0[-ndim:] -= (mod / 2.) if N.any(tzyx0[-ndim:]): a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds) 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 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] or rzy): #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.zeros_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.zeros_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_spline(a, tzyx=(0,0,0), r=0, mag=1, dzyx=(0,0), rzy=0, mr=0, reshape=False, ncpu=1, **splinekwds): """ mag: scalar_for_yx or [y,x] or [z,y,x] mr: rotational direction of yx-zoom in degrees ncpu: no usage """ splinekwds['prefilter'] = splinekwds.get('prefilter', True) splinekwds['order'] = splinekwds.get('order', 3) ndim = a.ndim shape = N.array(a.shape, N.float32) tzyx = N.asarray(tzyx, N.float32) # rotation axis if ndim == 3: axes = (1,2) else: axes = (1,0) # magnification try: if len(mag) == 1: # same as scalar mag = [1] * (ndim-2) + list(mag) * 2 else: mag = [1] * (ndim-2) * (3-len(mag)) + list(mag) except: # scalar -> convert to yx mag only mag = [1] * (ndim-2) + ([mag] * ndim)[:2] mag = N.asarray(mag) try: dzyx = N.array([0] * (ndim-2) * (3-len(dzyx)) + list(dzyx)) except: # scalar pass if mr: a = U.nd.rotate(a, mr, axes=axes, reshape=reshape, **splinekwds) splinekwds['prefilter'] = False if N.any(dzyx): a = U.nd.shift(a, -dzyx, **splinekwds) splinekwds['prefilter'] = False if r: a = U.nd.rotate(a, -r, axes=axes, reshape=reshape, **splinekwds) splinekwds['prefilter'] = False if N.any(mag != 1): a = U.nd.zoom(a, zoom=mag, **splinekwds) splinekwds['prefilter'] = False if not reshape: dif = (shape - N.array(a.shape, N.float32)) / 2. mod = N.ceil(N.mod(dif, 1)) tzyx[-ndim:] -= (mod / 2.) if rzy and ndim >= 3: # is this correct?? havn't tried yet a = U.nd.rotate(a, -rzy, axes=(0,1), reshape=reshape, **splinekwds) if N.any(dzyx): a = U.nd.shift(a, dzyx, **splinekwds) if mr: a = U.nd.rotate(a, -mr, axes=axes, reshape=reshape, **splinekwds) if reshape: a = U.nd.shift(a, tzyx[-ndim:], **splinekwds) else: tzyx0 = N.where(mag >= 1, tzyx[-ndim:], 0) if N.any(tzyx0[-ndim:]): a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds) if N.any(mag != 1) and not reshape: a = keepShape(a, shape, (dif, mod)) old=""" canvas = N.zeros(shape, a.dtype.type) #dif = (shape - N.array(a.shape, N.float32)) / 2 #mod = N.ceil(N.mod(dif, 1)) dif = N.where(dif > 0, N.ceil(dif), N.floor(dif)) # smaller aoff = N.where(dif < 0, 0, dif) aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)] # larger coff = N.where(dif > 0, 0, -dif) cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)] canvas[aslc] = a[cslc] a = canvas""" if not reshape: tzyx0 = N.where(mag < 1, tzyx[-ndim:], 0) if N.any(mag != 1): tzyx0[-ndim:] -= (mod / 2.) if N.any(tzyx0[-ndim:]): a = U.nd.shift(a, tzyx0[-ndim:], **splinekwds) 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