Skip to content

Commit 66811da

Browse files
committed
Work in progress, new examples
1 parent 81e82ee commit 66811da

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

examples2/ch1_image_representation.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from PIL import Image
2+
import numpy as np
3+
import matplotlib.pyplot as pl
4+
5+
"""
6+
This example shows how images are represented using pixels, color channels and data types.
7+
"""
8+
9+
10+
# read image to array
11+
im = np.array(Image.open('../data/empire.jpg'))
12+
print("Shape is: {0} of type {1}".format(im.shape, im.dtype))
13+
14+
# read grayscale version to float array
15+
im = np.array(Image.open('../data/empire.jpg').convert('L'),'f')
16+
print("Shape is: {0} of type {1}".format(im.shape, im.dtype))
17+
18+
# visualize the pixel value of a small region
19+
col_1, col_2 = 190, 225
20+
row_1, row_2 = 230, 265
21+
22+
# crop using array slicing
23+
crop = im[col_1:col_2,row_1:row_2]
24+
cols, rows = crop.shape
25+
26+
print("Created crop of shape: {0}".format(crop.shape))
27+
28+
# generate all the plots
29+
pl.figure()
30+
pl.imshow(im)
31+
pl.gray()
32+
pl.plot([row_1, row_2, row_2, row_1, row_1], [col_1, col_1, col_2, col_2, col_1], linewidth=2)
33+
pl.axis('off')
34+
35+
pl.figure()
36+
pl.imshow(crop)
37+
pl.gray()
38+
pl.axis('off')
39+
40+
pl.figure()
41+
pl.imshow(crop)
42+
pl.gray()
43+
pl.plot(20*np.ones(cols), linewidth=2)
44+
pl.axis('off')
45+
46+
pl.figure()
47+
pl.plot(crop[20,:])
48+
pl.ylabel("Graylevel value")
49+
50+
from mpl_toolkits.mplot3d import axes3d
51+
fig = pl.figure()
52+
ax = fig.gca(projection='3d')
53+
# surface plot with transparency 0.5
54+
X,Y = np.meshgrid(np.arange(cols),-np.arange(rows))
55+
ax.plot_surface(X, Y, crop, alpha=0.5, cstride=2, rstride=2)
56+
57+
pl.show()

examples2/examples.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Exemples for the upcoming edition of PCV
2+
3+
This folder contains work in progress and files intended for the second edition of "Programming Computer Vision with Python".
4+
5+
More details on the book can be found at [programmingcomputervision.com](http://programmingcomputervision.com/)

0 commit comments

Comments
 (0)