|
1 | 1 | # imutils
|
2 | 2 | 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 | + |
| 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;"> |
0 commit comments