Skip to content

Instantly share code, notes, and snippets.

@calston
Created February 15, 2015 16:51
Show Gist options
  • Save calston/b82e64de639367595084 to your computer and use it in GitHub Desktop.
Save calston/b82e64de639367595084 to your computer and use it in GitHub Desktop.
3dhisto.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
def processStream(fname):
cap = cv2.VideoCapture(fname)
bright = []
frames = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
r = cv2.calcHist([gray], [0], None, [256],[0,256])
cv2.normalize(r, r, 0, 255, cv2.NORM_MINMAX)
frames += 1
if frames > 50:
bright.append(np.ravel(r))
frames = 0
else:
break
cap.release()
y = np.arange(0, len(bright), 1)
x = np.arange(0, 256, 1)
x, y = np.meshgrid(x, y)
z = np.array(bright)
for rn, row in enumerate(z):
if (rn > 5) and (rn < len(z)-5):
for cn, col in enumerate(row):
if (cn > 5) and (cn < len(row)-5):
zf = [i[cn-5:cn+5] for i in z[rn-5:rn+5]]
z[rn][cn] = np.mean(zf)
else:
z[rn][cn] = 0
else:
for cn, col in enumerate(row):
z[rn][cn] = 0
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, rstride=10, cstride=10, cmap=cm.coolwarm, linewidth=0)
ax.set_ylim(0, len(bright))
ax.set_xlim(0, 256)
ax.set_xlabel('Luminosity')
ax.set_zlabel('Frequency')
ax.set_ylabel('Time')
ax.zaxis.set_major_formatter(FormatStrFormatter('%d'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment