Exemplo n.º 1
0
def toRadialShape(im, s=1024, smooth=501):

	"""
	desc:
		Extracts the radial outline from an image.

	argumens:
		im:
			desc:	An image with 0 for background and 1 for shape.
			type:	ndarray

	keywords:
		s:
			desc:	The length of the output arrays.
			type:	int
		smooth:
			desc:	Smoothing window for the radius or None for no smoothing.
			type:	[int, NoneType]

	returns:
		desc:	A tuple with an array of angles and an array of radii.
		type:	tuple
	"""

	im = edgeDetect(im)
	cx = im.shape[0]/2
	cy = im.shape[1]/2
	x, y = np.where(im != 0)
	a = np.arctan2(y-cy, x-cx)
	r = np.sqrt((x-cx)**2+(y-cy)**2)
	a = np.concatenate( (a-2*np.pi, a, a+2*np.pi) )
	r = np.concatenate( (r, r, r ) )
	i = np.argsort(a)
	a = a[i]
	r = r[i]
	if smooth != None:
		r = tk.smooth(r, windowLen=smooth)
	f = interpolate.interp1d(a, r)
	ia = np.linspace(-np.pi, np.pi, s)
	ir = f(ia)
	return ia, ir
Exemplo n.º 2
0
	def __finishTrial__(self, trialDict):

		"""
		Perform some finalization after we we have parsed a trial

		Arguments:
		trialDict -- a trial dictionary
		"""

		nPhase = len(self.traceDict)
		i = 1
		if self.traceImg or self.tracePlot:
			plt.clf()
			plt.close()
			plt.figure(figsize=(12,12))
			plt.subplots_adjust(hspace=.5, wspace=.5)
		for phase, trace in self.traceDict.iteritems():
			a = np.array(trace, dtype=float)
			if len(a) == 0:
				continue
			origA = a.copy()
			if self.blinkReconstruct:
				a[:,2] = TraceKit.blinkReconstruct(a[:,2])
			if self.traceSmoothParams != None:
				a[:,0] = TraceKit.smooth(a[:,0], **self.traceSmoothParams)
				a[:,1] = TraceKit.smooth(a[:,1], **self.traceSmoothParams)
				a[:,2] = TraceKit.smooth(a[:,2], **self.traceSmoothParams)
			self.traceDict[phase] = a
			path = os.path.join(self.traceFolder, '%s-%.5d-%s.npy' \
				% (trialDict['file'], trialDict['trialId'], phase))
			if not os.path.exists(self.traceFolder):
				print('Creating traceFolder: %s' % self.traceFolder)
				os.makedirs(self.traceFolder)

			np.save(path, a)
			trialDict['__trace_%s__' % phase] = path
			if self.traceImg or self.tracePlot:
				plt.subplot(nPhase, 3, i)
				i += 1
				plt.title('X(%s)' % phase)
				plt.plot(a[:,0])
				if self.traceSmoothParams != None:
					plt.plot(origA[:,0])
				plt.subplot(nPhase, 3, i)
				i += 1
				plt.title('Y(%s)' % phase)
				plt.plot(a[:,1])
				if self.traceSmoothParams != None:
					plt.plot(origA[:,1])
				plt.subplot(nPhase, 3, i)
				i += 1
				plt.title('Pupil(%s)' % phase)
				plt.plot(a[:,2])
				if self.traceSmoothParams != None or self.blinkReconstruct:
					plt.plot(origA[:,2])
		if self.traceImg or self.tracePlot:
			plt.suptitle(path)
			if self.traceImg:
				path = os.path.join(self.traceFolder, 'png', '%s-%.5d-%s.png' \
					% (trialDict['file'], trialDict['trialId'], phase))
				if not os.path.exists(os.path.join(self.traceFolder, 'png')):
					print('Creating traceImgFolder: %s' % os.path.join( \
						self.traceFolder, 'png'))
				if not os.path.exists(os.path.join(self.traceFolder, 'png')):
					os.makedirs(os.path.join(self.traceFolder, 'png'))
				plt.savefig(path)
			if self.tracePlot:
				plt.show()