Created
January 31, 2017 09:45
-
-
Save zxytim/a01cea7a720901f9576722f10114d257 to your computer and use it in GitHub Desktop.
Distortion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env mdl | |
# -*- coding: utf-8 -*- | |
# $File: qr.py | |
# $Author: Xinyu Zhou <[email protected]> | |
# Copyright (c) 2015 Megvii Inc. All rights reserved. | |
import cv2 | |
from scipy.ndimage import interpolation, filters | |
import numpy as np | |
rng = np.random.RandomState(42) | |
img = cv2.imread('qr.png') | |
h, w, c = img.shape | |
dsigma = 5 | |
distort = 10 | |
xys = [ | |
filters.gaussian_filter(xs, dsigma) | |
for xs in rng.randn(2, h, w)] | |
xys = np.array([ | |
xs * distort / np.amax(xs) | |
for xs in xys | |
]) | |
dx, dy = xys | |
nxys = np.asarray([(x + dx[x,y], y + dy[x,y]) | |
for x in range(h) for y in | |
range(w)]).T | |
cs = [ | |
interpolation.map_coordinates( | |
g, | |
nxys, | |
order=3, | |
mode='constant', | |
cval=0, | |
).reshape(g.shape) | |
for g in cv2.split(img) | |
] | |
out = cv2.merge(cs) | |
cv2.imwrite('out.png', out) | |
# vim: foldmethod=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment