def diff(img1, img2): img2 = match_histograms(img2, img1, multichannel=True) difference = ((img1 - img2) / (img1 + img2)) difference = (difference + 1) * 127 return np.concatenate((difference.astype(np.uint8), img1.astype( np.uint8), img2.astype(np.uint8)), axis=-1)
def make_red_cyan_qc_images(target: np.ndarray, specimen: np.ndarray, out_dir: Path, grey_cale_dir: Path, name: str, img_num: int, stage_id: str) -> List[np.ndarray]: """ Create a cyan red overlay Parameters ---------- target specimen` img_num A number to prefix onto the qc image so that when browing a folder the images will be sorteed Returns ------- 0: axial 1: coronal 2: sagittal """ oris = ['axial', 'coronal', 'sagittal'] def get_ori_dirs(root: Path): res = [] for ori_name in oris: dir_ = root / ori_name dir_.mkdir(exist_ok=True) res.append([dir_, ori_name]) return res if target.shape != specimen.shape: raise ValueError('target and specimen must be same shape') specimen = np.clip(specimen, 0, 255) specimen = rescale_intensity(specimen, out_range=MOVING_RANGE) def get_slices(img): slices = [] for ax in [0,1,2]: slices.append(np.take(img, img.shape[ax] // 2, axis=ax)) return slices t = get_slices(target) s = get_slices(specimen) # histogram match the specimen to the target s = [match_histograms(s_, reference=t_) for (s_, t_) in zip(s, t)] rc_oris = get_ori_dirs(out_dir) grey_oris = get_ori_dirs(grey_cale_dir) # put slices in folders by orientation for i in range(len(oris)): grey = s[i] rgb = red_cyan_overlay(s[i], t[i]) rgb = np.flipud(rgb) grey = np.flipud(grey) imsave(rc_oris[i][0] / f'{img_num}_{stage_id}_{name}_{rc_oris[i][1]}.png', rgb) imsave(grey_oris[i][0] / f'{img_num}_{stage_id}_{name}_{rc_oris[i][1]}.png', grey)
def swap_faces(img1, img2): # get landmark points from the images landmarks1 = landmarks_from_pil_image(img1) landmarks2 = landmarks_from_pil_image(img2) img1_index = random.randint(0, len(landmarks1) - 1) img2_index = random.randint(0, len(landmarks2) - 1) landmarks1 = landmarks1[img1_index] landmarks2 = landmarks2[random.randint(0, len(landmarks2) - 1)] bb = coords_from_pil_image(img1)[img1_index] bb = coords_from_pil_image(img2)[img2_index] # calculate transformation matrix to go from one set of points to the other trans_matrix1 = nudged.estimate(landmarks2, landmarks1).get_matrix() trans1 = transform.ProjectiveTransform(matrix=np.array(trans_matrix1)) # transform the images to be on top of each other img1_transformed = transform.warp( np.array(img1), trans1, output_shape=np.array(img2).shape[:2]).dot(255).astype('uint8') s = np.linspace(0, 2 * np.pi, 400) height = (bb.bottom() - bb.top()) rad_y = height / 2 * 1.2 x = (bb.left() + bb.right()) / 2 + (bb.right() - bb.left()) / 2 * np.cos(s) y = (bb.top() + bb.bottom()) / 2 + rad_y * np.sin(s) - height * 0.15 init = np.array([x, y]).T #init = init.dot(trans_matrix1) shape = active_contour(gaussian(rgb2gray(img1_transformed), 3), init, alpha=0.08, beta=1, gamma=0.001, max_iterations=height * 0.1, max_px_move=1) #shape = init output = Image.fromarray(img1_transformed) mask = Image.new('L', output.size) draw = ImageDraw.Draw(mask) draw.polygon([tuple(coord)[:2] for coord in shape], fill="white") #draw = ImageDraw.Draw(img2) #draw.polygon([tuple(coord)[:2] for coord in shape], outline="red") #return img2 # Match histograms on the selected areas hist_mask = Image.fromarray( morphology.dilation(np.array(mask), morphology.disk(int(height * 0.1)))) img2_histogram = generate_histogram(img2, hist_mask) img1_histogram = generate_histogram(Image.fromarray(img1_transformed), hist_mask) matched = Image.fromarray( match_histograms(np.asarray(img1_histogram), np.asarray(img2_histogram), multichannel=True)) mask = gaussian(np.array(mask), height * 0.1) mask = Image.fromarray(mask.dot(255).astype('uint8')) output = Image.composite(matched, img2, mask) return output
def full_validation(self): for i, row in self.df.iterrows(): ele = self.get_ele(row) tmp = {} dtce_metrics = self.get_metrics(ele['dtce'], ele['output']) iq_metrics = self.get_metrics(ele['dtce'], ele['das']) for key, value in iq_metrics.items(): tmp['iq_{}'.format(key)] = value for key, value in dtce_metrics.items(): tmp['{}'.format(key)] = value dtce = match_histograms(ele['dtce'], ele['das']) output = match_histograms(ele['output'], ele['das']) dtce_metrics = self.get_metrics(dtce, output) iq_metrics = self.get_metrics(dtce, output) for key, value in iq_metrics.items(): tmp['hm_iq_{}'.format(key)] = value for key, value in dtce_metrics.items(): tmp['hm_{}'.format(key)] = value tmp['filename'] = row.filename self.writer.writerow(tmp) print('{}/{} - {}'.format(i, len(self.df), row.filename))
def imgdiff(tile1, tile2, diff_path, save_path, data_path, img_path, msk_path, cloud_path, writer, width,height): xs = [piece.split('_')[4:5][0] for piece in os.listdir(os.path.join(data_path,tile1,img_path))] ys = [piece.split('_')[5:6][0].split('.')[0] for piece in os.listdir(os.path.join(data_path,tile1,img_path))] for i in range(len(xs)): if os.path.exists(os.path.join(data_path,tile1,img_path,tile1+'_'+xs[i]+'_'+ys[i]+'.tiff')) and os.path.exists(os.path.join(data_path,tile2,img_path,tile2+'_'+xs[i]+'_'+ys[i]+'.tiff')): img1,meta = readtiff( os.path.join(data_path,tile1,img_path,tile1+'_'+xs[i]+'_'+ys[i]+'.tiff') ) img2, _ = readtiff( os.path.join(data_path,tile2,img_path,tile2+'_'+xs[i]+'_'+ys[i]+'.tiff') ) msk1=imageio.imread( os.path.join(data_path,tile1,msk_path,tile1+'_'+xs[i]+'_'+ys[i]+'.png')) msk2=imageio.imread( os.path.join(data_path,tile2,msk_path,tile2+'_'+xs[i]+'_'+ys[i]+'.png')) cld1=io.imread( os.path.join(data_path,tile1,cloud_path,tile1+'_'+xs[i]+'_'+ys[i]+'.tiff')) cld2=io.imread( os.path.join(data_path,tile2,cloud_path,tile2+'_'+xs[i]+'_'+ys[i]+'.tiff')) else: continue if np.max(cld1)<0.2 and np.max(cld2)<0.2: img2 = match_histograms(img2, img1, multichannel=True) diff_img = diff(img1,img2, width,height) diff_msk = (np.abs(msk1-msk2)>0)*255 name = diff_path.split('/')[-1]+'_'+xs[i]+'_'+ys[i]+'.png' diff_msk = (gaussian_filter(diff_msk , 0.5)>0)*255 diff_msk = diff_msk.astype(np.uint8) diff_msk = cv2.resize(diff_msk, (height,width), interpolation = cv2.INTER_NEAREST) meta['width'] = width meta['height'] = height meta['count'] = diff_img.shape[2] with rs.open(os.path.join(diff_path, img_path, diff_path.split('/')[-1]+'_'+xs[i]+'_'+ys[i]+'.tiff'), 'w', **meta) as dst: for ix in range(diff_img.shape[2]): dst.write(diff_img[:, :, ix], ix + 1) dst.close() imageio.imwrite(os.path.join(diff_path, msk_path, diff_path.split('/')[-1]+'_'+xs[i]+'_'+ys[i]+'.png'), diff_msk) writer.writerow([ diff_path.split('/')[-1], diff_path.split('/')[-1], xs[i]+'_'+ys[i], int(diff_msk.sum()/255) ]) else: pass
def performHM(self, MRI_type='T2F'): filetree = _parse(self.data_path, MRI_type=MRI_type) # Flair Template flair_template_path = os.path.join(os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'files/Mean_Brain_Total_23_1mm_noVentr_NL_reg_edited.nii.gz') ref = sitk.ReadImage(flair_template_path) # Convert to array and flatten to 2D for histogram matching ref_arr = sitk.GetArrayFromImage(ref) ref_arr = ref_arr.reshape(-1, ref_arr.shape[2]) # Iterate through file tree for patient, dates in filetree.items(): print("Patient:", patient) # Iterate through all dates and perform histogram matching to template for date, files in dates.items(): print(date) for file_dir in files: print("file:",file_dir) file_mask_dir = file_dir.rsplit('/', 1)[0] + '/alpha_mask_brain_roi.nii.gz' # Read Image files and convert to arrays img = sitk.ReadImage(file_dir) mask = sitk.ReadImage(file_mask_dir) img_arr = sitk.GetArrayFromImage(img) mask_arr = sitk.GetArrayFromImage(mask) # Flatten to 2D for Histogram Matching img_arr_dim0 = img_arr.shape[0] img_arr = img_arr.reshape(-1, img_arr.shape[2]) # Histogram Matching matched = match_histograms(img_arr, ref_arr, multichannel=False) # Return image back to original shape matched = np.ceil(matched.reshape(img_arr_dim0, -1, img_arr.shape[1])) # Mask Background Values matched[mask_arr <= 0] = 0 # Copy Nifti file settings and save file new_nii = sitk.GetImageFromArray(matched) new_nii.CopyInformation(img) new_nii = sitk.Cast(new_nii, sitk.sitkFloat32) sitk.WriteImage(new_nii, file_dir)
def test_match_histograms(self, image, reference): """Assert that pdf of matched image is close to the reference's pdf for all channels and all values of matched""" # when matched = transform.match_histograms(image, reference, multichannel=True) matched_pdf = self._calculate_image_empirical_pdf(matched) reference_pdf = self._calculate_image_empirical_pdf(reference) # then for channel in range(len(matched_pdf)): reference_values, reference_quantiles = reference_pdf[channel] matched_values, matched_quantiles = matched_pdf[channel] for i, matched_value in enumerate(matched_values): closest_id = (np.abs(reference_values - matched_value)).argmin() assert_almost_equal(matched_quantiles[i], reference_quantiles[closest_id], decimal=1)
def test_match_histograms(self, image, reference): """Assert that pdf of matched image is close to the reference's pdf for all channels and all values of matched""" # when matched = transform.match_histograms(image, reference, multichannel=True) matched_pdf = self._calculate_image_empirical_pdf(matched) reference_pdf = self._calculate_image_empirical_pdf(reference) # then for channel in range(len(matched_pdf)): reference_values, reference_quantiles = reference_pdf[channel] matched_values, matched_quantiles = matched_pdf[channel] for i, matched_value in enumerate(matched_values): closest_id = (np.abs(reference_values - matched_value)).argmin() assert_almost_equal(matched_quantiles[i], reference_quantiles[closest_id], decimal=1)
def histogramMatching(): reference = data.coffee() image = data.chelsea() matched = match_histograms(image, reference, multichannel=True) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3), sharex=True, sharey=True) for aa in (ax1, ax2, ax3): aa.set_axis_off() ax1.imshow(image) ax1.set_title('Source') ax2.imshow(reference) ax2.set_title('Reference') ax3.imshow(matched) ax3.set_title('Matched') plt.tight_layout() plt.show()
def histogram_matching(image_left, image_right, verbose=1): """ Match the histogram of the left image to the right image and output the normalized left image verbose: if to show the figures of the histogram before and after normalization """ image = image_left reference = image_right matched = match_histograms(image, reference, multichannel=False) if verbose: plt.figure(figsize=(10, 2)) ax1 = plt.subplot(121) sns.distplot(image_left.ravel(), ax=ax1) plt.title('histogram of the left image before normalization') ax2 = plt.subplot(122) sns.distplot(image_right.ravel(), ax=ax2) plt.title('histogram of the right image normalization') plt.figure(figsize=(10, 2)) ax1 = plt.subplot(121) sns.distplot(reference.ravel(), ax=ax1) plt.title('histogram of the left image after normalization') return (matched.astype(np.uint8))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Sep 8 11:26:41 2019 @author: Jun """ import os import nibabel as nb from skimage.transform import match_histograms join = os.path.join img_path = 'path_to/nnUNet/nnunet/mydata_folder/nnUNet_raw_splitted/Task07_iSeg/imagesTs' ref_img = 'path_to/nnUNet/nnunet/mydata_folder/nnUNet_raw_splitted/Task07_iSeg/imagesVal/iseg_13_0001.nii.gz' save_path = 'path_to/nnUNet/nnunet/mydata_folder/nnUNet_raw_splitted/Task07_iSeg/imagesTsHist' filenames = os.listdir(img_path) filenames.sort() for img_name in filenames: img_nii = nb.load(join(img_path, img_name)) img_data = img_nii.get_data() ref_data = nb.load(ref_img).get_data() img_histmatch = match_histograms(img_data[img_data!=0], ref_data[ref_data!=0], multichannel=False) save_img = img_data.copy() save_img[img_data!=0] = img_histmatch img_hist_nii = nb.Nifti1Image(save_img, img_nii.affine, img_nii.header) nb.save(img_hist_nii, join(save_path, img_name))
def main(): print("Arguments: ") args = get_arguments() print("Reading Chlorophyll matrix from sheet %d from %s" % (args.sheet_no, args.chloro_ch)) print("RGB image: ", args.rgb_img) DIR_NAME = '.'.join(args.rgb_img.split('/')[-1].split('.')[:-1]) directory_path = os.path.join('save', DIR_NAME) if not os.path.exists(directory_path): os.mkdir(directory_path) # Read the chlorophyll image # Read sheet 1 chlorophyll channel ch = ChloroPreprocess(args.sheet_no) data, sheet_cnt = sheet_to_array(args.chloro_ch, args.sheet_no) print("Read chlorophyll data.") cv2.imwrite(directory_path + "/chloro" + str(args.sheet_no) + ".png", data) chloro_im = ch.preprocess_chlorophyll_ch(data, args.sheet_no) cv2.imwrite( directory_path + "/cropped_chloro" + str(args.sheet_no) + ".png", chloro_im) spectral_data = chloro_im[..., np.newaxis] hyperspectral_data = spectral_data for i in range(sheet_cnt): if i != args.sheet_no: data, _ = sheet_to_array(args.chloro_ch, i) im = ch.preprocess_chlorophyll_ch(data, i) cv2.imwrite(directory_path + "/cropped_chloro" + str(i) + ".png", im) try: hyperspectral_data = np.concatenate( (hyperspectral_data, im[..., np.newaxis]), axis=2) except: print("error!") hyperspectral_data = im[..., np.newaxis] else: try: hyperspectral_data = np.concatenate( (hyperspectral_data, spectral_data), axis=2) except: hyperspectral_data = spectral_data print(hyperspectral_data.shape) print("Read all the channels of hyperspectral image.") # Read the green channel from the rgb image and preprocess it. rgb = RGBPreprocess() rgb_img = cv2.imread(args.rgb_img) greench_img = rgb_img[:, :, 1] greench_im = rgb.preprocess_greench_image(greench_img) cv2.imwrite(directory_path + "/green_ch.png", greench_im) rgb_cropim = rgb.preprocess_rgb(rgb_img) cv2.imwrite(directory_path + "/rgb_cropped.png", rgb_cropim) # Match the histograms of the source and reference image ch_im = cv2.resize(spectral_data, (greench_im.shape[1], greench_im.shape[0])) greench_eq = match_histograms(greench_im, ch_im, multichannel=False) greench_eq = np.round(greench_eq).astype(np.uint8) cv2.imwrite(directory_path + "/hist_match_green_ch.png", greench_eq) # find matches using SIFT and warp the chlorophyll channel to align with the green channel of the rgb image. align = ImageAlignment() align.image_align_sift(spectral_data, greench_eq.astype(np.uint8)) aligned_im = align.warp_image(hyperspectral_data, rgb_cropim, sheet_cnt, directory_path) with open(directory_path + "/arr_dump.pickle", "wb") as f: pickle.dump(aligned_im, f)
def correct_color_scikit(sharp, blur): sharp = cv2.cvtColor(sharp, cv2.COLOR_RGB2BGR) blur = cv2.cvtColor(blur, cv2.COLOR_RGB2BGR) return match_histograms(blur, sharp, multichannel=True)
def ref(): Ref="" min = 100000000.0 path="ref" for i in os.listdir(path): full_path=os.path.join(path,i) temp=cv2.imread(full_path) hist=cv2.calcHist(temp,[0],None,[256],[0,256]) print(type(hist)) value=kl_divergence(hist_image, hist) print(value) if(value<min): min=value Ref=i return Ref Ref=ref() print(Ref) reference = cv2.imread(os.path.join('ref',Ref)) import numpy as np matched = match_histograms(image, reference,multichannel=True) print(matched.shape) cv2.imshow('orignal',cv2.resize(image,(500,500))) cv2.imshow('matched',cv2.resize(np.uint8(matched),(500,500))) cv2.waitKey(0)
ax[2].imshow(binary, cmap=plt.cm.gray) ax[2].set_title('Thresholded') ax[2].axis('off') plt.show() import matplotlib.pyplot as plt from skimage import data from skimage import exposure from skimage.transform import match_histograms ref = data.coffee() img = data.chelsea() matched = match_histograms(img, ref, multichannel=True) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3), sharex=True, sharey=True) for aa in (ax1, ax2, ax3): aa.set_axis_off() ax1.imshow(img) ax1.set_title('Source') ax2.imshow(ref) ax2.set_title('Reference') ax3.imshow(matched) ax3.set_title('Matched')
def register_perfusion(file_img_stress, file_msk_stress, file_img_rest, file_msk_rest): #LOAD NIFTIS img_data_stress = nib.load(file_img_stress) msk_data_stress = nib.load(file_msk_stress) img_data_rest = nib.load(file_img_rest) msk_data_rest = nib.load(file_msk_rest) #CONVERT TO LISTS img_lst_stress = [ img_data_stress.get_fdata()[t, ...] for t in range(img_data_stress.get_fdata().shape[0]) ] msk_lst_stress = [ msk_data_stress.get_fdata()[t, ...] for t in range(msk_data_stress.get_fdata().shape[0]) ] img_lst_rest = [ img_data_rest.get_fdata()[t, ...] for t in range(img_data_rest.get_fdata().shape[0]) ] msk_lst_rest = [ msk_data_rest.get_fdata()[t, ...] for t in range(msk_data_rest.get_fdata().shape[0]) ] #ID'S id = file_img_rest.split('_')[1] outcome = outcomes[id] #registration n_points = 20 rest = [] for img, msk in zip(img_lst_rest, msk_lst_rest): if len(np.unique(msk)) < 2: continue msk[msk == 2] = 0 img = img * msk img_out = obtain_polars(img, msk, n_points=20) rest.append(img_out) stress = [] for img, msk in zip(img_lst_stress, msk_lst_stress): if len(np.unique(msk)) < 2: continue msk[msk == 2] = 0 img = img * msk img_out = obtain_polars(img, msk, n_points=20) stress.append(img_out) stress_arr = np.array(stress) rest_arr = np.array(rest) stress_arr = zoom(stress_arr, (rest_arr.shape[0] / stress_arr.shape[0], 1.0, 1.0)) for idx in range(stress_arr.shape[0]): stress_img = stress_arr[idx, ...] rest_img = rest_arr[idx, ...] rest_img = transform.match_histograms(rest_img, stress_img) plt.gcf().canvas.flush_events() plt.imshow(np.hstack((stress_img, rest_img))) plt.show(block=False) plt.title(id + ' ' + str(outcome)) plt.pause(interval=0.1) return None
Histogram matching can be used as a lightweight normalisation for image processing, such as feature matching, especially in circumstances where the images have been taken from different sources or in different conditions (i.e. lighting). """ import matplotlib.pyplot as plt from skimage import data from skimage import exposure from skimage.transform import match_histograms reference = data.coffee() image = data.chelsea() matched = match_histograms(image, reference) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3), sharex=True, sharey=True) for aa in (ax1, ax2, ax3): aa.set_axis_off() ax1.imshow(image) ax1.set_title('Source') ax2.imshow(reference) ax2.set_title('Reference') ax3.imshow(matched) ax3.set_title('Matched') plt.tight_layout() plt.show()
def test_raises_value_error_on_channels_mismatch(self, image, reference): with pytest.raises(ValueError): transform.match_histograms(image, reference)
def test_raises_value_error_on_channels_mismatch(self, image, reference): with pytest.raises(ValueError): transform.match_histograms(image, reference)
def load_one_img(self, normalize=True, aug=True): if self.shuffle: file_name = random.sample(self.imgs_full_name, 1)[0] else: file_name = self.imgs_full_name.popleft() self.imgs_full_name.append(file_name) if self.for_what == 'train': raw_img = self.read_one_img(file_name) img = self.reduce_light(raw_img) hist = np.random.randint(0, 10, 1) if hist >= 4: alpha = np.random.randint(10, 50, 1) / 100 img = transform.match_histograms(img, self.std_dark * alpha) if aug: seq_det = data_aug_seq.to_deterministic() img = seq_det.augment_images([img.astype(np.uint8)])[0] dark_img = transform.resize(img, (img_size[0], img_size[1])) light_img = transform.resize(raw_img, (img_size[0], img_size[1])) n_gaussian = random.randint(5, 10) points = [] radius = [] for i in range(n_gaussian): x = random.randint(0, img_size[1] - 1) y = random.randint(0, img_size[0] - 1) radiu = random.uniform(50, max(*img_size) / 3) radius.append(radiu) points.append(np.array([x, y])) hm = self.__heat_map(img_size, points, radius) hm = np.greater(hm, 0.8).astype(np.float32) hm_ = 1. - hm dark_img = dark_img * np.expand_dims( hm, axis=-1) + light_img * np.expand_dims(hm_, axis=-1) cv2.imshow('hm', hm) return (light_img, dark_img) if normalize \ else ((light_img*255.).astype(np.uint8), (dark_img*255.).astype(np.uint8)) ##normalize to -1 -- 1 elif self.for_what == 'test': raw_img = self.read_one_img(file_name) raw_img = cv2.resize(raw_img, (img_size[1], img_size[0])) dark_img = self.reduce_light(raw_img) belta = np.random.randint(10, 100, 1) / 100 dark_img = transform.match_histograms(dark_img, self.std_dark * belta) return (raw_img*2./255.-1., dark_img*2./255.-1.) if normalize \ else (raw_img.astype(np.uint8), dark_img.astype(np.uint8)) elif self.for_what == 'real_night_test': raw_img = self.read_one_img(file_name) dark_img = cv2.resize(raw_img, (img_size[1], img_size[0])) belta = np.random.randint(10, 20, 1) / 100 dark_img = transform.match_histograms(dark_img, self.std_dark * belta) return (raw_img * 2. / 255. - 1., dark_img * 2. / 255. - 1.) if normalize \ else (raw_img.astype(np.uint8), dark_img.astype(np.uint8)) ##normalize to -1 -- 1 else: raise ValueError('wrong!!')
def match_hist(img, reference): img_eq = match_histograms(img, reference) img_eq[img == 0] = 0 return img_eq.astype(np.uint8)
def imgdiff(tile1, tile2, diff_path, save_path, data_path, img_path, msk_path, cloud_path, writer, width, height): xs = [ piece.split('_')[4:5][0] for piece in os.listdir(os.path.join(data_path, tile1, img_path)) ] ys = [ piece.split('_')[5:6][0].split('.')[0] for piece in os.listdir(os.path.join(data_path, tile1, img_path)) ] assert len(xs) == len(ys) for i in range(len(xs)): img1, meta = readtiff( os.path.join(data_path, tile1, img_path, tile1 + '_' + xs[i] + '_' + ys[i] + '.tiff')) img2, _ = readtiff( os.path.join(data_path, tile2, img_path, tile2 + '_' + xs[i] + '_' + ys[i] + '.tiff')) msk1 = imageio.imread( os.path.join(data_path, tile1, msk_path, tile1 + '_' + xs[i] + '_' + ys[i] + '.png')) msk2 = imageio.imread( os.path.join(data_path, tile2, msk_path, tile2 + '_' + xs[i] + '_' + ys[i] + '.png')) cld1 = io.imread( os.path.join(data_path, tile1, cloud_path, tile1 + '_' + xs[i] + '_' + ys[i] + '.tiff')) cld2 = io.imread( os.path.join(data_path, tile2, cloud_path, tile2 + '_' + xs[i] + '_' + ys[i] + '.tiff')) if np.max(cld1) < 0.2 and np.max(cld2) < 0.2: img2 = match_histograms(img2, img1, multichannel=True) diff_msk = np.abs(msk1 - msk2) name = diff_path.split( '/')[-1] + '_' + xs[i] + '_' + ys[i] + '.png' fail_names = os.listdir( '/home/vld-kh/data/big_data/DS/EcologyProject/CLEARCUT_DETECTION/DATA_DIFF/segmentation/data/diff5/img_msk_plots/fail_all' ) if name not in fail_names and (diff_msk.sum() / 255 > 2 or diff_msk.sum() / 255 == 0): diff_msk = (gaussian_filter(diff_msk, 0.5) > 0) * 255 diff_msk = diff_msk.astype(np.uint8) diff_msk = cv2.resize(diff_msk, (height, width), interpolation=cv2.INTER_NEAREST) meta['width'] = width meta['height'] = height u = 0 for img in [img1, img2]: u += 1 with rs.open( os.path.join( diff_path, img_path, diff_path.split('/')[-1] + '_' + xs[i] + '_' + ys[i] + f'_{u}.tiff'), 'w', **meta) as dst: for ix in range(img.shape[2]): dst.write(img[:, :, ix], ix + 1) dst.close() imageio.imwrite( os.path.join( diff_path, msk_path, diff_path.split('/')[-1] + '_' + xs[i] + '_' + ys[i] + f'_{1}.png'), diff_msk) writer.writerow([ diff_path.split('/')[-1], diff_path.split('/')[-1], xs[i] + '_' + ys[i], int(diff_msk.sum() / 255) ]) else: print(name) #""" else: pass
Histogram matching can be used as a lightweight normalisation for image processing, such as feature matching, especially in circumstances where the images have been taken from different sources or in different conditions (i.e. lighting). """ import matplotlib.pyplot as plt from skimage import data from skimage import exposure from skimage.transform import match_histograms reference = data.coffee() image = data.chelsea() matched = match_histograms(image, reference, multichannel=True) fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3), sharex=True, sharey=True) for aa in (ax1, ax2, ax3): aa.set_axis_off() ax1.imshow(image) ax1.set_title('Source') ax2.imshow(reference) ax2.set_title('Reference') ax3.imshow(matched) ax3.set_title('Matched') plt.tight_layout() plt.show()
def read_data(study_dir, subids, nsub=1e6, psize, npatch_perslice, erode_sz=1, lesioned=False, dohisteq=False): # erode_sz: reads the mask and erodes it by given number of voxels # dirlist = glob.glob(study_dir + '/TBI*') subno = 0 ref_imgs = 0 ref_imgs_set = False for subj in subids: t1_file = os.path.join(study_dir, subj, 'T1mni.nii.gz') t1_mask_file = os.path.join(study_dir, subj, 'T1mni.mask.nii.gz') t2_file = os.path.join(study_dir, subj, 'T2mni.nii.gz') flair_file = os.path.join(study_dir, subj, 'FLAIRmni.nii.gz') if not (os.path.isfile(t1_file) and os.path.isfile(t1_mask_file) and os.path.isfile(t2_file) and os.path.isfile(flair_file)): continue if subno < nsub: subno = subno + 1 print("subject %d " % (subno)) else: break # Read the three images t1 = nl.image.load_img(t1_file).get_data() t2 = nl.image.load_img(t2_file).get_data() flair = nl.image.load_img(flair_file).get_data() t1_msk = np.float32(nl.image.load_img(t1_mask_file).get_data() > 0) p = np.percentile(np.ravel(t1), 95) #normalize to 95 percentile t1 = np.float32(t1) / p p = np.percentile(np.ravel(t2), 95) #normalize to 95 percentile t2 = np.float32(t2) / p p = np.percentile(np.ravel(flair), 95) #normalize to 95 percentile flair = np.float32(flair) / p t1_msk = binary_erosion(t1_msk, iterations=erode_sz) imgs = np.stack((t1, t2, flair, t1_msk), axis=3) if ref_imgs_set == False: ref_imgs = imgs ref_imgs_set = True if dohisteq == True: imgs = match_histograms(image=imgs, reference=ref_imgs, multichannel=True) if lesioned == True: lesion = np.zeros(t1.shape) mskx, msky, mskz = np.where(t1 > 0) ind = random.randint(0, mskx.shape[0]) mskx = mskx[ind] msky = msky[ind] mskz = mskz[ind] # xyz = np.unravel_index(ind, shape=t1.shape) centr = np.array([mskx, msky, mskz])[:, None] blob, _ = make_blobs(n_samples=10, n_features=3, centers=centr, cluster_std=random.uniform(0, 30)) blob = np.int16( np.clip(np.round(blob), [0, 0, 0], np.array(t1.shape) - 1)) lesion[blob[:, 0], blob[:, 1], blob[:, 2]] = 1.0 #lesion[blob.ravel]=1.0 lesion = gaussian_filter(lesion, 5) lesion /= lesion.max() imgs = np.concatenate( (imgs[:, :, :, :3], lesion[:, :, :, None], imgs[:, :, :, -1:]), axis=3) # Generate random patches # preallocate if subno == 1: num_slices = imgs.shape[2] patch_data = np.zeros((nsub * npatch_perslice * num_slices, psize[0], psize[1], imgs.shape[-1])) for sliceno in tqdm(range(num_slices)): ptch = extract_patches_2d(image=imgs[:, :, sliceno, :], patch_size=psize, max_patches=npatch_perslice, random_state=1121) strt_ind = ( subno - 1) * npatch_perslice * num_slices + sliceno * npatch_perslice end_ind = (subno - 1) * npatch_perslice * num_slices + ( sliceno + 1) * npatch_perslice patch_data[strt_ind:end_ind, :, :, :] = ptch mask_data = patch_data[:, :, :, -1] patch_data = patch_data[:, :, :, :-1] return patch_data, mask_data # npatch x width x height x channels
def imgdiff(self, tile_current, tile_previous, diff_path, writer): def path_to_image(tile, path, x, y, ext='.png'): return os.path.join(self.data_path, tile, path, tile + '_' + x + '_' + y + ext) pieces = os.listdir( f"{self.data_path}/{tile_current}/{self.images_path}") xs = [piece.split('_')[-2:][0] for piece in pieces] ys = [piece.split('_')[-2:][1].split('.')[0] for piece in pieces] is_path_tile = {} for idx in range(len(xs)): images = {} masks = {} clouds = {} is_path_tile['current'] = os.path.exists( path_to_image(tile_current, self.images_path, xs[idx], ys[idx], ext='.tiff')) is_path_tile['previous'] = os.path.exists( path_to_image(tile_previous, self.images_path, xs[idx], ys[idx], ext='.tiff')) if is_path_tile['current'] and is_path_tile['previous']: images['current'], meta = readtiff( path_to_image(tile_current, self.images_path, xs[idx], ys[idx], ext='.tiff')) images['previous'], _ = readtiff( path_to_image(tile_current, self.images_path, xs[idx], ys[idx], ext='.tiff')) mask_path = path_to_image(tile_current, self.masks_path, xs[idx], ys[idx]) masks['current'] = imageio.imread(mask_path) mask_path = path_to_image(tile_previous, self.masks_path, xs[idx], ys[idx]) masks['previous'] = imageio.imread(mask_path) cloud_path = path_to_image(tile_current, self.clouds_path, xs[idx], ys[idx]) clouds['current'] = imageio.imread(cloud_path) / 255 cloud_path = path_to_image(tile_previous, self.clouds_path, xs[idx], ys[idx]) clouds['previous'] = imageio.imread(cloud_path) / 255 else: continue is_clouds_current = np.sum(clouds['current']) / clouds[ 'current'].size < settings.MAXIMUM_CLOUD_PERCENTAGE_ALLOWED is_clouds_previous = np.sum(clouds['previous']) / clouds[ 'previous'].size < settings.MAXIMUM_CLOUD_PERCENTAGE_ALLOWED if is_clouds_current and is_clouds_previous: images['previous'] = match_histograms(images['current'], images['previous'], multichannel=True) diff_img = self.diff(images, self.width, self.height) diff_msk = (np.abs(masks['current'] - masks['previous']) > 0) * 255 diff_msk = (gaussian_filter(diff_msk, 0.5) > 0) * 255 diff_msk = diff_msk.astype(np.uint8) diff_msk = cv2.resize(diff_msk, (self.height, self.width), interpolation=cv2.INTER_NEAREST) meta['width'] = self.width meta['height'] = self.height meta['count'] = diff_img.shape[2] result_images = os.path.join( diff_path, self.images_path, diff_path.split('/')[-1] + '_' + xs[idx] + '_' + ys[idx] + '.tiff') with rs.open(result_images, 'w', **meta) as dst: for ix in range(diff_img.shape[2]): dst.write(diff_img[:, :, ix], ix + 1) dst.close() result_masks = os.path.join( diff_path, self.masks_path, diff_path.split('/')[-1] + '_' + xs[idx] + '_' + ys[idx] + '.png') imageio.imwrite(result_masks, diff_msk) writer.writerow([ diff_path.split('/')[-1], diff_path.split('/')[-1], xs[idx] + '_' + ys[idx], int(diff_msk.sum() / 255) ])