Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for python3 (and python2) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
473 changes: 86 additions & 387 deletions README.ipynb

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ imshow(image)



<matplotlib.image.AxesImage at 0x7fabcc7ab390>
<matplotlib.image.AxesImage at 0x7f511c1b0588>



Expand Down Expand Up @@ -62,18 +62,18 @@ ocrodeg.random_transform()



{'angle': -0.016783842893063807,
'aniso': 0.805280370671964,
'scale': 0.9709145529604223,
'translation': (0.014319657859164045, 0.03676897986267606)}
{'angle': -0.012292452096776464,
'scale': 1.0082362389124033,
'aniso': 1.1871904039346834,
'translation': (-0.015090714955534455, -0.011666614466062153)}



Here are four samples generated by random transforms.


```python
for i in xrange(4):
for i in range(4):
subplot(2, 2, i+1)
imshow(ocrodeg.transform_image(image, **ocrodeg.random_transform()))
```
Expand Down Expand Up @@ -198,7 +198,7 @@ imshow(patch)



<matplotlib.image.AxesImage at 0x7fabc88c7e10>
<matplotlib.image.AxesImage at 0x7f5118190710>



Expand Down Expand Up @@ -233,7 +233,6 @@ for i, s in enumerate([0, 1, 2, 4]):


```python
reload(ocrodeg)
for i, s in enumerate([0.0, 1.0, 2.0, 4.0]):
subplot(2, 2, i+1)
blurred = ocrodeg.binary_blur(patch, s)
Expand Down Expand Up @@ -261,7 +260,6 @@ for i, s in enumerate([0.0, 0.1, 0.2, 0.3]):


```python
reload(ocrodeg)
for i in range(4):
noisy = ocrodeg.make_multiscale_noise_uniform((512, 512))
subplot(2, 2, i+1); imshow(noisy, vmin=0, vmax=1)
Expand All @@ -286,7 +284,6 @@ for i, s in enumerate([2, 5, 10, 20]):


```python
reload(ocrodeg)
blotched = ocrodeg.random_blotches(patch, 3e-4, 1e-4)
#blotched = minimum(maximum(patch, ocrodeg.random_blobs(patch.shape, 30, 10)), 1-ocrodeg.random_blobs(patch.shape, 15, 8))
subplot(121); imshow(patch); subplot(122); imshow(blotched)
Expand All @@ -295,7 +292,7 @@ subplot(121); imshow(patch); subplot(122); imshow(blotched)



<matplotlib.image.AxesImage at 0x7fabc8a35490>
<matplotlib.image.AxesImage at 0x7f51187a3438>



Expand All @@ -313,7 +310,7 @@ imshow(ocrodeg.make_fibrous_image((256, 256), 700, 300, 0.01))



<matplotlib.image.AxesImage at 0x7fabc8852450>
<matplotlib.image.AxesImage at 0x7f51185f4160>



Expand All @@ -331,7 +328,7 @@ subplot(121); imshow(patch); subplot(122); imshow(ocrodeg.printlike_multiscale(p



<matplotlib.image.AxesImage at 0x7fabc8676d90>
<matplotlib.image.AxesImage at 0x7f511858f0f0>



Expand All @@ -347,7 +344,7 @@ subplot(121); imshow(patch); subplot(122); imshow(ocrodeg.printlike_fibrous(patc



<matplotlib.image.AxesImage at 0x7fabc8d1b250>
<matplotlib.image.AxesImage at 0x7f51185bd8d0>



Expand Down
3 changes: 2 additions & 1 deletion ocrodeg/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from degrade import * # noqa
from __future__ import absolute_import
from ocrodeg.degrade import * # noqa
28 changes: 15 additions & 13 deletions ocrodeg/degrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ def make_noise_at_scale(shape, scale):
result = ndi.zoom(data, scale)
return result[:h, :w]

def make_multiscale_noise(shape, scales, weights=None, range=(0.0, 1.0)):
def make_multiscale_noise(shape, scales, weights=None, span=(0.0, 1.0)):
if weights is None: weights = [1.0] * len(scales)
result = make_noise_at_scale(shape, scales[0]) * weights[0]
for s, w in zip(scales, weights):
result += make_noise_at_scale(shape, s) * w
lo, hi = range
lo, hi = span
result -= amin(result)
result /= amax(result)
result *= (hi-lo)
result += lo
return result

def make_multiscale_noise_uniform(shape, srange=(1.0, 100.0), nscales=4, range=(0.0, 1.0)):
def make_multiscale_noise_uniform(shape, srange=(1.0, 100.0), nscales=4, span=(0.0, 1.0)):
lo, hi = log10(srange[0]), log10(srange[1])
scales = np.random.uniform(size=nscales)
scales = add.accumulate(scales)
Expand All @@ -125,18 +125,19 @@ def make_multiscale_noise_uniform(shape, srange=(1.0, 100.0), nscales=4, range=(
scales += lo
scales = 10**scales
weights = 2.0 * np.random.uniform(size=nscales)
return make_multiscale_noise(shape, scales, weights=weights, range=range)
return make_multiscale_noise(shape, scales, weights=weights, span=span)

#
# random blobs
#

def random_blobs(shape, blobdensity, size, roughness=2.0):
from random import randint
from builtins import range # python2 compatible
h, w = shape
numblobs = int(blobdensity * w * h)
mask = np.zeros((h, w), 'i')
for i in xrange(numblobs):
for i in range(numblobs):
mask[randint(0, h-1), randint(0, w-1)] = 1
dt = ndi.distance_transform_edt(1-mask)
mask = np.array(dt < size, 'f')
Expand Down Expand Up @@ -166,11 +167,12 @@ def make_fiber(l, a, stepsize=0.5):
sins = add.accumulate(sin(angles)*stepsize)
return array([coss, sins]).transpose(1, 0)

def make_fibrous_image(shape, nfibers=300, l=300, a=0.2, stepsize=0.5, range=(0.1, 1.0), blur=1.0):
def make_fibrous_image(shape, nfibers=300, l=300, a=0.2, stepsize=0.5, span=(0.1, 1.0), blur=1.0):
from builtins import range # python2 compatible
h, w = shape
lo, hi = range
lo, hi = span
result = zeros(shape)
for i in xrange(nfibers):
for i in range(nfibers):
v = pylab.rand() * (hi-lo) + lo
fiber = make_fiber(l, a, stepsize=stepsize)
y, x = randint(0, h-1), randint(0, w-1)
Expand All @@ -195,8 +197,8 @@ def make_fibrous_image(shape, nfibers=300, l=300, a=0.2, stepsize=0.5, range=(0.
def printlike_multiscale(image, blur=1.0, blotches=5e-5):
selector = autoinvert(image)
selector = random_blotches(selector, 3*blotches, blotches)
paper = make_multiscale_noise_uniform(image.shape, range=(0.5, 1.0))
ink = make_multiscale_noise_uniform(image.shape, range=(0.0, 0.5))
paper = make_multiscale_noise_uniform(image.shape, span=(0.5, 1.0))
ink = make_multiscale_noise_uniform(image.shape, span=(0.0, 0.5))
blurred = ndi.gaussian_filter(selector, blur)
printed = blurred * ink + (1-blurred) * paper
return printed
Expand All @@ -205,9 +207,9 @@ def printlike_multiscale(image, blur=1.0, blotches=5e-5):
def printlike_fibrous(image, blur=1.0, blotches=5e-5):
selector = autoinvert(image)
selector = random_blotches(selector, 3*blotches, blotches)
paper = make_multiscale_noise(image.shape, [1.0, 5.0, 10.0, 50.0], weights=[1.0, 0.3, 0.5, 0.3], range=(0.7, 1.0))
paper -= make_fibrous_image(image.shape, 300, 500, 0.01, range=(0.0, 0.25), blur=0.5)
ink = make_multiscale_noise(image.shape, [1.0, 5.0, 10.0, 50.0], range=(0.0, 0.5))
paper = make_multiscale_noise(image.shape, [1.0, 5.0, 10.0, 50.0], weights=[1.0, 0.3, 0.5, 0.3], span=(0.7, 1.0))
paper -= make_fibrous_image(image.shape, 300, 500, 0.01, span=(0.0, 0.25), blur=0.5)
ink = make_multiscale_noise(image.shape, [1.0, 5.0, 10.0, 50.0], span=(0.0, 0.5))
blurred = ndi.gaussian_filter(selector, blur)
printed = blurred * ink + (1-blurred) * paper
return printed
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy
matplotlib
scipy
2 changes: 1 addition & 1 deletion run-tests
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -e
for nbtest in test-*.ipynb; do
jupyter nbconvert --execute --to markdown $nbtest
jupyter nbconvert --execute --to markdown $nbtest --ExecutePreprocessor.kernel_name=python
done
34 changes: 7 additions & 27 deletions test-degrade.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,13 @@
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python2.7/dist-packages/IPython/core/magics/pylab.py:161: UserWarning: pylab import has clobbered these variables: ['angle']\n",
"`%matplotlib` prevents importing * from pylab and numpy\n",
" \"\\n`%matplotlib` prevents importing * from pylab and numpy\"\n"
]
}
],
"outputs": [],
"source": [
"%pylab inline"
]
Expand Down Expand Up @@ -366,7 +349,6 @@
},
"outputs": [],
"source": [
"reload(ocrodeg)\n",
"for i, s in enumerate([0.0, 1.0, 2.0, 4.0]):\n",
" subplot(2, 2, i+1)\n",
" blurred = ocrodeg.binary_blur(patch, s)\n",
Expand Down Expand Up @@ -411,7 +393,6 @@
},
"outputs": [],
"source": [
"reload(ocrodeg)\n",
"for i in range(4):\n",
" noisy = ocrodeg.make_multiscale_noise_uniform((512, 512))\n",
" subplot(2, 2, i+1); imshow(noisy, vmin=0, vmax=1)"
Expand Down Expand Up @@ -453,7 +434,6 @@
},
"outputs": [],
"source": [
"reload(ocrodeg)\n",
"blotched = ocrodeg.random_blotches(patch, 3e-4, 1e-4)\n",
"#blotched = minimum(maximum(patch, ocrodeg.random_blobs(patch.shape, 30, 10)), 1-ocrodeg.random_blobs(patch.shape, 15, 8))\n",
"subplot(121); imshow(patch); subplot(122); imshow(blotched)"
Expand Down Expand Up @@ -535,21 +515,21 @@
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
Expand Down