From 3dce2cf2e177d2b969075c954cce05867bfcfbd8 Mon Sep 17 00:00:00 2001 From: Adrian Rosebrock Date: Sun, 11 Jan 2015 15:10:10 -0500 Subject: [PATCH] Initial commit of the imutils package --- LICENSE.txt | 21 ++++++++++ demo.py | 0 imutils/.DS_Store | Bin 0 -> 6148 bytes imutils/__init__.py | 99 ++++++++++++++++++++++++++++++++++++++++++++ setup.cfg | 2 + setup.py | 14 +++++++ 6 files changed, 136 insertions(+) create mode 100644 LICENSE.txt create mode 100644 demo.py create mode 100644 imutils/.DS_Store create mode 100644 imutils/__init__.py create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..b7a6352 --- /dev/null +++ b/LICENSE.txt @@ -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. \ No newline at end of file diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..e69de29 diff --git a/imutils/.DS_Store b/imutils/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a47b8828ae700af7b16146fdd7bd7a55aa375cd7 GIT binary patch literal 6148 zcmeHKF-`+P474Fdq%Vi2dgMVqUHghKocn<6r`ZLig)p|Fk@Rtr-KHG28|-V2*A5*4L@6p#X63h@2V;KW`y zB*v=)Aw~e;4CyeeW0n9mCV;(gNJIwaNd+d=YsB!RBj2j77Y>O@H;x8rQhq3x~v@BOi32J_e|ZObYz90-xBM8@B)e literal 0 HcmV?d00001 diff --git a/imutils/__init__.py b/imutils/__init__.py new file mode 100644 index 0000000..1fde7dd --- /dev/null +++ b/imutils/__init__.py @@ -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) \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..224a779 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5cca9be --- /dev/null +++ b/setup.py @@ -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 = 'adrian@pyimagesearch.com', + url = 'https://github.com/jrosebr1/imutils', + download_url = 'https://github.com/jrosebr1/imutils/tarball/0.1', + keywords = ['computer vision', 'image processing', 'opencv', 'matplotlib'], + classifiers = [], +) \ No newline at end of file