def _run_interface(self, runtime): args = {} for key in [k for k,_ in self.inputs.items() if k not in BaseInterfaceInputSpec().trait_names()]: value = getattr(self.inputs, key) if isdefined(value): if key in ['mean_volume', 'reference_volume']: nii = nb.load(value) value = nii.get_data() args[key] = value brain_mask = compute_mask(**args) self._brain_mask_path = os.path.abspath("brain_mask.nii") nb.save(nb.Nifti1Image(brain_mask.astype(np.uint8), nii.get_affine()), self._brain_mask_path) return runtime
def _run_interface(self, runtime): from nipy.labs.mask import compute_mask args = {} for key in [k for k, _ in self.inputs.items() if k not in BaseInterfaceInputSpec().trait_names()]: value = getattr(self.inputs, key) if isdefined(value): if key in ["mean_volume", "reference_volume"]: nii = nb.load(value) value = nii.get_data() args[key] = value brain_mask = compute_mask(**args) _, name, ext = split_filename(self.inputs.mean_volume) self._brain_mask_path = os.path.abspath("%s_mask.%s" % (name, ext)) nb.save(nb.Nifti1Image(brain_mask.astype(np.uint8), nii.get_affine()), self._brain_mask_path) return runtime
def inter_run_mask(fnames, hist_m=.3, hist_M=.8): """ create the inter runs mask : - first create a mask of all constant voxels on axis 3 - second create a mask of the mean - """ EPSFCT = 10000. # factor: multiply numpy epsilon by it imgs = [nib.load(fn) for fn in fnames] # initialize mask to ones mask = np.ones(imgs[0].shape[:3]) for img in imgs: arrstd = np.asarray(img.get_data().std(axis=3)) mask = np.logical_and(mask, arrstd > EPSFCT*np.finfo(arrstd.dtype).eps) mask_mean = compute_mask(img.get_data().mean(axis=3), None, \ m=hist_m, M=hist_M, cc=True, opening=2, exclude_zeros=False) mask = np.logical_and(mask, mask_mean) return mask
def _run_interface(self, runtime): args = {} for key in [k for k, _ in self.inputs.items() if k not in BaseInterfaceInputSpec().trait_names()]: value = getattr(self.inputs, key) if isdefined(value): if key in ['mean_volume', 'reference_volume']: nii = nb.load(value) value = nii.get_data() args[key] = value brain_mask = compute_mask(**args) _, name, ext = split_filename(self.inputs.mean_volume) self._brain_mask_path = os.path.abspath("%s_mask.%s" % (name, ext)) nb.save(nb.Nifti1Image(brain_mask.astype(np.uint8), nii.get_affine()), self._brain_mask_path) return runtime
def create_mask_pass1(img): """ This is the first pass in creating a mask. The skimage.exposure is used as it helps to get rid of the glue around the tissue. This also calls the compute_mask method which is part of the nipy package which you will need to install from github :param img: the raw image :return: the 1st pass of the image """ img = exposure.adjust_log(img, 1) img = exposure.adjust_gamma(img, 2) mask = compute_mask(img, m=0.2, M=0.9, cc=False, opening=2, exclude_zeros=True) mask = mask.astype(int) mask[mask==0] = 0 mask[mask==1] = 255 kernel = np.ones((5, 5), np.uint8) mask = cv2.dilate(mask.astype(np.uint8), kernel, iterations=2) mask = mask.astype(np.uint8) return mask
def _run_interface(self, runtime): from nipy.labs.mask import compute_mask args = {} for key in [ k for k, _ in list(self.inputs.items()) if k not in BaseInterfaceInputSpec().trait_names() ]: value = getattr(self.inputs, key) if isdefined(value): if key in ["mean_volume", "reference_volume"]: value = np.asanyarray(nb.load(value).dataobj) args[key] = value brain_mask = compute_mask(**args) _, name, ext = split_filename(self.inputs.mean_volume) self._brain_mask_path = os.path.abspath("%s_mask.%s" % (name, ext)) nb.save( nb.Nifti1Image(brain_mask.astype(np.uint8), nii.affine), self._brain_mask_path, ) return runtime
cv2.imwrite(ch1_outpath, ch1_img.astype(np.uint8)) cv2.imwrite(ch2_outpath, ch2_img.astype(np.uint8)) cv2.imwrite(ch3_outpath, ch3_img.astype(np.uint8)) """ max_width = 2000 max_height = 1000 for channel in [2]: INPUT = os.path.join(BASEINPUT, f'CH{channel}/thumbnail') files = sorted(os.listdir(INPUT)) for file in tqdm(files): infile = os.path.join(INPUT, file) img = io.imread(infile) img, _ = remove_strip(img) mask = compute_mask(img, m=0.2, M=0.9, cc=False, opening=2, exclude_zeros=True) mask = mask.astype(int) mask[mask==0] = 0 mask[mask==1] = 255 kernel = np.ones((5, 5), np.uint8) mask = cv2.dilate(mask.astype(np.uint8), kernel, iterations=2) mask = mask.astype(np.uint8) fixed = cv2.bitwise_and(img, img, mask=mask) fixed = equalized(fixed) BASEOUTPUT = os.path.join(BASEINPUT, f'CH{channel}/thumbnail_cleaned') outpath = os.path.join(BASEOUTPUT, file) os.makedirs(BASEOUTPUT, exist_ok=True) fixed = np.rot90(fixed, 3, axes=(1,0)) fixed = np.flip(fixed, axis=1) fixed = place_image(fixed, file, max_width, max_height, 0)