Last active
December 22, 2017 14:42
-
-
Save avanetten/dd180414aa59dabe3ca839b000166103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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_truth_coords(input_image, pixel_coords, | |
figsize=(8,8), plot_name='', | |
add_title=False, poly_face_color='orange', | |
poly_edge_color='red', poly_nofill_color='blue', cmap='bwr'): | |
'''Plot ground truth coordinaates, pixel_coords should be a numpy array''' | |
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(2*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, alpha=0.75, match_original=True) | |
p2 = 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(p2) | |
ax1.set_title('Ground Truth Building Polygons') | |
#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 patches, patches_nofill | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment