Skip to content

Instantly share code, notes, and snippets.

@wlach
Created February 24, 2014 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wlach/9196750 to your computer and use it in GitHub Desktop.
Save wlach/9196750 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import videocapture
import numpy
from scipy import ndimage
import math
import multiprocessing
c = videocapture.Capture(sys.argv[1])
prevframe = None
diffs = []
def _get_sobelized_entropy(i):
frame = c.get_frame(i, True).astype('float')
frame = ndimage.median_filter(frame, 3)
dx = ndimage.sobel(frame, 0) # horizontal derivative
dy = ndimage.sobel(frame, 1) # vertical derivative
frame = numpy.hypot(dx, dy) # magnitude
frame *= 255.0 / numpy.max(frame) # normalize (Q&D)
histogram = numpy.histogram(frame, bins=256)[0]
histogram_length = sum(histogram)
samples_probability = [float(h) / histogram_length for h in histogram]
entropy = -sum([p * math.log(p, 2) for p in samples_probability if p != 0])
return entropy
pool = multiprocessing.Pool(processes=4)
res = pool.map(_get_sobelized_entropy, range(1, c.num_frames+1))
for (i, entropy) in enumerate(res):
print "%s,%s" % (i, entropy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment