Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from scipy.ndimage import convolve1d
- from scipy.misc import imread, imsave
- import numpy as np
- Y_AXIS, X_AXIS = 0, 1
- def valid_edge(arr, i):
- return arr[i-1] < arr[i] > arr[(i+1) % len(arr)]
- def edge_loc(chunk, max_h_std=11):
- data = convolve1d(chunk, [1., -1.], X_AXIS)
- mean = np.mean(data, Y_AXIS)
- mx = np.argsort(mean)
- for m in reversed(mx):
- if not valid_edge(mean, m):
- continue
- std = np.std(data[:, m])
- if std > max_h_std:
- continue
- yield m, std
- def check(prev, cur):
- data = np.concatenate((prev, cur), Y_AXIS)
- data = convolve1d(data, [1., -1.], Y_AXIS)
- return np.mean(np.std(data, Y_AXIS) - np.mean(np.std(data, X_AXIS)))
- def main():
- chunk_size = 6
- max_retries = 5
- img_name = 'image.png'
- max_v_std = 1
- strfmt = '{:2} {:3} {:4} {:5.2f} {:5.2f}'
- titlefmt = ' {:>3} {:>4} {:>5} {:>5}'
- img = imread(img_name)
- data = imread(img_name, True)
- imgy, imgx, _ = img.shape
- prev = None
- print(titlefmt.format('x', 'y', 'hstd', 'vdiff'))
- for y in reversed(range(0, imgy, chunk_size)):
- hstd, vdiff = 0, 0
- rolled = None
- a, b = max(imgy - chunk_size - y, 0), imgy - y
- chunk = data[a:b]
- for t, (r, hstd) in zip(range(max_retries), edge_loc(chunk)):
- r += 1 # edge loc vs real edge diff
- rolled = np.roll(chunk, -r, X_AXIS)
- if prev is None:
- break
- vdiff = check(prev, rolled)
- if vdiff < max_v_std:
- break
- print(strfmt.format(t, a, r, hstd, vdiff))
- print(strfmt.format('>>', a, r, hstd, vdiff))
- img[a:b] = np.roll(img[a:b], -r, X_AXIS)
- prev = rolled
- imsave('out.png', np.rot90(img, 2))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement