def all_kernels(kdim, writefile=True, makeimage=False, vb=False): ## Get files -- path hard-coded allpngs = numpy.sort(file_seek("../Data/GPSF/", "snap*d0_CFHTLS_03_g*.png")) ## Collect pairs of files for i in range(0, len(allpngs), 2): ## Extract image info image1 = png_pix(allpngs[i]) image2 = png_pix(allpngs[i + 1]) ## Cut out extraneous white pixels image1 = pngcropwhite(image1) image2 = pngcropwhite(image2) ## Deconvolve A = get_kernel(image1, image2, kdim) B = get_kernel(image2, image1, kdim) ##------------------------------------------------------------ ## Write kernels to file and make images ## Gaussian to PSF outfile = os.path.splitext(allpngs[i])[0][:-4] + "_GausstoPSF_" + str( A.shape[0]) + ".krn" f = open(outfile, "w") f.write( "## Convolution kernel taking 2D Gaussian to PSFEx image\n\n\n") writearray(f, A, True) if vb: print "DeconvolveToTargetPSF.py: kernel written to", outfile ## PSF to Gaussian outfile = os.path.splitext(allpngs[i])[0][:-4] + "_PSFtoGauss_" + str( B.shape[0]) + ".krn" f = open(outfile, "w") f.write( "## Convolution kernel taking PSFEx image to 2D Gaussian\n\n\n") writearray(f, B, True) if vb: print "DeconvolveToTargetPSF.py: kernel written to", outfile print "\n" ##------------------------------------------------------------ return
def all_kernels(kdim, writefile=True,makeimage=False, vb=False): ## Get files allpngs = numpy.sort(file_seek("../Data/GPSF/","snap*d0_CFHTLS_03_g*.png")) ## Collect pairs of files for i in range (0,len(allpngs),2): ## Extract image info image1 = png_pix(allpngs[i]) image2 = png_pix(allpngs[i+1]) ## Cut out extraneous white pixels image1 = pngcropwhite(image1) image2 = pngcropwhite(image2) ## Deconvolve A=get_kernel(image1,image2, kdim) B=get_kernel(image2,image1, kdim) ##------------------------------------------------------------ ## Write kernels to file and make images ## Gaussian to PSF outfile=os.path.splitext(allpngs[i])[0][:-4]+"_GausstoPSF_"+str(A.shape[0])+".krn" f=open(outfile,"w") f.write("## Convolution kernel taking 2D Gaussian to PSFEx image\n\n\n") writearray(f,A,True) if vb: print "DeconvolveToTargetPSF.py: kernel written to",outfile ## PSF to Gaussian outfile=os.path.splitext(allpngs[i])[0][:-4]+"_PSFtoGauss_"+str(B.shape[0])+".krn" f=open(outfile,"w") f.write("## Convolution kernel taking PSFEx image to 2D Gaussian\n\n\n") writearray(f,B,True) if vb: print "DeconvolveToTargetPSF.py: kernel written to",outfile print "\n" ##------------------------------------------------------------ return
def reconvolve(image1, image2, kernel): ## Read in images & kernel and process into shape arr1 = pngcropwhite(png_pix(image1)) kernel = numpy.loadtxt(kernel) newdim = numpy.array(arr1.shape)-numpy.array(kernel.shape)+1 arr2 = shave(newdim, pngcropwhite(png_pix(image2))) ## Reconvolved image -- should be integer array recon = numpy.around(convolve(arr1, kernel, mode="valid")) ## Residue should be 0 residue = recon-arr2 ## Visualise residue from pylab import imshow, show, colorbar imshow(residue) #colorbar() show() return
def deconvolve_image(imagefile, kernel, vb=False): ##------------------------------------------------------------ ## Read in image ## Determine image file type and get pixels imgext = os.path.splitext(imagefile)[1] if imgext==".fits": imgarr = fits_pix(imagefile) elif imgext==".png": imgarr = png_pix(imagefile) ## Doesn't work ## For some reason the image is flipped at this point, so un-flip imgarr = imgarr[::-1,:] ## Filter out noise imgarr[imgarr<cutoff] = 0.0 ##------------------------------------------------------------ ## Read in kernel ## Distinguish between array-kernel and file-kernel if type(kernel) is str: ## Ensure the right kernel file has been selected if "PSFtoGauss" in kernel: kernel = numpy.loadtxt(kernel) else: print "DeconvolveToTargetPSF.py: deconvolve_image: wrong kernel file. Abort." return elif type(kernel) is numpy.array: pass ## Kernel dimensions kernel_h,kernel_w = kernel.shape # Should also check match with imagefile # ##------------------------------------------------------------ ## Compute linalg objects from images ## Honest dimensions for scene scene_dim = numpy.array(imgarr.shape)-numpy.array(kernel.shape)+1 scene_siz = scene_dim[0]*scene_dim[1] ##------------------------------------------------------------ convmeth = "scipy" ## Manual convolution if convmeth=="manual": ## 1D array for SCENE (convolved with Gaussian) g_sc = numpy.empty(imgarr.size)#scene_siz ## 1D array for IMAGE stride = imgarr.shape[0] imgarr = imgarr.flatten() ##------------------------------------------------------------ ## Manual matrix product ## Initialise kernel "vector" len_krn_lin = (stride)*(kernel_h-1)+kernel_w ## Still keeps a lot of zeros krn_lin = numpy.zeros(len_krn_lin) ## Loop over slices in the kernel image for j in range (kernel_h): startcol = j*stride krn_lin[startcol:startcol+kernel_w] = kernel[j,:] t0 = time.time() ## Perform linalg product ## i labels the scene pixel and the slice in the original for i in range (scene_siz): imageslice = imgarr[i:i+len_krn_lin] g_sc[i] = numpy.dot(krn_lin,imageslice) if vb: print "DeconvolveToTargetPSF.py: deconvolve_image: vector multiplication took",\ round(time.time()-t0,2),"seconds." ## Delete spurious elements (from overlapping) i=len(g_sc) while i>=0: if i%stride+kernel_w > stride: g_sc = numpy.delete(g_sc,i) i-=1 ## Delete spurious elements from declaring it too big g_sc = numpy.delete(g_sc,slice(scene_siz-len(g_sc)-1,-1)) if scene_siz-len(g_sc): print "LINE #"+str(lineno())+": size discrepancy" ##------------------------------------------------------------ elif convmeth=="scipy": t0=time.time() ## Do convolution using scipy imgarr = numpy.array(imgarr, dtype="float64") g_sc = convolve(imgarr, kernel, mode="valid") if vb: print "DeconvolveToTargetPSF.py: deconvolve_image: SciPy convolution took",\ round(time.time()-t0,2),"seconds." else: print "LINE #"+str(lineno())+": convmeth error, abort." return ##------------------------------------------------------------ ## Reshape if g_sc.shape[0]!=scene_dim[0] or g_sc.shape[1]!=scene_dim[1]: if vb: print "DeconvolveToTargetPSF.py: deconvolve_image: reshaping." try: g_sc = g_sc.reshape(scene_dim) except ValueError: print "DeconvolveToTargetPSF.py: deconvolve_image: output has wrong shape. Investigate." ##------------------------------------------------------------ ## Filter out (computer) noise g_sc[g_sc<cutoff] = 0.0 ## Outfile name imagedir,imagename = os.path.split(imagefile) info = imagename[imagename.find("CFHT"):imagename.find("sci")+3] outfile = imagedir+"/gDeconvolved_"+info ## Rescale (linear stretch) if 1: ## Scaling parameters vmin,vmax = stretch_params(g_sc) ## Stretch g_sc = linear_rescale(g_sc, vmin, vmax) ## Modify filename outfile = outfile+"_rescaled" ## Delete pre-existing images if os.path.isfile(outfile+".fits"): os.remove(outfile+".fits") if os.path.isfile(outfile+".png"): os.remove(outfile+".png") ## Save image as FITS and as PNG makeFITS(outfile+".fits", g_sc) scipy.misc.imsave(outfile+".png", g_sc) if vb: print "DeconvolveToTargetPSF.py: deconvolve_image: image saved to",outfile+".fits" return None