Skip to content

Commit 503ea0e

Browse files
committed
Updated README
1 parent 3dce2cf commit 503ea0e

16 files changed

+160
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,82 @@
11
# imutils
22
A series of convenience functions to make basic image processing functions such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.
3+
4+
For more information, along with a detailed code review check out the following posts on the [PyImageSearch.com](http://www.pyimagesearch.com) blog:
5+
6+
- *Links to be added soon*
7+
8+
## Translation
9+
Translation is the shifting of an image in either the *x* or *y* direction. To translate an image in OpenCV you would need to supply the *(x, y)*-shift, denoted as *(t<sub>x</sub>, t<sub>y</sub>)* to construct the translation matrix *M*:
10+
11+
![Translation equation](docs/images/translation_eq.png?raw=true)
12+
13+
And from there, you woud need to apply the `cv2.warpAffine` function.
14+
15+
Instead of manually constructing the translation matrix *M* and calling `cv2.warpAffine`, you can simply make a call to the `translate` function of `imutils`.
16+
17+
#### Example:
18+
<pre># translate the image x=25 pixels to the right and y=75 pixels up
19+
translated = imutils.translate(workspace, 25, -75)</pre>
20+
21+
#### Output:
22+
<img src="docs/images/translation.png?raw=true" alt="Translation example"/ style="max-width: 500px;">
23+
24+
## Rotation
25+
Rotating an image in OpenCV is accomplished by making a call to `cv2.getRotationMatrix2D` and `cv2.warpAffine`. Further care has to be taken to supply the *(x, y)*-coordinate of the point the image is to be rotated about. These calculation calls can quickly add up and make your code bulky and less readable. The `rotate` function in `imutils` helps resolve this problem.
26+
27+
#### Example:
28+
<pre># loop over the angles to rotate the image
29+
for angle in xrange(0, 360, 90):
30+
# rotate the image and display it
31+
rotated = imutils.rotate(bridge, angle=angle)
32+
cv2.imshow("Angle=%d" % (angle), rotated)</pre>
33+
34+
#### Output:
35+
<img src="docs/images/rotation.png?raw=true" alt="Rotation example"/ style="max-width: 500px;">
36+
37+
## Resizing
38+
Resizing an image in OpenCV is accomplished by calling the `cv2.resize` function. However, special care needs to be taken to ensure that the aspect ratio is maintained. This `resize` function of `imutils` maintains the aspect ratio and provides the keyword arguments `width` and `height` so the image can be resized to the intended width/height while (1) maintaining aspect ratio and (2) ensuring the dimensions of the image do not have to be explicitly computed by the developer.
39+
40+
Another optional keyword argument, `inter`, can be used to specify interpolation method as well.
41+
42+
#### Example:
43+
<pre># loop over varying widths to resize the image to
44+
for width in (400, 300, 200, 100):
45+
# resize the image and display it
46+
resized = imutils.resize(workspace, width=width)
47+
cv2.imshow("Width=%dpx" % (width), resized)</pre>
48+
49+
#### Output:
50+
<img src="docs/images/resizing.png?raw=true" alt="Resizing example"/ style="max-width: 500px;">
51+
52+
## Skeletonization
53+
Skeletonization is the process of constructing the "topological skeleton" of an object in an image, where the object is presumed to be white on a black background. OpenCV does not provide a function to explicity construct the skeleton, but does provide the morphological and binary functions to do so.
54+
55+
For convenience, the `skeletonize` function of `imutils` can be used to construct the topological skeleton of the image.
56+
57+
The first argument, `size` is the size of the structuring element kernel. An optional argument, `structuring`, can be used to control the structuring element -- it defaults to `cv2.MORPH_RECT` , but can be any valid structuring element.
58+
59+
#### Example:
60+
<pre># skeletonize the image
61+
gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
62+
skeleton = imutils.skeletonize(gray, size=(3, 3))
63+
cv2.imshow("Skeleton", skeleton)</pre>
64+
65+
#### Output:
66+
<img src="docs/images/skeletonization.png?raw=true" alt="Skeletonization example"/ style="max-width: 500px;">
67+
68+
## Displaying with Matplotlib
69+
In the Python bindings of OpenCV, images are represented as NumPy arrays in BGR order. This works fine when using the `cv2.imshow` function. However, if you intend on using Matplotlib, the `plt.imshow` function assumes the image is in RGB order. A simple call to `cv2.cvtColor` will resolve this problem, or you can use the `opencv2matplotlib` conveince function.
70+
71+
#### Example:
72+
<pre># INCORRECT: show the image without converting color spaces
73+
plt.figure("Incorrect")
74+
plt.imshow(cactus)
75+
76+
# CORRECT: convert color spaces before using plt.imshow
77+
plt.figure("Correct")
78+
plt.imshow(imutils.opencv2matplotlib(cactus))
79+
plt.show()</pre>
80+
81+
#### Output:
82+
<img src="docs/images/matplotlib.png?raw=true" alt="Matplotlib example"/ style="max-width: 500px;">

demo.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# author: Adrian Rosebrock
2+
# website: http://www.pyimagesearch.com
3+
4+
# USAGE
5+
# python demo.py
6+
7+
# import the necessary packages
8+
import matplotlib.pyplot as plt
9+
import imutils
10+
import cv2
11+
12+
# load the example images
13+
bridge = cv2.imread("demo_images/bridge.jpg")
14+
cactus = cv2.imread("demo_images/cactus.jpg")
15+
logo = cv2.imread("demo_images/pyimagesearch_logo.jpg")
16+
workspace = cv2.imread("demo_images/workspace.jpg")
17+
18+
# 1. TRANSLATION
19+
# show the original image
20+
cv2.imshow("Original", workspace)
21+
22+
# translate the image x-50 pixels to the left and y=100 pixels down
23+
translated = imutils.translate(workspace, -50, 100)
24+
cv2.imshow("Translated", translated)
25+
cv2.waitKey(0)
26+
27+
# translate the image x=25 pixels to the right and y=75 pixels up
28+
translated = imutils.translate(workspace, 25, -75)
29+
cv2.imshow("Translated", translated)
30+
cv2.waitKey(0)
31+
cv2.destroyAllWindows()
32+
33+
# 2. ROTATION
34+
# loop over the angles to rotate the image
35+
for angle in xrange(0, 360, 90):
36+
# rotate the image and display it
37+
rotated = imutils.rotate(bridge, angle=angle)
38+
cv2.imshow("Angle=%d" % (angle), rotated)
39+
40+
# wait for a keypress, then close all the windows
41+
cv2.waitKey(0)
42+
cv2.destroyAllWindows()
43+
44+
# 3. RESIZING
45+
# loop over varying widths to resize the image to
46+
for width in (400, 300, 200, 100):
47+
# resize the image and display it
48+
resized = imutils.resize(workspace, width=width)
49+
cv2.imshow("Width=%dpx" % (width), resized)
50+
51+
# wait for a keypress, then close all the windows
52+
cv2.waitKey(0)
53+
cv2.destroyAllWindows()
54+
55+
# 4. SKELETONIZATION
56+
# skeletonize the image using a 3x3 kernel
57+
cv2.imshow("Original", logo)
58+
gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
59+
skeleton = imutils.skeletonize(gray, size=(3, 3))
60+
cv2.imshow("Skeleton", skeleton)
61+
cv2.waitKey(0)
62+
cv2.destroyAllWindows()
63+
64+
# 5. MATPLOTLIB
65+
# INCORRECT: show the image without converting color spaces
66+
plt.figure("Incorrect")
67+
plt.imshow(cactus)
68+
69+
# CORRECT: convert color spaces before using plt.imshow
70+
plt.figure("Correct")
71+
plt.imshow(imutils.opencv2matplotlib(cactus))
72+
plt.show()

demo_images/bridge.jpg

39.7 KB
Loading

demo_images/cactus.jpg

28.5 KB
Loading

demo_images/pyimagesearch_logo.jpg

12.7 KB
Loading

demo_images/workspace.jpg

32.9 KB
Loading

docs/images/matplotlib.png

901 KB
Loading

docs/images/resizing.png

536 KB
Loading

docs/images/rotation.png

1.17 MB
Loading

docs/images/skeletonization.png

316 KB
Loading

docs/images/translation.png

644 KB
Loading

docs/images/translation_eq.png

641 Bytes
Loading

imutils/.DS_Store

0 Bytes
Binary file not shown.

imutils/__init__.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
# author: Adrian Rosebrock
22
# website: http://www.pyimagesearch.com
33

4-
PEP8
5-
64
# import the necessary packages
75
import numpy as np
86
import cv2
97

108
def translate(image, x, y):
11-
# Define the translation matrix and perform the translation
9+
# define the translation matrix and perform the translation
1210
M = np.float32([[1, 0, x], [0, 1, y]])
1311
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
1412

15-
# Return the translated image
13+
# return the translated image
1614
return shifted
1715

1816
def rotate(image, angle, center=None, scale=1.0):
19-
# Grab the dimensions of the image
17+
# grab the dimensions of the image
2018
(h, w) = image.shape[:2]
2119

22-
# If the center is None, initialize it as the center of
20+
# if the center is None, initialize it as the center of
2321
# the image
2422
if center is None:
2523
center = (w / 2, h / 2)
2624

27-
# Perform the rotation
25+
# perform the rotation
2826
M = cv2.getRotationMatrix2D(center, angle, scale)
2927
rotated = cv2.warpAffine(image, M, (w, h))
3028

31-
# Return the rotated image
29+
# return the rotated image
3230
return rotated
3331

3432
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
@@ -57,7 +55,7 @@ def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
5755
dim = (width, int(h * r))
5856

5957
# resize the image
60-
resized = cv2.resize(image, dim, interpolation = inter)
58+
resized = cv2.resize(image, dim, interpolation=inter)
6159

6260
# return the resized image
6361
return resized

imutils/__init__.pyc

2.29 KB
Binary file not shown.

0 commit comments

Comments
 (0)