Skip to content

Commit

Permalink
Improved code quality based on PEP-8
Browse files Browse the repository at this point in the history
  • Loading branch information
marjinal1st committed Jun 10, 2015
1 parent a269851 commit 1dfc172
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 232 deletions.
170 changes: 167 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,168 @@
imutils/.DS_Store
# Created by https://www.gitignore.io

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/


### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


### OSX ###
.DS_Store
MANIFEST
dist
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*
18 changes: 9 additions & 9 deletions demos/image_basics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com

# USAGE
# BE SURE TO INSTALL 'imutils' PRIOR TO EXECUTING THIS COMMAND
Expand Down Expand Up @@ -34,9 +34,9 @@
# 2. ROTATION
# loop over the angles to rotate the image
for angle in xrange(0, 360, 90):
# rotate the image and display it
rotated = imutils.rotate(bridge, angle=angle)
cv2.imshow("Angle=%d" % (angle), rotated)
# rotate the image and display it
rotated = imutils.rotate(bridge, angle=angle)
cv2.imshow("Angle=%d" % (angle), rotated)

# wait for a keypress, then close all the windows
cv2.waitKey(0)
Expand All @@ -45,9 +45,9 @@
# 3. RESIZING
# loop over varying widths to resize the image to
for width in (400, 300, 200, 100):
# resize the image and display it
resized = imutils.resize(workspace, width=width)
cv2.imshow("Width=%dpx" % (width), resized)
# resize the image and display it
resized = imutils.resize(workspace, width=width)
cv2.imshow("Width=%dpx" % (width), resized)

# wait for a keypress, then close all the windows
cv2.waitKey(0)
Expand Down Expand Up @@ -87,4 +87,4 @@
edgeMap = imutils.auto_canny(gray)
cv2.imshow("Original", logo)
cv2.imshow("Automatic Edge Map", edgeMap)
cv2.waitKey(0)
cv2.waitKey(0)
2 changes: 1 addition & 1 deletion demos/image_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# loop over the image paths in the previous 'demo_images'
# directory and print the paths to the terminal
for imagePath in paths.list_images("../demo_images"):
print imagePath
print imagePath
4 changes: 2 additions & 2 deletions demos/perspective_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# loop over the points and draw them on the cloned image
for (x, y) in pts:
cv2.circle(clone, (x, y), 5, (0, 255, 0), -1)
cv2.circle(clone, (x, y), 5, (0, 255, 0), -1)

# apply the four point tranform to obtain a "birds eye view" of
# the notecard
Expand All @@ -27,4 +27,4 @@
# show the original and warped images
cv2.imshow("Original", clone)
cv2.imshow("Warped", warped)
cv2.waitKey(0)
cv2.waitKey(0)
26 changes: 13 additions & 13 deletions demos/sorting_contours.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com

# USAGE
# BE SURE TO INSTALL 'imutils' PRIOR TO EXECUTING THIS COMMAND
Expand All @@ -19,27 +19,27 @@

# find contours in the edge map
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cv2.CHAIN_APPROX_SIMPLE)

# loop over the (unsorted) contours and label them
for (i, c) in enumerate(cnts):
orig = contours.label_contour(orig, c, i, color=(240, 0, 159))
orig = contours.label_contour(orig, c, i, color=(240, 0, 159))

# show the original image
cv2.imshow("Original", orig)

# loop over the sorting methods
for method in ("left-to-right", "right-to-left", "top-to-bottom", "bottom-to-top"):
# sort the contours
(cnts, boundingBoxes) = contours.sort_contours(cnts, method=method)
clone = image.copy()
# sort the contours
(cnts, boundingBoxes) = contours.sort_contours(cnts, method=method)
clone = image.copy()

# loop over the sorted contours and label them
for (i, c) in enumerate(cnts):
sortedImage = contours.label_contour(clone, c, i, color=(240, 0, 159))
# loop over the sorted contours and label them
for (i, c) in enumerate(cnts):
sortedImage = contours.label_contour(clone, c, i, color=(240, 0, 159))

# show the sorted contour image
cv2.imshow(method, sortedImage)
# show the sorted contour image
cv2.imshow(method, sortedImage)

# wait for a keypress
cv2.waitKey(0)
cv2.waitKey(0)
66 changes: 34 additions & 32 deletions imutils/contours.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com
# author: Adrian Rosebrock
# website: http://www.pyimagesearch.com

# import the necessary packages
import cv2


def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# initialize the reverse flag and sort index
reverse = False
i = 0

# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True

# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1

# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse))

# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b:b[1][i], reverse=reverse))
# return the list of sorted contours and bounding boxes
return cnts, boundingBoxes

# return the list of sorted contours and bounding boxes
return (cnts, boundingBoxes)

def label_contour(image, c, i, color=(0, 255, 0), thickness=2):
# compute the center of the contour area and draw a circle
# representing the center
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# draw the contour and label number on the image
cv2.drawContours(image, [c], -1, color, thickness)
cv2.putText(image, "#{}".format(i + 1), (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX,
1.0, (255, 255, 255), 2)
# return the image with the contour number drawn on it
return image
# compute the center of the contour area and draw a circle
# representing the center
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

# draw the contour and label number on the image
cv2.drawContours(image, [c], -1, color, thickness)
cv2.putText(image, "#{}".format(i + 1), (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX,
1.0, (255, 255, 255), 2)

# return the image with the contour number drawn on it
return image
Loading

0 comments on commit 1dfc172

Please sign in to comment.