def _correctBadPix(self,badmask): for i in np.arange(self.stack.shape[2]): plane = self.stack[:,:,i] maskedImage = np.ma.array(plane, mask=badmask.mask) NANMask = maskedImage.filled(np.NaN) self.stack[:,:,i] = inpaint.replace_nans(NANMask, 5, 0.5, 1, 'idw') plane = self.ustack[:,:,i] maskedImage = np.ma.array(plane, mask=badmask.mask) NANMask = maskedImage.filled(np.NaN) self.ustack[:,:,i] = inpaint.replace_nans(NANMask, 5, 0.5, 1, 'idw')
def inpaint_array(inputArray, mask, **kwargs): maskedImg = np.ma.array(inputArray, mask=mask) NANMask = maskedImg.filled(np.NaN) badArrays, num_badArrays = ndimage.label(mask) # data_slices = ndimage.find_objects(badArrays) filled = inpaint.replace_nans(NANMask, **kwargs) return filled
def inpaint(self): """ Replace masked-out elements in an array using an iterative image inpainting algorithm. """ import inpaint filled = inpaint.replace_nans( np.ma.filled(self.raster_data, np.NAN).astype(np.float32), 3, 0.01, 2) self.raster_data = np.ma.masked_invalid(filled)
def callInpaint(img, mask, outputFilename, i): maskedImg = np.ma.array(img, mask = mask) NANMask = maskedImg.filled(np.NaN) filled = inpaint.replace_nans(NANMask) #outputFilename = utils.createOutputFilename(sdssFilename, dataDir) hdu = pyfits.PrimaryHDU(filled) if os.path.exists(outputFilename): print outputFilename outputFilename = outputFilename+"B" hdu.writeto(outputFilename) else: hdu.writeto(outputFilename)
def inpaint_array(inputArray, mask): # maskedImg = np.ma.array(inputArray, mask=mask) # NANMask = maskedImg.filled(np.NaN) # badArrays, num_badArrays = ndimage.label(mask) # data_slices = ndimage.find_objects(badArrays) filled = replace_nans( inputArray * mask, max_iter=20, tol=0.05, kernel_radius=5, kernel_sigma=2, method='idw') return filled
def runInpainting(maskFile, listFile, dataDir, ID, log): maskFilename = utils.getMask(maskFile, ID) sdssFilename = GalaxyParameters.getSDSSUrl(listFile, dataDir, ID) outputFilename = utils.createOutputFilename(sdssFilename) sdssImage = pyfits.open(sdssFilename) imageData = sdssImage[0].data imageData = imageData - 1000 #soft bias subtraction, comment out if needed head = sdssImage[0].header maskFile = pyfits.open(maskFilename) mask = maskFile[0].data maskedImg = np.ma.array(imageData, mask = mask) NANMask = maskedImg.filled(np.NaN) filled = inpaint.replace_nans(NANMask, 15, 0.1, 2, 'idw') hdu = pyfits.PrimaryHDU(filled, header = head) try: hdu.writeto(dataDir+'/filled/'+outputFilename) except IOError: errmsg = 'file already exists!', dataDir+'/filled/'+outputFilename print errmsg log.append(errmsg) return log
def inpaint(self): """ Replace masked-out elements in an array using an iterative image inpainting algorithm. """ import inpaint filled = inpaint.replace_nans(np.ma.filled(self.raster_data, np.NAN).astype(np.float32), 3, 0.01, 2) self.raster_data = np.ma.masked_invalid(filled)
def saveDataClearMask(edfimagepath, newpath, stem, first, last, medianename, mask, ndigits=4, extension='edf', doinpaint=False): """ Save new EDF files with the median and mask removed edfimagepath: Path to the EDF images. The median image is assumed to be in the same directory newpath: Path to save the new EDF images. stem: Stem for EDF images. first: First image number last: Last image number medianename: Name of median EDF file (in full, with extension) mask: mask data ndigits: Number of digits for EDF file numbering. extension: EDF file extension. doinpaint: if set to true, fills diamond mask with inpainting. If not set, diamond mask is filled with median value """ if ((not (os.path.isdir(newpath))) or (not (os.path.exists(newpath)))): print("ERROR! %s is not a directory or does not exist.\nAborting." % newpath) return if (os.path.samefile(edfimagepath, newpath)): print( "ERROR!\nImages are read from %s.\nNew EDF should be saved in %s.\nThis will destroy the original data.\nAborting" % (edfimagepath, newpath)) return print("Reading median image") # Read median image imagename = os.path.join(edfimagepath, medianename) medianeIm = fabio.edfimage.edfimage() medianeIm.read(imagename) medianeData = medianeIm.getData().astype('float32') if (doinpaint): print("Filling mask with inpainting") else: print("Filling mask with median value") # Loop on images and test median substraction for i in range(first, last + 1): # Read image data format = "%s%0" + str(ndigits) + "d." + extension image = format % (stem, i) imagename = os.path.join(edfimagepath, image) print("Reading and processing " + imagename) im = fabio.edfimage.edfimage() im.read(imagename) data = im.getData().astype('float32') header = im.getHeader() # Removing median image data = data - medianeData # Removing anything below 0 data = data.clip(min=0) meanI = data.mean() medianI = numpy.median(data) maxI = data.max() minI = data.min() xsize = im.getDim1() ysize = im.getDim2() # Preparing mask thismask = mask[i - first] thismask = thismask.astype( numpy.float32 ) # New versions of python do not like resizing with integer... maskscaled = scipy.misc.imresize(thismask, (xsize, ysize), interp='nearest', mode='F') # Creating data under mask using linear interpolation or inpainting # Need to create a list of points for which we have data # Actually, gave up, fill with median value! idx = (maskscaled > 0) if (doinpaint): # Creating data under mask using inpainting # We rescale the image and inpaint on a smaller version. Before reducing, remove extreme intensity values (it works better) datacopy = data.copy() datacopy = datacopy.clip(0., 10. * meanI) datareduced = scipy.misc.imresize( datacopy, (thismask.shape[0], thismask.shape[1]), interp='nearest', mode='F') # Setting mask data as NaN and call for inpainting. Parameters have been set from trial and error idx2 = (thismask > 0) datareduced[idx2] = numpy.NaN result0 = inpaint.replace_nans(datareduced, max_iter=20, tol=1., kernel_radius=2, kernel_sigma=5, method='idw') # Rescaling inpainted image and set new values at mask positions result0 = scipy.misc.imresize(result0, (xsize, ysize), interp='nearest', mode='F') data[idx] = result0[idx] else: # Fill maslwith median value! data[idx] = medianI # Save new data newname = os.path.join(newpath, image) print("Saving new EDF with median and mask removed in " + newname) im = fabio.edfimage.edfimage() im.read(imagename) im.setData(data.astype('uint32')) im.setHeader(header) im.save(newname)
NANMask = maskedImg.filled(np.NaN) badArrays, num_badArrays = sp.ndimage.label(mask) print num_badArrays data_slices = sp.ndimage.find_objects(badArrays) #data_slices = np.ma.extras.notmasked_contiguous(maskedImg) import inpaint #def replace_nans(array, max_iter, tol,kernel_size=1,method='localmean'): #data = np.array([[11,22.0,353],[1,np.NaN,343],[11,0,343]]) filled = inpaint.replace_nans(NANMask, 5, 0.5, 2, 'idw') hdu = pyfits.PrimaryHDU(filled, header = head) hdu.writeto('IC3376R_masked.fits') #hdu = pyfits.PrimaryHDU(NANMask) #hdu.writeto('NanMask.fits') #invdisttree = idw.Invdisttree(maskedImg, maskedImg) #print data_slices #for slice in data_slices: # dy, dx = slice # coords = float(dy.start), float(dy.stop), float(dx.start), float(dx.stop) # print 'slice', coords # submask = cutoutMask[(dy.start):(dy.stop),(dx.start):(dx.stop)] # subarray = cutout[(dy.start):(dy.stop+1),(dx.start):(dx.stop+1)]