Skip to content

Instantly share code, notes, and snippets.

@AxelStrem
Last active December 5, 2016 17:07
Show Gist options
  • Save AxelStrem/7a307b3618e650f8e9a48655bd65f978 to your computer and use it in GitHub Desktop.
Save AxelStrem/7a307b3618e650f8e9a48655bd65f978 to your computer and use it in GitHub Desktop.
import numpy as np
from skimage.color import rgb2gray
from skimage.filters import gaussian
import scipy.ndimage as ndi
import random
import scipy
import PIL
def img_prepare(img):
r = random.random()
if r<=0.3:
img = img.quantize()
img = img.convert("RGB")
r = random.random()
if r>=0.5:
return img
px = np.asarray(img, dtype = np.int32)
gs = rgb2gray(px/255.0)
markers = np.zeros_like(gs)
markers[gs < 0.1] = 1
markers[gs > 0.6] = 2
from skimage.filters import sobel
elevation_map = sobel(gs)
from skimage.morphology import watershed
segmentation = watershed(elevation_map, markers)
color = (int(random.random()*255),int(random.random()*255),int(random.random()*255))
if r<0.2:
px[segmentation>1,0:3]=color
elif r<0.5:
px[segmentation==1,0:3]=color
return PIL.Image.fromarray( np.asarray( np.clip(px,0,255), dtype=np.int8), "RGB" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment