Skip to content

Instantly share code, notes, and snippets.

@zxytim
Created January 31, 2017 09:45
Show Gist options
  • Save zxytim/a01cea7a720901f9576722f10114d257 to your computer and use it in GitHub Desktop.
Save zxytim/a01cea7a720901f9576722f10114d257 to your computer and use it in GitHub Desktop.
Distortion
#!/usr/bin/env mdl
# -*- coding: utf-8 -*-
# $File: qr.py
# $Author: Xinyu Zhou <zxy@megvii.com>
# 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