Guest User

floatingforests

a guest
Jan 29th, 2015
19
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import skimage.color as skcolor
  3. import skimage.io as skio
  4. import scipy.stats as stats
  5. import os
  6.  
  7.  
  8. def hsv_thresholding(filename):
  9.     im = skio.imread(filename)
  10.     im = skcolor.rgb2hsv(im)
  11.  
  12.     im[:, :, 0] = stats.threshold(im[:, :, 0], threshmin=.5, threshmax=.7, newval=0)     # hue
  13.     im[:, :, 1] = stats.threshold(im[:, :, 1], threshmin=.05, threshmax=.45, newval=0)   # sat
  14.     im[:, :, 2] = stats.threshold(im[:, :, 2], threshmin=.005, threshmax=.55, newval=0)  # value
  15.     return im
  16.  
  17.  
  18. def coast_predicate(filename):
  19.  
  20.     def count_thresholded_pixels(array):
  21.         flat = np.reshape(np.ravel(array), (array.size//3, 3))
  22.         output = flat[(flat[:, 0] != 0) & (flat[:, 1] != 0) & (flat[:, 2] != 0)]
  23.         return output.shape[0]
  24.  
  25.     im = hsv_thresholding(filename)
  26.  
  27.     total_pixels = np.size(im)//3
  28.     water_percent = count_thresholded_pixels(im)/total_pixels
  29.     if verbose: print(round(water_percent, 3))
  30.     if water_percent > 0.05:
  31.         return True
  32.  
  33.     water_edge = False
  34.     edges = (count_thresholded_pixels(im[0, :, :]), count_thresholded_pixels(im[-1, :, :]),
  35.              count_thresholded_pixels(im[:, 0, :]), count_thresholded_pixels(im[:, -1, :]))
  36.     pixel_threshold = 20
  37.  
  38.     if any(edge > pixel_threshold for edge in edges):
  39.         if verbose: print('has water edge')
  40.         water_edge = True
  41.  
  42.     if water_edge and water_percent > 0.01:
  43.         return True
  44.  
  45.  
  46. if __name__ == "__main__":
  47.     verbose = False
  48.     datapath = 'data'
  49.     if not os.path.exists('pass'):
  50.         os.mkdir('pass')
  51.     if not os.path.exists('fail'):
  52.         os.mkdir('fail')
  53.  
  54.     filelist = [os.path.join(datapath, f) for f in os.listdir(datapath) if '.jpg' in f]
  55.     for filename in filelist:
  56.         if verbose: print(filename)
  57.  
  58.         if coast_predicate(filename):
  59.             if verbose: print('good image')
  60.             os.rename(filename, filename.replace(datapath, 'pass'))
  61.         else:
  62.             os.rename(filename, filename.replace(datapath, 'fail'))
  63.         if verbose: print('----')
RAW Paste Data