Skip to content

Commit 82d7286

Browse files
committed
DOC Better documentation of code
1 parent de42ba3 commit 82d7286

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

ch10/edginess.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111

1212
def edginess_sobel(image):
13-
'''
14-
edgi = edginess_sobel(image)
13+
'''Measure the "edginess" of an image
14+
15+
image should be a 2d numpy array (an image)
16+
17+
Returns a floating point value which is higher the "edgier" the image is.
1518
16-
Measure the "edginess" of an image
1719
'''
1820
edges = mh.sobel(image, just_filter=True)
1921
edges = edges.ravel()

ch10/figure10.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import numpy as np
99
import mahotas as mh
1010

11+
# This little script just builds an image with two examples, side-by-side:
12+
1113
text = mh.imread("simple-dataset/text21.jpg")
1214
scene = mh.imread("simple-dataset/scene00.jpg")
1315
h, w, _ = text.shape

ch10/figure13.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99
from mahotas.colors import rgb2grey
1010
import numpy as np
1111

12+
# Adds a little salt-n-pepper noise to an image
13+
1214
im = mh.imread('lenna.jpg')
1315
im = rgb2grey(im)
1416

17+
# Salt & pepper arrays
1518
salt = np.random.random(im.shape) > .975
1619
pepper = np.random.random(im.shape) > .975
1720

21+
# salt is 170 & pepper is 30
22+
# Some playing around showed that setting these to more extreme values looks
23+
# very artificial. These look nicer
24+
1825
im = np.maximum(salt * 170, mh.stretch(im))
1926
im = np.minimum(pepper * 30 + im * (~pepper), im)
2027

ch10/lenna-ring.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,35 @@
77

88
import mahotas as mh
99
import numpy as np
10+
11+
# Read in the image
1012
im = mh.imread('lenna.jpg')
13+
14+
# This breaks up the image into RGB channels
1115
r, g, b = im.transpose(2, 0, 1)
1216
h, w = r.shape
17+
18+
# smooth the image per channel:
1319
r12 = mh.gaussian_filter(r, 12.)
1420
g12 = mh.gaussian_filter(g, 12.)
1521
b12 = mh.gaussian_filter(b, 12.)
22+
23+
# build back the RGB image
1624
im12 = mh.as_rgb(r12, g12, b12)
1725

1826
X, Y = np.mgrid[:h, :w]
1927
X = X - h / 2.
2028
Y = Y - w / 2.
2129
X /= X.max()
2230
Y /= Y.max()
31+
32+
# Array C will have the highest values in the center, fading out to the edges:
33+
2334
C = np.exp(-2. * (X ** 2 + Y ** 2))
2435
C -= C.min()
2536
C /= C.ptp()
2637
C = C[:, :, None]
2738

39+
# The final result is sharp in the centre and smooths out to the borders:
2840
ring = mh.stretch(im * C + (1 - C) * im12)
2941
mh.imsave('lenna-ring.jpg', ring)

0 commit comments

Comments
 (0)