Exemple #1
0
def anaglyph(image, depth, clContext, clQueue):
    if not hasattr(infiniteFocus, "program"):
        kernelFile = open('src/kernels/anaglyph.cl', 'r')
        anaglyph.program = cl.Program(clContext, kernelFile.read()).build()
        kernelFile.close()

    mf = cl.mem_flags

    nd_image.gaussian_filter(depth, 20, output=depth)

    depthBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=depth)

    image = PILToNumpy(NumpyToPIL(image).convert("L").convert("RGB"))

    outputR = numpy.zeros(depth.shape).astype(numpy.uint8)
    outputG = numpy.zeros(depth.shape).astype(numpy.uint8)
    outputB = numpy.zeros(depth.shape).astype(numpy.uint8)
    outputRBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=outputR)
    outputGBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=outputG)
    outputBBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=outputB)

    r, g, b = [a.copy() for a in image.transpose(2, 0, 1)]

    rBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=r)
    gBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=g)
    bBuffer = cl.Buffer(clContext, mf.READ_WRITE | mf.COPY_HOST_PTR, hostbuf=b)

    anaglyph.program.anaglyph(clQueue, [depth.size], None, rBuffer, outputRBuffer, depthBuffer, numpy.uint32(image.shape[1]), numpy.uint32(image.shape[0]), numpy.int32(-1)).wait()
    anaglyph.program.anaglyph(clQueue, [depth.size], None, gBuffer, outputGBuffer, depthBuffer, numpy.uint32(image.shape[1]), numpy.uint32(image.shape[0]), numpy.int32(1)).wait()
    anaglyph.program.anaglyph(clQueue, [depth.size], None, bBuffer, outputBBuffer, depthBuffer, numpy.uint32(image.shape[1]), numpy.uint32(image.shape[0]), numpy.int32(1)).wait()

    cl.enqueue_read_buffer(clQueue, outputRBuffer, outputR).wait()
    cl.enqueue_read_buffer(clQueue, outputGBuffer, outputG).wait()
    cl.enqueue_read_buffer(clQueue, outputBBuffer, outputB).wait()

    rgb = numpy.array((outputR, outputG, outputB)).transpose(1, 2, 0)

    return rgb
Exemple #2
0
def mpt(im_in, thresh = 1.0, im_start=0, im_end = None, **kwargs):
	"""Multiple Particle tracking.  
	Using the stacked image from im_in, finds particles and localizes them 
	with the fitting2d function
		
	Return
	------
	a list of dictionaries, each of which contains keys whose values are:
		'numspots':	Number of spots detected
		'x2': 		array of x coordinates of the spots
		'y2': 		array of y coordinates of the spots
		'varx': 	array of width of the fit in x
		'vary': 	array of width of the fit in y
		'offset': 	array of level of the background
		'amp':		array of height of the fit above background
		'exits':	array of return values from the fitting
		 
	Optional Keyword Arguments
	--------------------------
	DEBUG : Outputs debugging information, especially graphs; Default is False
	smooth : use a gaussian blur to smooth the data (?why); Default is False
	abs_thresh : 
	
	"""
	
	smooth = False
	abs_thresh = False
	DEBUG = False
	padding = 7
	
	for key in kwargs:
		if key == "smooth": smooth = kwargs[key]
		elif key == "abs_thresh": abs_thresh = kwargs[key]
		elif key == "frame_num" : frame_num = kwargs[key]
		elif key == "DEBUG": DEBUG = kwargs[key]
		elif key == "padding": padding = kwargs[key]
		
	
	if(im_end == None): 
		if len(shape(im_in)) == 3: 
			im_end = shape(im_in)[2]
		else: 
			# Reshape to a stack if only a 1-D image
			im_end = 1 
			im_in = reshape(im_in, (shape(im_in)[0], shape(im_in)[1], 1))
	
	coords = [None]*(im_end - im_start)
	for i in range(im_start, im_end):
		if DEBUG > 0: print i, "of ", im_end - im_start
		A = im_in[:,:,i]
		
		if smooth: bA = ndi.gaussian_filter(A, 2)
		else: bA = A
		
		if abs_thresh: 
			T = 1
		else: 
			T = otsu_threshold(bA)
		
		
		Amask = bA > (T * thresh)
		
		l, n = ndi.label(Amask)
		
		if DEBUG > 1:
			ion()
			
			figure(1138)
			imshow(Amask*bA, cmap = cm.gray)
			colorbar()
			title('Mask: Foo > %f' % (T * thresh))
			print T
			print thresh
			print "Number of spots found: ", n
		
		if n == 0:
			x2 = []
			y2 = []
			varx = []
			vary = []
			amp = []
			offset = []
			exits = []
		else:
			D = ndi.find_objects(l)    # Generates a list of slices
			
			# Set up empty variables
			x2 = zeros(n)
			y2 = zeros(n)
			varx = zeros(n)
			vary = zeros(n)
			amp = zeros(n)
			offset = zeros(n)
			exits = zeros(n)
			
			
			for j in range(n):
				row, col = D[j]

                # Make sure fitbox is legal
				rstart = max(0, row.start - padding)
				rend = min(shape(im_in)[0]-1, row.stop + padding)
				cstart = max(0, col.start - padding)
				cend = min(shape(im_in)[1]-1, col.stop + padding)
				
                # Take fitbox and subtract background
				B = im_in[rstart:rend, cstart:cend, i]
				B = B - B.min()
				
                # Coordinates for fitting
				dy, dx = mgrid[rstart:rend, cstart:cend]
								
				if DEBUG > 2:
					figure()
					pcolor(dx, dy, B)
					colorbar()
				try:
					fit, exit = fit2d(dx, dy, B)
					x2[j] = fit[0]
					y2[j] = fit[1]
					varx[j] = fit[2]
					vary[j] = fit[3]
					offset[j] = fit[4]
					amp[j] = fit[5]
					exits[j] = exit

                    # If fit is too close to the edge of the frame, then ignore it
                    if not (padding < x2[j] < shape(im_in)[0] - padding) \
                       or not (padding < y2[j] < shape(im_in)[1] - padding):
                        x2[j] = float("nan")
                        y2[j] = float("nan")
                        exits[j] = -1

					if DEBUG > 1:
						print "Fitted a point at:", fit
				except ValueError, err:
					print "Value error", err
					exits[j] = -1138