-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of the imutils package
- Loading branch information
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Adrian Rosebrock, http://www.pyimagesearch.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# author: Adrian Rosebrock | ||
# website: http://www.pyimagesearch.com | ||
|
||
PEP8 | ||
|
||
# import the necessary packages | ||
import numpy as np | ||
import cv2 | ||
|
||
def translate(image, x, y): | ||
# Define the translation matrix and perform the translation | ||
M = np.float32([[1, 0, x], [0, 1, y]]) | ||
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) | ||
|
||
# Return the translated image | ||
return shifted | ||
|
||
def rotate(image, angle, center=None, scale=1.0): | ||
# Grab the dimensions of the image | ||
(h, w) = image.shape[:2] | ||
|
||
# If the center is None, initialize it as the center of | ||
# the image | ||
if center is None: | ||
center = (w / 2, h / 2) | ||
|
||
# Perform the rotation | ||
M = cv2.getRotationMatrix2D(center, angle, scale) | ||
rotated = cv2.warpAffine(image, M, (w, h)) | ||
|
||
# Return the rotated image | ||
return rotated | ||
|
||
def resize(image, width=None, height=None, inter=cv2.INTER_AREA): | ||
# initialize the dimensions of the image to be resized and | ||
# grab the image size | ||
dim = None | ||
(h, w) = image.shape[:2] | ||
|
||
# if both the width and height are None, then return the | ||
# original image | ||
if width is None and height is None: | ||
return image | ||
|
||
# check to see if the width is None | ||
if width is None: | ||
# calculate the ratio of the height and construct the | ||
# dimensions | ||
r = height / float(h) | ||
dim = (int(w * r), height) | ||
|
||
# otherwise, the height is None | ||
else: | ||
# calculate the ratio of the width and construct the | ||
# dimensions | ||
r = width / float(w) | ||
dim = (width, int(h * r)) | ||
|
||
# resize the image | ||
resized = cv2.resize(image, dim, interpolation = inter) | ||
|
||
# return the resized image | ||
return resized | ||
|
||
def skeletonize(image, size, structuring=cv2.MORPH_RECT): | ||
# determine the area (i.e. total number of pixels in the image), | ||
# initialize the output skeletonized image, and construct the | ||
# morphological structuring element | ||
area = image.shape[0] * image.shape[1] | ||
skeleton = np.zeros(image.shape, dtype="uint8") | ||
elem = cv2.getStructuringElement(structuring, size) | ||
|
||
# keep looping until the erosions remove all pixels from the | ||
# image | ||
while True: | ||
# erode and dilate the image using the structuring element | ||
eroded = cv2.erode(image, elem) | ||
temp = cv2.dilate(eroded, elem) | ||
|
||
# subtract the temporary image from the original, eroded | ||
# image, then take the bitwise 'or' between the skeleton | ||
# and the temporary image | ||
temp = cv2.subtract(image, temp) | ||
skeleton = cv2.bitwise_or(skeleton, temp) | ||
image = eroded.copy() | ||
|
||
# if there are no more 'white' pixels in the image, then | ||
# break from the loop | ||
if area == area - cv2.countNonZero(image): | ||
break | ||
|
||
# return the skeletonized image | ||
return skeleton | ||
|
||
def opencv2matplotlib(image): | ||
# OpenCV represents images in BGR order; however, Matplotlib | ||
# expects the image in RGB order, so simply convert from BGR | ||
# to RGB and return | ||
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from distutils.core import setup | ||
|
||
setup( | ||
name = 'imutils', | ||
packages = ['imutils'], | ||
version = '0.1', | ||
description = '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.', | ||
author = 'Adrian Rosebrock', | ||
author_email = '[email protected]', | ||
url = 'https://github.com/jrosebr1/imutils', | ||
download_url = 'https://github.com/jrosebr1/imutils/tarball/0.1', | ||
keywords = ['computer vision', 'image processing', 'opencv', 'matplotlib'], | ||
classifiers = [], | ||
) |