Skip to content

Instantly share code, notes, and snippets.

@tianruig
Last active October 1, 2018 04:21
Show Gist options
  • Save tianruig/c875effe69c676635750b0e0714b17d6 to your computer and use it in GitHub Desktop.
Save tianruig/c875effe69c676635750b0e0714b17d6 to your computer and use it in GitHub Desktop.
import argparse
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import LassoSelector
from matplotlib.path import Path
from skimage import color
parser = argparse.ArgumentParser(description="mask an image")
parser.add_argument("image")
args = parser.parse_args()
fn = args.image
im = color.rgb2gray(plt.imread(fn)/255.)
fout = ''.join(fn.split('.')[:-1]) + "_mask"
imh, imw = im.shape
plt.figure()
plt.imshow(im, cmap='gray')
def onselect(verts):
path = Path(verts)
x = np.arange(imw)
y = np.arange(imh)
xx, yy = np.meshgrid(x, y)
points = list(zip(np.ravel(xx), np.ravel(yy)))
inside = np.reshape(path.contains_points(points), (imh, imw))
plt.figure()
plt.imshow(inside)
plt.show()
np.save(fout, inside)
lasso = LassoSelector(plt.gca(), onselect)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment