def run(self): nrrd_reader = NrrdReader() tiff_ring_reader = TiffRingReader() # read the flatfield flat = nrrd_reader.read(self.input()[1].path) dark = nrrd_reader.read(self.input()[3].path) # read the msi nr_filters = len(sc.other["RECORDED_WAVELENGTHS"]) msi, segmentation = tiff_ring_reader.read(self.input()[2].path, nr_filters, resize_factor=0.5) # only take into account not saturated pixels. segmentation = np.logical_and( segmentation, (np.max(msi.get_image(), axis=-1) < 1000.)) # correct image setup filter_nr = int(self.image_name[-6:-5]) original_order = np.arange(nr_filters) new_image_order = np.concatenate( (original_order[nr_filters - filter_nr:], original_order[:nr_filters - filter_nr])) # resort msi to restore original order msimani.get_bands(msi, new_image_order) # correct by flatfield msimani.image_correction(msi, flat, dark) # create artificial rgb rgb_image = msi.get_image()[:, :, [2, 3, 1]] rgb_image /= np.max(rgb_image) rgb_image *= 255. # preprocess the image # sortout unwanted bands print "1" # zero values would lead to infinity logarithm, thus clip. msi.set_image(np.clip(msi.get_image(), 0.00001, 2.**64)) # normalize to get rid of lighting intensity norm.standard_normalizer.normalize(msi) # transform to absorption msi.set_image(-np.log(msi.get_image())) # normalize by l2 for stability norm.standard_normalizer.normalize(msi, "l2") print "2" # estimate path = sc.get_full_dir("TENSORFLOW_DATA") sitk_image, time = estimate_image_tensorflow(msi, path) image = sitk.GetArrayFromImage(sitk_image) plt.figure() print "3" rgb_image = rgb_image.astype(np.uint8) im = Image.fromarray(rgb_image, 'RGB') enh_brightness = ImageEnhance.Brightness(im) im = enh_brightness.enhance(10.) plotted_image = np.array(im) top_left_axis = plt.gca() top_left_axis.imshow(plotted_image, interpolation='nearest') top_left_axis.xaxis.set_visible(False) top_left_axis.yaxis.set_visible(False) plt.set_cmap("jet") print "4" # plot parametric maps segmentation[0, 0] = 1 segmentation[0, 1] = 1 oxy_image = np.ma.masked_array(image[:, :], ~segmentation) oxy_image[np.isnan(oxy_image)] = 0. oxy_image[np.isinf(oxy_image)] = 0. oxy_mean = np.mean(oxy_image) oxy_image[0, 0] = 0.0 oxy_image[0, 1] = 1. plot_image(oxy_image[:, :], plt.gca()) df_image_results = pd.DataFrame( data=np.expand_dims([self.image_name, oxy_mean * 100., time], 0), columns=["image name", "oxygenation mean [%]", "time to estimate"]) results_file = os.path.join(sc.get_full_dir("SMALL_BOWEL_RESULT"), "results.csv") if os.path.isfile(results_file): df_results = pd.read_csv(results_file, index_col=0) df_results = pd.concat( (df_results, df_image_results)).reset_index(drop=True) else: df_results = df_image_results df_results.to_csv(results_file) plt.savefig(self.output().path, dpi=250, bbox_inches='tight') plt.close("all")
def setUp(self): self.nrrdReader = NrrdReader() self.msi = self.nrrdReader.read('./msi/data/testMsi.nrrd')