def rot_shift2D(img, angle = 0.0, sx = 0.0, sy = 0.0, mirror = 0, scale = 1.0, interpolation_method = None, mode = "background"): """ rotate/shift image using: 1. linear interpolation 2. quadratic interpolation 3. gridding mode specifies what will be put in corners, should they stick out: background - leave original values cyclic - use pixels from the image using wrap-around transformation """ if scale == 0.0 : ERROR("0 scale factor encountered","rot_shift2D", 1) if(interpolation_method): use_method = interpolation_method else: use_method = interpolation_method_2D if(use_method == "linear" and mode == "cyclic"): T = Transform({'type': 'SPIDER', 'psi': angle, 'tx': sx, 'ty': sy, 'scale':scale}) img = img.rot_scale_trans(T) if mirror: img.process_inplace("xform.mirror", {"axis":'x'}) return img elif(use_method == "linear" and mode == "background"): T = Transform({'type': 'SPIDER', 'psi': angle, 'tx': sx, 'ty': sy, 'scale':scale}) img = img.rot_scale_trans_background(T) if mirror: img.process_inplace("xform.mirror", {"axis":'x'}) return img elif(use_method == "quadratic" and mode == "cyclic"): img = img.rot_scale_trans2D(angle, sx, sy, scale) if mirror: img.process_inplace("xform.mirror", {"axis":'x'}) return img elif(use_method == "quadratic" and mode == "background"): img = img.rot_scale_trans2D_background(angle, sx, sy, scale) if mirror: img.process_inplace("xform.mirror", {"axis":'x'}) return img elif(use_method == "gridding" and mode == "cyclic"): # previous rtshg from math import radians from fundamentals import prepi o, kb = prepi(img) # gridding rotation o = o.rot_scale_conv_new(radians(angle), sx, sy, kb, scale) if mirror: o.process_inplace("xform.mirror", {"axis":'x'}) return o elif(use_method == "gridding" and mode == "background"): # previous rtshg from math import radians from fundamentals import prepi o, kb = prepi(img) # gridding rotation o = o.rot_scale_conv_new_background(radians(angle), sx, sy, kb, scale) if mirror: o.process_inplace("xform.mirror", {"axis":'x'}) return o elif(use_method == "ftgridding"): # previous rtshg from fundamentals import gridrot_shift2D img = gridrot_shift2D(img, angle, sx, sy, scale) if mirror: img.process_inplace("xform.mirror", {"axis":'x'}) return img else: ERROR("rot_shift_2D interpolation method is incorrectly set", "rot_shift_2D", 1)
def prep_refim_gridding(refim, wr, numr, mode = "F"): from fundamentals import prepi nx = refim.get_xsize() ny = refim.get_ysize() cnx = nx//2+1 cny = ny//2+1 #precalculate rings temp,kb = prepi(refim) crefim = Util.Polar2Dmi(temp, cnx, cny, numr, mode, kb) Util.Normalize_ring(crefim, numr) Util.Frngs(crefim, numr) Util.Applyws(crefim, numr, wr) return crefim,kb
def prep_refim_gridding(refim, wr, numr, mode="F"): from fundamentals import prepi nx = refim.get_xsize() ny = refim.get_ysize() cnx = nx // 2 + 1 cny = ny // 2 + 1 #precalculate rings temp, kb = prepi(refim) crefim = Util.Polar2Dmi(temp, cnx, cny, numr, mode, kb) Util.Normalize_ring(crefim, numr) Util.Frngs(crefim, numr) Util.Applyws(crefim, numr, wr) return crefim, kb
def rtshg(image, angle = 0.0, sx=0.0, sy=0.0, scale = 1.0): """ Name rtshg - rotate and shift a 2D image using the gridding method Input image: a 2D input image alpha: rotation angle sx: x shift (default = 0.0) sy: y shift (default = 0.0) scale: magnification change (default = 1.0) Output the output rotated and shifted image """ from math import radians o,kb = prepi(image) # gridding rotation return o.rot_scale_conv_new(radians(angle), sx, sy, kb, scale)
def resample(img, sub_rate=0.5): """ resample image based on the value of sub_rate. the input image can be either 2D image or 3D volume. sub_rate < 1.0, subsampling the image. sub_rate > 1.0, upsampling the image using new gridding interpolation. fit_to_fft will change the ouput image size to an fft_friendly size """ from fundamentals import subsample from utilities import get_pixel_size, set_pixel_size if type(img) == str: from utilities import get_image img = get_image(img) nx = img.get_xsize() ny = img.get_ysize() nz = img.get_zsize() if( ny == 1): ERROR("Only 2D or 3D images allowed","resample",1) if sub_rate == 1.0: return img.copy() elif sub_rate < 1.0: e = subsample(img, sub_rate) else: # sub_rate>1 new_nx = int(nx*sub_rate+0.5) new_ny = int(ny*sub_rate+0.5) if nz==1: new_nz = 1 else: new_nz = int(ny*sub_rate+0.5) if ( nx!=ny and nz==1 ): nn = max(new_nx, new_ny) e = Util.pad(img, nn, nn, 1, 0, 0, 0, "circumference") e, kb = prepi(e) e = Util.window( e.rot_scale_conv_new(0.0, 0.0, 0.0, kb, sub_rate), new_nx, new_ny, 1, 0,0,0) elif ((nx!=ny or nx!=nz or ny!=nz) and nz>1): nn = max(new_nx, new_ny,new_nz) e = Util.pad(img, nn, nn, nn, 0, 0, 0, "circumference") e, kb = prepi3D(e) e = Util.window( e.rot_scale_conv_new_3D(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, kb, sub_rate), new_nx, new_ny, new_nz, 0,0,0) else: if nz==1: e, kb = prepi(Util.pad(img, new_nx, new_ny, 1, 0, 0, 0, "circumference")) e = e.rot_scale_conv_new(0.0, 0.0, 0.0, kb, sub_rate) else: e, kb = prepi3D(Util.pad(img, new_nx, new_ny, new_nz, 0, 0, 0, "circumference")) e = e.rot_scale_conv_new_3D(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, kb, sub_rate) # Automatically adjust pixel size for ctf parameters from utilities import get_pixel_size, set_pixel_size apix = get_pixel_size(e) apix /= sub_rate set_pixel_size(e, apix) cc = e.get_attr_default("xform.projection", None) if cc: cp = cc.get_params("spider") cp["tx"] *= sub_rate cp["ty"] *= sub_rate from utilities import set_params_proj set_params_proj(e, [cp["phi"], cp["theta"], cp["psi"], -cp["tx"], -cp["ty"]]) # have to invert as set inverts them again cc = e.get_attr_default("xform.align2d", None) if cc: cp = cc.get_params("2D") cp["tx"] *= sub_rate cp["ty"] *= sub_rate from utilities import set_params2D set_params2D(e, [cp["alpha"], cp["tx"], cp["ty"], cp["mirror"], cp["scale"]]) return e