Skip to content

Instantly share code, notes, and snippets.

@Perif
Last active August 15, 2017 13:38
Show Gist options
  • Save Perif/443cb396f9e1468d8e0d2cf5ce32a357 to your computer and use it in GitHub Desktop.
Save Perif/443cb396f9e1468d8e0d2cf5ce32a357 to your computer and use it in GitHub Desktop.
Plot images overlay
import pickle as pkl
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from sklearn import preprocessing
from mpl_toolkits.axes_grid1 import make_axes_locatable
from skimage import filters
discriminate = True
print("Reading Data")
images_orig = pkl.load(open('orig_imgs_n20_c1_h584_w565.pkl'))
images_gtruth = pkl.load(open('gtruth_imgs_n20_c1_h584_w565.pkl'))
images_pred = pkl.load(open('pred_imgs_n20_c1_h584_w565.pkl'))
min_max_scaler = preprocessing.MinMaxScaler()
print("Exporting images")
for i in range(images_orig.shape[0]):
print("> %3d of %3d" % (i,images_orig.shape[0]))
img_orig = images_orig[i][0]
img_gtruth = images_gtruth[i][0]
img_pred = images_pred[i][0]
# rescale to 0 and 1
img_gtruth = min_max_scaler.fit_transform(img_gtruth)
img_pred = min_max_scaler.fit_transform(img_pred)
if discriminate:
img_orig = np.ma.masked_where(img_orig < np.std(img_orig), img_orig)
img_gtruth = np.ma.masked_where(img_gtruth < filters.threshold_otsu(img_gtruth), img_gtruth)
img_pred = np.ma.masked_where(img_pred < filters.threshold_otsu(img_pred), img_pred)
fig = plt.figure(frameon=False)
im1 = plt.imshow(img_orig, cmap=plt.cm.gray, interpolation='nearest')
im2 = plt.imshow(img_gtruth, cmap=plt.cm.plasma, alpha=.8, interpolation='none',clim=(0.0, 1.0))
plt.colorbar()
fig.savefig('orig_gtruth_%d.png' % i, transparent=True)
plt.close(fig)
plt.clf()
fig = plt.figure(frameon=False)
im1 = plt.imshow(img_orig, cmap=plt.cm.gray, interpolation='nearest')
im2 = plt.imshow(img_pred, cmap=plt.cm.plasma, alpha=.8, interpolation='none', clim=(0.0, 1.0))
plt.colorbar()
fig.savefig('orig_pred_%d.png' % i, transparent=True)
plt.close(fig)
plt.clf()
figs = [(img_orig,img_gtruth), (img_orig, img_pred)]
titles = ['Ground Truth','Prediction with U-Net']
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12,8))
for dat, ax, title in zip(figs, axes.flat, titles):
im1=ax.imshow(dat[0], cmap=plt.cm.gray, interpolation='nearest')
im2=ax.imshow(dat[1], cmap=plt.cm.plasma_r, alpha=.8, interpolation='none',clim=(0.0, 1.0))
ax.set_title(title)
fig.colorbar(im2, ax=axes.ravel().tolist(), orientation='horizontal')
fig.savefig('comparison2_%d.png' % i, transparent=True)
plt.close(fig)
plt.clf()
print("Finised exporting images")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment