Skip to content

Instantly share code, notes, and snippets.

@avanetten
Last active December 22, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avanetten/25e642259b81ed598de8fb70a46b754b to your computer and use it in GitHub Desktop.
Save avanetten/25e642259b81ed598de8fb70a46b754b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import matplotlib.pyplot as plt
import numpy as np
###############################################################################
def plot_building_mask(input_image, pixel_coords, mask_image,
figsize=(8,8), plot_name='',
add_title=False, poly_face_color='orange',
poly_edge_color='red', poly_nofill_color='blue', cmap='bwr'):
fig, (ax0, ax1, ax2) = plt.subplots(1, 3,
figsize=(3*figsize[0], figsize[1]))
if add_title:
suptitle = fig.suptitle(plot_name.split('/')[-1], fontsize='large')
# create patches
patches = []
patches_nofill = []
if len(pixel_coords) > 0:
# get patches
for coord in pixel_coords:
patches_nofill.append(Polygon(coord, facecolor=poly_nofill_color,
edgecolor=poly_edge_color, lw=3))
patches.append(Polygon(coord, edgecolor=poly_edge_color, fill=True,
facecolor=poly_face_color))
p0 = PatchCollection(patches, alpha=0.25, match_original=True)
p1 = PatchCollection(patches_nofill, alpha=0.75, match_original=True)
#if len(patches) > 0:
# p0 = PatchCollection(patches, alpha=0.25, match_original=True)
# #p1 = PatchCollection(patches, alpha=0.75, match_original=True)
# p1 = PatchCollection(patches_nofill, alpha=0.75, match_original=True)
# ax0: raw image
ax0.imshow(input_image)
if len(patches) > 0:
ax0.add_collection(p0)
ax0.set_title('Input Image + Ground Truth Buildings')
# truth polygons
zero_arr = np.zeros(input_image.shape[:2])
# set background to white?
#zero_arr[zero_arr == 0.0] = np.nan
ax1.imshow(zero_arr, cmap=cmap)
if len(patches) > 0:
ax1.add_collection(p1)
ax1.set_title('Ground Truth Building Polygons')
# old method of truth, with mask
## ax0: raw imageø
#ax0.imshow(input_image)
## ground truth
## set zeros to nan
#palette = plt.cm.gray
#palette.set_over('orange', 1.0)
#z = mask_image.astype(float)
#z[z==0] = np.nan
#ax0.imshow(z, cmap=palette, alpha=0.25,
# norm=matplotlib.colors.Normalize(vmin=0.5, vmax=0.9, clip=False))
#ax0.set_title('Input Image + Ground Truth Buildings')
# mask
ax2.imshow(mask_image, cmap=cmap)
# truth polygons?
#if len(patches) > 0:
# ax1.add_collection(p1)
ax2.set_title('Ground Truth Building Mask')
#plt.axis('off')
plt.tight_layout()
if add_title:
suptitle.set_y(0.95)
fig.subplots_adjust(top=0.96)
#plt.show()
if len(plot_name) > 0:
plt.savefig(plot_name)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment