def main(): if len(sys.argv) != 2: print """ Usage: python img2spline.py [path_to_img] \n""" sys.exit(-1) else: path_to_img = sys.argv[1] img = Image.open(path_to_img).convert('L') # RGB -> [0..255] print "Image was opened and converted to grayscale." img.show() width, height = img.size data = np.array(list(img.getdata()), dtype=int) data = data.reshape((height, width)) print "Data was extracted." # Start plotting original surface of image fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') ax = fig.add_subplot(111) ax.invert_yaxis() x = range(0, width) y = range(0, height) # rev_y = range(height-1, -1, -1) # reverse y X, Y = np.meshgrid(x, y) print Y r_stride = 1 + width / 20 c_stride = 1 + height / 20 # ax.plot_surface(X, Y, data, rstride=r_stride, cstride=c_stride) mappable = ax.pcolor(X, Y, data) plt.colorbar(mappable) ax.set_title("Original grayscale image") ax.set_xlabel('Width (px)') ax.set_ylabel('Height (px)') plt.draw() # Finish plotting original surface of image # 2D Interpolation here spline = RectBivariateSpline(x, y, data) print spline.get_coeffs() plt.show()
def main(): if len(sys.argv) != 2: print """ Usage: python img2spline.py [path_to_img] \n""" sys.exit(-1) else: path_to_img = sys.argv[1] img = Image.open(path_to_img).convert('L') # RGB -> [0..255] print "Image was opened and converted to grayscale." img.show() width, height = img.size data = np.array(list(img.getdata()), dtype=int) data = data.reshape((height, width)) print "Data was extracted." # Start plotting original surface of image fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') ax = fig.add_subplot(111) ax.invert_yaxis() x = range(0, width) y = range(0, height) # rev_y = range(height-1, -1, -1) # reverse y X, Y = np.meshgrid(x, y) print Y r_stride = 1 + width / 20 c_stride = 1 + height / 20 # ax.plot_surface(X, Y, data, rstride=r_stride, cstride=c_stride) mappable = ax.pcolor(X, Y, data) plt.colorbar(mappable) ax.set_title("Original grayscale image") ax.set_xlabel('Width (px)') ax.set_ylabel('Height (px)') plt.draw() # Finish plotting original surface of image # 2D Interpolation here spline = RectBivariateSpline(x, y, data) print spline.get_coeffs() plt.show()
def _approx(fmapnii, s=14.): """ Slice-wise approximation of a smooth 2D bspline credits: http://scipython.com/book/chapter-8-scipy/examples/two-dimensional-interpolation-\ with-scipyinterpolaterectbivariatespline/ """ from scipy.interpolate import RectBivariateSpline from builtins import str, bytes if isinstance(fmapnii, (str, bytes)): fmapnii = nb.load(fmapnii) if not isinstance(s, (tuple, list)): s = np.array([s] * 2) data = fmapnii.get_data() zooms = fmapnii.header.get_zooms() knot_decimate = np.floor(s / np.array(zooms)[:2]).astype(np.uint8) knot_space = np.array(zooms)[:2] * knot_decimate xmax = 0.5 * data.shape[0] * zooms[0] ymax = 0.5 * data.shape[1] * zooms[1] x = np.arange(-xmax, xmax, knot_space[0]) y = np.arange(-ymax, ymax, knot_space[1]) x2 = np.arange(-xmax, xmax, zooms[0]) y2 = np.arange(-ymax, ymax, zooms[1]) coeffs = [] nslices = data.shape[-1] for k in range(nslices): data2d = data[..., k] data2dsubs = data2d[::knot_decimate[0], ::knot_decimate[1]] interp_spline = RectBivariateSpline(x, y, data2dsubs) data[..., k] = interp_spline(x2, y2) coeffs.append(interp_spline.get_coeffs().reshape(data2dsubs.shape)) # Save smoothed data hdr = fmapnii.header.copy() caff = fmapnii.affine datanii = nb.Nifti1Image(data.astype(np.float32), caff, hdr) # Save bspline coeffs caff[0, 0] = knot_space[0] caff[1, 1] = knot_space[1] coeffnii = nb.Nifti1Image(np.stack(coeffs, axis=2), caff, hdr) return datanii, coeffnii
def _approx(fmapnii, s=14.): """ Slice-wise approximation of a smooth 2D bspline credits: http://scipython.com/book/chapter-8-scipy/examples/two-dimensional-interpolation-\ with-scipyinterpolaterectbivariatespline/ """ from scipy.interpolate import RectBivariateSpline from builtins import str, bytes if isinstance(fmapnii, (str, bytes)): fmapnii = nb.load(fmapnii) if not isinstance(s, (tuple, list)): s = np.array([s] * 2) data = fmapnii.get_data() zooms = fmapnii.header.get_zooms() knot_decimate = np.floor(s / np.array(zooms)[:2]).astype(np.uint8) knot_space = np.array(zooms)[:2] * knot_decimate xmax = 0.5 * data.shape[0] * zooms[0] ymax = 0.5 * data.shape[1] * zooms[1] x = np.arange(-xmax, xmax, knot_space[0]) y = np.arange(-ymax, ymax, knot_space[1]) x2 = np.arange(-xmax, xmax, zooms[0]) y2 = np.arange(-ymax, ymax, zooms[1]) coeffs = [] nslices = data.shape[-1] for k in range(nslices): data2d = data[..., k] data2dsubs = data2d[::knot_decimate[0], ::knot_decimate[1]] interp_spline = RectBivariateSpline(x, y, data2dsubs) data[..., k] = interp_spline(x2, y2) coeffs.append(interp_spline.get_coeffs().reshape(data2dsubs.shape)) # Save smoothed data hdr = fmapnii.header.copy() caff = fmapnii.affine datanii = nb.Nifti1Image(data.astype(np.float32), caff, hdr) # Save bspline coeffs caff[0, 0] = knot_space[0] caff[1, 1] = knot_space[1] coeffnii = nb.Nifti1Image(np.stack(coeffs, axis=2), caff, hdr) return datanii, coeffnii
#now try to solve by grad descent kacq2d=zeros(kfull.shape,complex) kacq2d[t2_array,t1_array]=kacq imgus=ifft2(ifftshift(kacq2d)) imgwvlt=pywt.wavedec2(abs(imgus),wavelet='db4',mode='symmetric') Ny,Nx=imgus.shape #fit smoothed phase, evaluate phase basis functions ph_splfit=RectBivariateSpline(arange(imgus.shape[0]),arange(imgus.shape[1]),1.0*i_unwrap_2d(angle(imgus)),s=1.7e5) spl_tx=ph_splfit.tck[0] spl_ty=ph_splfit.tck[1] spl_kx,spl_ky=ph_splfit.degrees spl_ncoeffs=ph_splfit.get_coeffs().shape[0] Sb=zeros([spl_ncoeffs,Nx,Ny],float) spl_BVS=BivariateSpline() spl_BVS.degrees=(spl_kx,spl_ky) for j in range(spl_ncoeffs): spl_coeffs=zeros(spl_ncoeffs,float) spl_coeffs[j]=1.0 spl_BVS.tck=(spl_tx,spl_ty,spl_coeffs) spl_bfunc=spl_BVS.ev(arange(Nx*Ny)%Ny,arange(Nx*Ny)/Ny) spl_bfunc.shape=(Nx,Ny) Sb[j,:,:]=transpose(spl_bfunc[:,:]) #unfortunately, spline funcs use transposed orientation convention phest=ph_splfit.get_coeffs() p0=append(compose_vec_from_wvlt(imgwvlt,imgus.shape,wavelet='db4',mode='symmetric'),phest) l_w=[0.2]
#now try to solve by grad descent kacq2d = zeros(kfull.shape, complex) kacq2d[t2_array, t1_array] = kacq imgus = ifft2(ifftshift(kacq2d)) imgwvlt = pywt.wavedec2(abs(imgus), wavelet='db4', mode='symmetric') Ny, Nx = imgus.shape #fit smoothed phase, evaluate phase basis functions ph_splfit = RectBivariateSpline(arange(imgus.shape[0]), arange(imgus.shape[1]), 1.0 * i_unwrap_2d(angle(imgus)), s=1.7e5) spl_tx = ph_splfit.tck[0] spl_ty = ph_splfit.tck[1] spl_kx, spl_ky = ph_splfit.degrees spl_ncoeffs = ph_splfit.get_coeffs().shape[0] Sb = zeros([spl_ncoeffs, Nx, Ny], float) spl_BVS = BivariateSpline() spl_BVS.degrees = (spl_kx, spl_ky) for j in range(spl_ncoeffs): spl_coeffs = zeros(spl_ncoeffs, float) spl_coeffs[j] = 1.0 spl_BVS.tck = (spl_tx, spl_ty, spl_coeffs) spl_bfunc = spl_BVS.ev(arange(Nx * Ny) % Ny, arange(Nx * Ny) / Ny) spl_bfunc.shape = (Nx, Ny) Sb[j, :, :] = transpose( spl_bfunc[:, :] ) #unfortunately, spline funcs use transposed orientation convention phest = ph_splfit.get_coeffs() p0 = append(