Skip to content

Instantly share code, notes, and snippets.

@zamber
Last active April 3, 2017 17:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zamber/0030e4ac39e39a8fcfab0ec8c9e713ac to your computer and use it in GitHub Desktop.
Parse images in directories and spew out sharpness and brightness for each one (as naive CSV)
#!/usr/bin/env python2.7
'''
Based on:
http://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
opencv is available for python2.7 on Ubuntu only (in default repos)
hence pip2 and above hashbang
# Installation
sudo apt install python-opencv
sudo pip2 install numpy
sudo pip2 install imutils
It's Dangerous to Go Alone! Take This
(to drop to the interpreter in-line):
import code
code.interact(local=dict(globals(), **locals()))
'''
import sys
import argparse
from imutils import paths
import cv2
import numpy
class MyParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
sys.exit(2)
def create_parser():
parser = MyParser(description='Uber (sharp/bright)-ness CSVer')
parser.add_argument(
'image_dirs',
metavar='IMAGE_DIRS',
type=str,
nargs="+",
help='Directories containing images to parse and spew out'
)
return parser
def analize_image(image_path):
# load the image, convert it to grayscale, and compute the
# focus measure of the image using the Variance of Laplacian
# method
image = cv2.imread(image_path.replace('\\', ''))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# compute the Laplacian of the image and then return the
# focus measure, which is the variance of the Laplacian
fm = cv2.Laplacian(gray, cv2.CV_64F).var()
# convert image to HSV, split out V, calculate a histogram
# for V (which is the value for brightness), then get a
# median for it
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
v = cv2.split(hsv)
br_hist = cv2.calcHist(v, [2], None, [256], [0,256])
br_median = numpy.median(br_hist)
return image_path, fm, br_median
if (__name__ == '__main__'):
parser = create_parser()
args = parser.parse_args()
print '"%s", "%s", "%s"' % ("image", "sharpness", "brightness")
for directory in args.image_dirs:
for image_path in paths.list_images(directory):
# Professional Output Generator (TM)
print '"%s", "%s", "%s"' % (analize_image(image_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment