# Path: pyKinectTools/utils/DepthUtils.py
# def posImage2XYZ(im_in, min_=500, max_=4000):
#     # Used to convert 8bit depth image to a
#     # 3 dimensional floating point image w/ x,y,z
#     imOut = np.zeros([im_in.shape[0],im_in.shape[1],3], dtype=float)
#     if im_in.sum() == 0:
#         return imOut
# 
#     imOut[:,:,2] = im_in
#     imOut[:,:,2] *= float((max_-min_)/256.0)
#     imOut[:,:,2] += min_
#     imOut[:,:,2] *= imOut[:,:,2]>min_
# 
#     inds = np.nonzero(imOut[:,:,2]>min_)
#     xyzVals = depth2world(np.array([inds[0],inds[1],imOut[inds[0],inds[1],2]]).T)
# 
#     # imOut[inds] = xyzVals
#     imOut[inds[0],inds[1],0] = xyzVals[:,0]
#     imOut[inds[0],inds[1],1] = xyzVals[:,1]
#     # imOut[inds[0],inds[1],0] = xyzVals[:,1]
#     # imOut[inds[0],inds[1],1] = xyzVals[:,0]
#     imOut[inds[0],inds[1],2] = xyzVals[:,2]
# 
#     return imOut
# 
# Path: pyKinectTools/algs/BackgroundSubtraction.py
# def extract_people(im, minPersonPixThresh=5000, gradThresh=None):
# 	'''
# 	---Paramaters---
# 	im :
# 	mask :
# 	minPersonPixThresh :
# 	gradThresh :
# 	gradientFilter :
# 
# 	---Returns---
# 	mask :
# 	userBoundingBoxes :
# 	userLabels :
# 	'''
# 	mask = im!=0
# 	if gradThresh == None:
# 		grad_bin = mask
# 	else:
# 		gradients = np.gradient(im)
# 		mag = np.sqrt(gradients[0]**2+gradients[1]**2)
# 		mask *= mag < gradThresh
# 
# 		# grad_g = np.max(np.abs(np.gradient(im.astype(np.int16))), 0)
# 		# grad_bin = (np.abs(grad_g) < gradThresh)
# 		# mask = mask*grad_bin# np.logical_and(mask[:,:-1],grad_bin)# np.logical_not(grad_bin))
# 
# 	labelIm, maxLabel = nd.label(im*mask)
# 	connComps = nd.find_objects(labelIm, maxLabel)
# 
# 	# Only extract if there are sufficient pixels and it is within a valid height/width ratio
# 	px_count = [nd.sum(labelIm[c]==l) for c,l in zip(connComps,range(1, maxLabel+1))]
# 	ratios = [(c[1].stop-c[1].start)/float(c[0].stop-c[0].start)for c in connComps]
# 	du = [float(c[0].stop-c[0].start) for c in connComps]
# 	dv = [float(c[1].stop-c[1].start) for c in connComps]
# 	areas = [float(c[0].stop-c[0].start)*float(c[1].stop-c[1].start)for c in connComps]
# 	# Filter
# 	usrTmp = [(c,l,px) for c,l,px,ratio,area in zip(connComps,range(1, maxLabel+1), px_count, ratios, areas)
# 				if ratio < 2 and
# 					px > minPersonPixThresh and
# 					px/area > 0.2
# 			]
# 
# 	if len(usrTmp) > 0:
# 		userBoundingBoxes, userLabels, px_count = zip(*usrTmp)
# 	else:
# 		userBoundingBoxes = []
# 		userLabels = []
# 	userCount = len(userLabels)
# 
# 	#Relabel foregound mask with multiple labels
# 	mask = im.astype(np.uint8)*0
# 	for i,i_new in zip(userLabels, range(1, userCount+1)):
# 		mask[labelIm==i] = i_new
# 
# 	return mask, userBoundingBoxes, userLabels, px_count
# 
# def removeNoise(im, thresh=500):
# 	#Thresh is the envelope in the depth dimension within we remove noise
# 	zAvg = im[im[:,:,2]>0,2].mean()
# 	zThresh = thresh
# 	im[im[:,:,2]>zAvg+zThresh] = 0
# 	im[im[:,:,2]<zAvg-zThresh] = 0
# 	im[:,:,2] = nd.median_filter(im[:,:,2], 3)
# 
# 	return im


import os, scipy, time
import scipy.ndimage as nd
import cv, cv2
from pyKinectTools.utils.DepthUtils import posImage2XYZ
from pyKinectTools.algs.PictorialStructures import *
from pyKinectTools.algs.BackgroundSubtraction import extract_people, removeNoise
dataDir = '/Users/colin/data/ICU_7May2012_Close_jpg/diffDraw1/'
# dataDir = '/Users/colin/data/ICU_7May2012_Close_jpg/d1c/'


global bodyPos
global partIndex
global posMat

AllBodyPos = []
bodyPos = []
bodyTimes = []
partIndex = 0


def onclick(event):

	# global regions
	global posMat
	global bodyPos

# Path: pyKinectTools/utils/DepthUtils.py
# def posImage2XYZ(im_in, min_=500, max_=4000):
#     # Used to convert 8bit depth image to a
#     # 3 dimensional floating point image w/ x,y,z
#     imOut = np.zeros([im_in.shape[0],im_in.shape[1],3], dtype=float)
#     if im_in.sum() == 0:
#         return imOut
# 
#     imOut[:,:,2] = im_in
#     imOut[:,:,2] *= float((max_-min_)/256.0)
#     imOut[:,:,2] += min_
#     imOut[:,:,2] *= imOut[:,:,2]>min_
# 
#     inds = np.nonzero(imOut[:,:,2]>min_)
#     xyzVals = depth2world(np.array([inds[0],inds[1],imOut[inds[0],inds[1],2]]).T)
# 
#     # imOut[inds] = xyzVals
#     imOut[inds[0],inds[1],0] = xyzVals[:,0]
#     imOut[inds[0],inds[1],1] = xyzVals[:,1]
#     # imOut[inds[0],inds[1],0] = xyzVals[:,1]
#     # imOut[inds[0],inds[1],1] = xyzVals[:,0]
#     imOut[inds[0],inds[1],2] = xyzVals[:,2]
# 
#     return imOut
# 
# Path: pyKinectTools/algs/BackgroundSubtraction.py
# def extract_people(im, minPersonPixThresh=5000, gradThresh=None):
# 	'''
# 	---Paramaters---
# 	im :
# 	mask :
# 	minPersonPixThresh :
# 	gradThresh :
# 	gradientFilter :
# 
# 	---Returns---
# 	mask :
# 	userBoundingBoxes :
# 	userLabels :
# 	'''
# 	mask = im!=0
# 	if gradThresh == None:
# 		grad_bin = mask
# 	else:
# 		gradients = np.gradient(im)
# 		mag = np.sqrt(gradients[0]**2+gradients[1]**2)
# 		mask *= mag < gradThresh
# 
# 		# grad_g = np.max(np.abs(np.gradient(im.astype(np.int16))), 0)
# 		# grad_bin = (np.abs(grad_g) < gradThresh)
# 		# mask = mask*grad_bin# np.logical_and(mask[:,:-1],grad_bin)# np.logical_not(grad_bin))
# 
# 	labelIm, maxLabel = nd.label(im*mask)
# 	connComps = nd.find_objects(labelIm, maxLabel)
# 
# 	# Only extract if there are sufficient pixels and it is within a valid height/width ratio
# 	px_count = [nd.sum(labelIm[c]==l) for c,l in zip(connComps,range(1, maxLabel+1))]
# 	ratios = [(c[1].stop-c[1].start)/float(c[0].stop-c[0].start)for c in connComps]
# 	du = [float(c[0].stop-c[0].start) for c in connComps]
# 	dv = [float(c[1].stop-c[1].start) for c in connComps]
# 	areas = [float(c[0].stop-c[0].start)*float(c[1].stop-c[1].start)for c in connComps]
# 	# Filter
# 	usrTmp = [(c,l,px) for c,l,px,ratio,area in zip(connComps,range(1, maxLabel+1), px_count, ratios, areas)
# 				if ratio < 2 and
# 					px > minPersonPixThresh and
# 					px/area > 0.2
# 			]
# 
# 	if len(usrTmp) > 0:
# 		userBoundingBoxes, userLabels, px_count = zip(*usrTmp)
# 	else:
# 		userBoundingBoxes = []
# 		userLabels = []
# 	userCount = len(userLabels)
# 
# 	#Relabel foregound mask with multiple labels
# 	mask = im.astype(np.uint8)*0
# 	for i,i_new in zip(userLabels, range(1, userCount+1)):
# 		mask[labelIm==i] = i_new
# 
# 	return mask, userBoundingBoxes, userLabels, px_count
# 
# def removeNoise(im, thresh=500):
# 	#Thresh is the envelope in the depth dimension within we remove noise
# 	zAvg = im[im[:,:,2]>0,2].mean()
# 	zThresh = thresh
# 	im[im[:,:,2]>zAvg+zThresh] = 0
# 	im[im[:,:,2]<zAvg-zThresh] = 0
# 	im[:,:,2] = nd.median_filter(im[:,:,2], 3)
# 
# 	return im


# Path: pyKinectTools/scripts/labelSkeletons.py
import os, scipy, time
import scipy.ndimage as nd
import cv, cv2
from pyKinectTools.utils.DepthUtils import posImage2XYZ
from pyKinectTools.algs.PictorialStructures import *
from pyKinectTools.algs.BackgroundSubtraction import extract_people, removeNoise

dataDir = '/Users/colin/data/ICU_7May2012_Close_jpg/diffDraw1/'
# dataDir = '/Users/colin/data/ICU_7May2012_Close_jpg/d1c/'


global bodyPos
global partIndex
global posMat

AllBodyPos = []
bodyPos = []
bodyTimes = []
partIndex = 0


def onclick(event):

	# global regions
	global posMat
	global bodyPos

	global partIndex