Skip to content

Instantly share code, notes, and snippets.

@Seagor
Created April 25, 2016 20:44
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 Seagor/9d874fadddf421f5b6f8755fd232f9ca to your computer and use it in GitHub Desktop.
Save Seagor/9d874fadddf421f5b6f8755fd232f9ca to your computer and use it in GitHub Desktop.
normalize = lambda x: (255 / (x.max() - x.min())) * (x - x.min())
smooth = lambda x: ndimage.convolve(x, np.ones([3,3])/9)
def threshold_mask(img, threshold=150):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
mask = np.ones(gray.shape).astype(np.uint8)
mask[gray < threshold] = 0
return mask
def scanline_mask(imask):
mask = np.ndarray(imask.shape)
for l in xrange(imask.shape[0]):
line = imask[l,:]
labels = measure.label(line)
land = labels[-1]
if land == 1:
mask[l,:] = labels
else:
labels[ labels != land ] = 0
labels[ labels > 0 ] = 1
mask[l,:] = labels
return (mask >= 1).astype(np.uint8)
def saturation_mask(img, threshold=170):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
normS = ndimage.convolve(~smooth(gray[:,:,1]), np.ones([3,3])/9)
mask = normS > threshold
return mask
region_masks_all = []
region_masks = []
for r in xrange(len(image_regions[0])):
_masks = []
for i in xrange(len(image_regions)):
img = image_regions[4][r].copy()
mask = saturation_mask( img )
_masks.append( mask )
region_masks_all.append(_masks)
r_mask = scanline_mask( reduce( lambda s,e: s + e, _masks ) )
render_array(r_mask, image_regions[0][r])
region_masks.append(r_mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment