Advertisement
bluex0

spoil_img

Oct 13th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from scipy.ndimage import convolve1d
  4. from scipy.misc import imread, imsave
  5. import numpy as np
  6.  
  7. Y_AXIS, X_AXIS = 0, 1
  8.  
  9.  
  10. def valid_edge(arr, i):
  11.     return arr[i-1] < arr[i] > arr[(i+1) % len(arr)]
  12.  
  13.  
  14. def edge_loc(chunk, max_h_std=11):
  15.     data = convolve1d(chunk, [1., -1.], X_AXIS)
  16.     mean = np.mean(data, Y_AXIS)
  17.     mx = np.argsort(mean)
  18.     for m in reversed(mx):
  19.         if not valid_edge(mean, m):
  20.             continue
  21.         std = np.std(data[:, m])
  22.         if std > max_h_std:
  23.             continue
  24.         yield m, std
  25.  
  26.  
  27. def check(prev, cur):
  28.     data = np.concatenate((prev, cur), Y_AXIS)
  29.     data = convolve1d(data, [1., -1.], Y_AXIS)
  30.     return np.mean(np.std(data, Y_AXIS) - np.mean(np.std(data, X_AXIS)))
  31.  
  32.  
  33. def main():
  34.     chunk_size = 6
  35.     max_retries = 5
  36.     img_name = 'image.png'
  37.     max_v_std = 1
  38.     strfmt = '{:2} {:3} {:4} {:5.2f} {:5.2f}'
  39.     titlefmt = '   {:>3} {:>4} {:>5} {:>5}'
  40.  
  41.     img = imread(img_name)
  42.     data = imread(img_name, True)
  43.     imgy, imgx, _ = img.shape
  44.     prev = None
  45.     print(titlefmt.format('x', 'y', 'hstd', 'vdiff'))
  46.     for y in reversed(range(0, imgy, chunk_size)):
  47.         hstd, vdiff = 0, 0
  48.         rolled = None
  49.         a, b = max(imgy - chunk_size - y, 0), imgy - y
  50.         chunk = data[a:b]
  51.         for t, (r, hstd) in zip(range(max_retries), edge_loc(chunk)):
  52.             r += 1  # edge loc vs real edge diff
  53.             rolled = np.roll(chunk, -r, X_AXIS)
  54.             if prev is None:
  55.                 break
  56.             vdiff = check(prev, rolled)
  57.             if vdiff < max_v_std:
  58.                 break
  59.             print(strfmt.format(t, a, r, hstd, vdiff))
  60.         print(strfmt.format('>>', a, r, hstd, vdiff))
  61.         img[a:b] = np.roll(img[a:b], -r, X_AXIS)
  62.         prev = rolled
  63.     imsave('out.png', np.rot90(img, 2))
  64.  
  65. if __name__ == '__main__':
  66.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement