def copyImages(configFile, destination, onlyImage=True, prefix=''): #Copys all images in configFile to destination folder #Original folder will be used as prefix #set onlyImage to False to also copy _mod.tif and dots.csv files if not os.path.isdir(destination): os.mkdir(destination) config = file_utils.file2dict(configFile, ',\t') folder = os.path.split(configFile)[0] originImages = [ x + '.tif' for x in config['ROUTE'] if not x.endswith('_dupl') ] destinationImages = [ prefix + x.replace('/', '__') for x in originImages ] #Subfolders names are used as prefix seperated by __ _copyFilesFromList(folder, originImages, destination, destinationImages) if not onlyImage: originMod = [ x + '_mod.tif' for x in config['ROUTE'] if os.path.isfile(os.path.join(folder, x) + '_mod.tif') ] originDots = [ x + 'dots.csv' for x in config['ROUTE'] if os.path.isfile(os.path.join(folder, x) + 'dots.csv') ] destinationMod = [prefix + x.replace('/', '__') for x in originMod] destinationDots = [prefix + x.replace('/', '__') for x in originMod] _copyFilesFromList(folder, originMod, destination, destinationMod) _copyFilesFromList(folder, originDots, destination, destinationDots)
def copyTestImages(all_configs, sourcefolder, destinationfolder, modsuffix=''): #Get the names of individual image files imgFiles = file_utils.file2dict(all_configs, ';')['Name'] for f in imgFiles: iPath = os.path.join(os.path.split(all_configs)[0], f[0:-3] + 'csv') images = file_utils.file2dict(iPath, ';') imgs = images['OriginalRoute'] types = images['Type'] imgs = [i for i, t in zip(imgs, types) if t == 'test'] sc = [os.path.join(sourcefolder, i) for i in imgs] dst = [ os.path.join(destinationfolder, os.path.split(i)[1]) for i in imgs ] for s, d in zip(sc, dst): copyfile(s + '.tif', d + '.tif') copyfile(s + '_mod.tif', d + '_mod' + modsuffix + '.tif')
def getCoordinatesFromConfig(configFile, outputPath=False, downscale_factor=False): config = file_utils.file2dict(configFile, ',\t') folder = os.path.split(configFile)[0] images = [os.path.join(folder, x) + '_mod.tif' for x in config['ROUTE']] return getCoordinatesFromFiles(images, outputPath, downscale_factor)
def duplicateImage(configFile, index, copyMod=False, duplicate_suffix='_dupl'): '''Duplicates image with index and adds duplicate to Darea project''' #index starts from 0 images = file_utils.file2dict(configFile, ',\t') imbasepath = os.path.join(os.path.dirname(configFile), images['ROUTE'][index]) if 'ANGLES' in images and images['SELECTED'] != 'NaN': isSerEM = True else: isSerEM = False modbasepath = imbasepath while imbasepath.endswith('_dupl'): imbasepath = imbasepath[:-5] impath = imbasepath if isSerEM else imbasepath + '.tif' assert os.path.isfile(impath) #Find a path to copy the image to, by appending duplicat_suffix until it is not already taken cppath = imbasepath + duplicate_suffix while os.path.relpath(cppath, os.path.dirname(configFile)) in images['ROUTE']: cppath += duplicate_suffix cproute = os.path.relpath( cppath, os.path.dirname(configFile)) #Will be written to config cproute = cproute.replace('\\', '/') #Deal with windows issues if copyMod and os.path.isfile(modbasepath + '_mod.tif'): #Also duplicate _mod image if wanted and exists. copyfile(modbasepath + '_mod.tif', cppath + '_mod.tif') if copyMod and isSerEM: i = 1 while os.path.isfile(f'{modbasepath}_mod_{i}.tif'): copyfile(f'{modbasepath}_mod_{i}.tif', f'{cppath}_mod_{i}.tif') i += 1 if copyMod and os.path.isfile(modbasepath + 'dots.csv'): #Also duplicate dots file copyfile(modbasepath + 'dots.csv', cppath + 'dots.csv') if copyMod and isSerEM: i = 1 while os.path.isfile(f'{modbasepath}dots_{i}.csv'): copyfile(f'{modbasepath}dots_{i}.csv', f'{cppath}dots_{i}.csv') i += 1 #copyfile(impath,cppath+'.tif') #don't copy image to save space for k in images: lst = images[k][0:index + 1] if k == 'ROUTE': lst.append(cproute) else: lst.append(images[k][index]) lst += images[k][index + 1:] images[k] = lst file_utils.dict2file(configFile, images, ',\t')
def removeImage(configFile, index): '''Removes image with index from Darea project''' #index starts from 0 if type(index) == int: index = [index] elif type(index) == list: index.sort(reverse=True) else: print( 'Index needs to be an integer or a list of integers. Got {} with type {}' .format(index, type(index))) return images = file_utils.file2dict(configFile, ',\t') for idx in index: for i in images: del (images[i][idx]) file_utils.dict2file(configFile, images, ',\t')
def addImages(configFile, imagepaths, actualConfig=False): #configFile is the one modified, but if actualConfig is provided, #Relative path is taken from actualConfig print(configFile) print(imagepaths) print(actualConfig) images = file_utils.file2dict(configFile, ',\t') imagepaths = [os.path.splitext(i)[0] for i in imagepaths] for i in imagepaths: print(i) if actualConfig: common_pref = os.path.commonprefix([i, actualConfig]) im = os.path.relpath(i, common_pref) else: common_pref = os.path.commonprefix([i, configFile]) im = os.path.relpath(i, common_pref) print(im) if im not in images['ROUTE']: images['ROUTE'].append(im) images['PIXELSIZE'].append('NaN') images['GROUP'].append(os.path.split(im)[0]) file_utils.dict2file(configFile, images, ',\t')
def copyImagesfromChooser(destination, choiceFile, choicesToCopy, keepStructure=False, file_ext='.tif'): ''' Copys files chosen with matlab filechooser to destination folder destination: destination Folder choiceFile: Path to choice file made with matlab gpdq filechooser choicesToCopy: List of strings specifying the choices which images should be copied keepStructure: keeps Original folder structure. If False, will modify file names, numbering then will be 1.tif, 2.tif ...; If numbered files already at destination, will increment numbers rather than overwriting files file_ext: fileextenstion of images (in choice file, no fileextensions are saved) ''' file_utils.safe_mkdir(destination) choices = file_utils.file2dict(choiceFile, delimiter='\t') filesToCopy = [ f + file_ext for i, f in enumerate(choices['Image']) if choices['Choice'][i] in choicesToCopy ] print(filesToCopy) if not filesToCopy: return if keepStructure: #Needs to be implemented Still pass else: nr_start = 1 + getMaxIntFromNumberedFiles(destination, file_ext) fileDests = [ os.path.join(destination, str(nr_start + i) + file_ext) for i, _ in enumerate(filesToCopy) ] for x, y in zip(filesToCopy, fileDests): shutil.copyfile(x, y)
def changeSelectedAngle(configFile, index, newValue): images = file_utils.file2dict(configFile, ',\t') images['SELECTED'][index] = str(newValue) file_utils.dict2file(configFile, images, ',\t')
def changeScales(configFile, indeces, newValues): images = file_utils.file2dict(configFile, ',\t') for index, newValue in zip(indeces, newValues): images['PIXELSIZE'][index] = str(newValue) file_utils.dict2file(configFile, images, ',\t')
def compareDemVis(configFile, prediction_folder, line, plot=True, cropsize=768): config = file_utils.file2dict(configFile, ',\t') folder = os.path.split(configFile)[0] images = [os.path.join(folder, x) + '.tif' for x in config['ROUTE']] image = [f for f in images if f.endswith(line[0] + '.tif')][0] modImage = image[:-4] + '_mod.tif' prediction = os.path.join(prediction_folder, os.path.split(image)[1]) img = cv2.cvtColor(cv2.imread(image), cv2.COLOR_RGB2GRAY) if os.path.isfile(modImage): modImg = cv2.cvtColor(cv2.imread(modImage), cv2.COLOR_RGB2GRAY) else: modImg = np.ones(img.shape, dtype=np.uint8) * np.iinfo(np.uint8).max if os.path.isfile(modImage[0:-4] + '_morph.tif'): morphModImg = invertImage( cv2.cvtColor(cv2.imread(modImage[0:-4] + '_morph.tif'), cv2.COLOR_RGB2GRAY)) else: morphModImg = None pred = invertImage(cv2.cvtColor(cv2.imread(prediction), cv2.COLOR_RGB2GRAY)) if os.path.isfile(prediction[0:-4] + '_morph.tif'): morphPred = invertImage( cv2.cvtColor(cv2.imread(prediction[0:-4] + '_morph.tif'), cv2.COLOR_RGB2GRAY)) else: morphPred = None if pred.shape != img.shape: #Upscale prediction pred = cv2.resize(pred, img.shape, cv2.INTER_NEAREST) if morphPred is not None: morphPred = cv2.resize(morphPred, img.shape, cv2.INTER_NEAREST) try: cropY1 = max(0, round(line[1][0] - cropsize / 2)) cropY2 = min(img.shape[1], round(line[1][0] + cropsize / 2)) cropX1 = max(0, round(line[1][1] - cropsize / 2)) cropX2 = min(img.shape[0], round(line[1][1] + cropsize / 2)) except: print(line) raise plotted_images = [ img[cropX1:cropX2, cropY1:cropY2], modImg[cropX1:cropX2, cropY1:cropY2] ] if morphModImg is not None: plotted_images.append(morphModImg[cropX1:cropX2, cropY1:cropY2]) else: plotted_images.append(None) plotted_images.append(pred[cropX1:cropX2, cropY1:cropY2]) if morphPred is not None: plotted_images.append(morphPred[cropX1:cropX2, cropY1:cropY2]) else: plotted_images.append(None) if not plot: return plotted_images plt.figure() plt.imshow(plotted_images[0], cmap='gray') for i, x in enumerate(plotted_images): if x is None: continue plt.figure() plt.imshow(x, cmap='gray', vmin=0, vmax=255)