Created
February 24, 2014 20:50
-
-
Save wlach/9196750 to your computer and use it in GitHub Desktop.
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
#!/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