def radon(image, stepsize=2, maskrad=None):
    from multiprocessing import Queue, Process
    t0 = time.time()
    ### prepare mask
    if maskrad is None:
        maskrad = image.shape[0] / 2
    blackcircle = imagefun.filled_circle(image.shape, maskrad)
    mask = 1 - blackcircle

    nsteps = int(math.ceil(180 / stepsize))
    queuelist = []
    for row in range(nsteps):
        angle = -row * stepsize
        queue = Queue()
        queuelist.append(queue)
        #below is equivalent to "project(image, row, angle, mask, queue)"
        proc = Process(target=project, args=(image, row, angle, mask, queue))
        proc.start()
    proc.join()

    ### assemble radon image
    radonimage = numpy.zeros((nsteps, image.shape[0]))
    for queue in queuelist:
        row, line = queue.get()
        radonimage[row, :] = line

    #radonlr = numpy.fliplr(radonimage)
    #radonimage = numpy.vstack((radonimage, radonlr))
    imagefile.arrayToJpeg(radonimage, "radonimage.jpg", msg=False)
    print "Multi radon completed in %s" % (apDisplay.timeString(time.time() -
                                                                t0))
    return radonimage
def radon(image, stepsize=2, maskrad=None):
	from multiprocessing import Queue, Process
	t0 = time.time()
	### prepare mask
	if maskrad is None:
		maskrad = image.shape[0]/2
	blackcircle = imagefun.filled_circle(image.shape, maskrad)
	mask = 1 - blackcircle

	nsteps = int(math.ceil(180/stepsize))
	queuelist = []
	for row in range(nsteps):
		angle = -row*stepsize
		queue = Queue()
		queuelist.append(queue)
		#below is equivalent to "project(image, row, angle, mask, queue)"
		proc = Process(target=project, args=(image, row, angle, mask, queue))
		proc.start()
	proc.join()

	### assemble radon image
	radonimage = numpy.zeros( (nsteps, image.shape[0]) )
	for queue in queuelist:
		row, line = queue.get()
		radonimage[row, :] = line
		
	#radonlr = numpy.fliplr(radonimage)
	#radonimage = numpy.vstack((radonimage, radonlr))
	imagefile.arrayToJpeg(radonimage, "radonimage.jpg", msg=False)
	print "Multi radon completed in %s"%(apDisplay.timeString(time.time() - t0))
	return radonimage
                b = ndimage.shift(b, shift=shift, mode='wrap', order=1)
        bnoise = b + noiselevel*numpy.random.random(shape)
        bnoise = ndimage.median_filter(bnoise, size=2)
        bnoise = imagenorm.normStdev(bnoise)

        return bnoise

if __name__ == "__main__":
        u = mem.used()
        stepsize = 1.0
        shape = (256,256)
        noiselevel = 2.0

        a = createDots(angle=0., noiselevel=noiselevel, shape=shape)
        b = createDots(angle=-30., noiselevel=noiselevel, shape=shape, shift=(3,4))
        imagefile.arrayToJpeg(a, "imagea.jpg")
        imagefile.arrayToJpeg(b, "imageb.jpg")

        ar = radonTransform(a, stepsize)
        br = radonTransform(b, stepsize)
        imagefile.arrayToJpeg(ar, "radona.jpg")
        imagefile.arrayToJpeg(br, "radonb.jpg")

        #print "%.3f"%(a[1,0])
        #print "%.3f"%(b[1,0])
        for radius in range(0,11,2):
                e = radermacher.radonshift(ar,br,radius)
                e = e / (ar.std()*br.std()) # result is not normalized properly
                val = e.argmax()
                y = val%e.shape[1]
                x = int(val/e.shape[1])
	def findEllipseEdge(self, fftarray, mindef=None, maxdef=None):
		if mindef is None:
			mindef = self.params['mindef']
		if maxdef is None:
			maxdef = self.params['maxdef']

		minpeaks = ctftools.getCtfExtrema(maxdef, self.freq*1e10,
			self.ctfvalues['cs'], self.ctfvalues['volts'], 0.0,
			numzeros=3, zerotype="peaks")
		minEdgeRadius = minpeaks[0]

		maxvalleys = ctftools.getCtfExtrema(mindef, self.freq*1e10,
			self.ctfvalues['cs'], self.ctfvalues['volts'], 0.0,
			numzeros=3, zerotype="valleys")

		minEdgeRadius = (maxvalleys[0]+minpeaks[0])/2
		maxEdgeRadius = maxvalleys[2]

		minCircum = math.ceil(2 * minEdgeRadius * math.pi)
		minEdges = int(minCircum)

		maxCircum = math.ceil(2 * maxEdgeRadius * math.pi)
		maxEdges = 2*int(maxCircum)

		dograd = (minpeaks[1] - minpeaks[0])/3.0
		dogarray = apDog.diffOfGauss(fftarray, dograd, k=1.2)
		#dogarray = fftarray

		print "Edge range: ", minEdges, maxEdges

		edges = canny.canny_edges(dogarray, low_thresh=0.01,
			minEdgeRadius=minEdgeRadius, maxEdgeRadius=maxEdgeRadius, minedges=minEdges, maxedges=maxEdges)
		#minedges=2500, maxedges=15000,
		invedges = numpy.fliplr(numpy.flipud(edges))
		self.edgeMap = numpy.logical_and(edges, invedges)
		edges = self.edgeMap * (dogarray.max()/self.edgeMap.max())
		imagedata = dogarray.copy() + edges
		imagefile.arrayToJpeg(imagedata, "edges.jpg")

		edgeThresh = 3
		ellipseParams = ransac.ellipseRANSAC(self.edgeMap, edgeThresh, maxRatio=self.maxRatio)
		if ellipseParams is None:
			return None

		fitEllipse1 = ransac.generateEllipseRangeMap2(ellipseParams, edgeThresh, self.edgeMap.shape)
		fitEllipse2 = ransac.generateEllipseRangeMap2(ellipseParams, edgeThresh*3, self.edgeMap.shape)
		outlineEllipse = fitEllipse2 - fitEllipse1
		# image is draw upside down
		#outlineEllipse = numpy.flipud(outlineEllipse)

		imagedata = imagedata + 0.5*outlineEllipse*imagedata.max()

		if self.debug is True:
			from matplotlib import pyplot
			pyplot.clf()
			pyplot.imshow(imagedata)
			pyplot.gray()
			pyplot.show()

		self.ellipseParams = ellipseParams

		#self.convertEllipseToCtf(node=3)

		return ellipseParams
		b = ndimage.shift(b, shift=shift, mode='wrap', order=1)
	bnoise = b + noiselevel*numpy.random.random(shape)
	bnoise = ndimage.median_filter(bnoise, size=2)
	bnoise = imagenorm.normStdev(bnoise)

	return bnoise

if __name__ == "__main__":
	u = mem.used()
	stepsize = 1.0
	shape = (256,256)
	noiselevel = 2.0

	a = createDots(angle=0., noiselevel=noiselevel, shape=shape)
	b = createDots(angle=-30., noiselevel=noiselevel, shape=shape, shift=(3,4))
	imagefile.arrayToJpeg(a, "imagea.jpg")
	imagefile.arrayToJpeg(b, "imageb.jpg")

	ar = radonTransform(a, stepsize)
	br = radonTransform(b, stepsize)
	imagefile.arrayToJpeg(ar, "radona.jpg")
	imagefile.arrayToJpeg(br, "radonb.jpg")

	#print "%.3f"%(a[1,0])
	#print "%.3f"%(b[1,0])
	for radius in range(0,11,2):
		e = radermacher.radonshift(ar,br,radius)
		e = e / (ar.std()*br.std()) # result is not normalized properly
		val = e.argmax()
		y = val%e.shape[1]
		x = int(val/e.shape[1])