Example #1
0
def extractChannel(imp, nChannel, nFrame):
    """ Extract a stack for a specific color channel and time frame """
    stack = imp.getImageStack()
    ch = ImageStack(imp.width, imp.height)
    for i in range(1, imp.getNSlices() + 1):
        index = imp.getStackIndex(nChannel, i, nFrame)
        ch.addSlice(str(i), stack.getProcessor(index))
    stack_to_return = ImagePlus("Channel " + str(nChannel), ch)
    stack_to_return.copyScale(imp)
    return stack_to_return
Example #2
0
def decon(seriesNumber, imgP, psfP):	
	nChannels = imgP.getNChannels()	
	
	if nChannels>1:
		imgPc = ChannelSplitter.split(imgP)
	else:
		imgPc = [imgP]
	
	if psfP.getNChannels()>1:
		psfPc = ChannelSplitter.split(psfP)

		if len(psfPc)<nChannels:
			log("PSF image has fewer channels! Skipping image's trailing channels {} to {}".format(psfP.getNChannels()+1,nChannels))
			imgPc = imgPc[:psfP.getNChannels()]
	else:
		psfPc = [psfP]*nChannels
		
	nChannels = len(imgPc)
	if combinedChannels:
		deconChannels = None
		
	for channelNumber in range(nChannels):
		log("Processing image {} series {} channel {}..".format(inputFile.getAbsolutePath(),seriesNumber,channelNumber))
	
		imgP = imgPc[channelNumber]
		psfP = psfPc[channelNumber]
		
		img = ImageJFunctions.wrap(imgP)
		psf = ImageJFunctions.wrap(psfP)
		
		# convert to float (TODO: make sure deconvolution op works on other types)
		imgF=ops.convert().float32(img)
		psfF=ops.convert().float32(psf)
		
		# make psf same size as image
		psfSize=FinalDimensions([img.dimension(0), img.dimension(1), img.dimension(2)]);
		
		# add border in z direction
		#borderSize=[0,0,psfSize.dimension(2)/2];
		borderSize=[0,0,0];
		
		deconvolved = ops.deconvolve().richardsonLucy(imgF, psfF, numIterations);

		# Create the ImagePlus, copy scale and dimensions
		deconvolvedImp = ImageJFunctions.wrapFloat(deconvolved,inputFile.getName()+'-series{}-channel{}-deconvolvedWith-{}-{}iterations.tiff'.format(seriesNumber,channelNumber,psfFile.getName(),numIterations))
		deconvolvedImp.copyScale(imgP)
		width, height, nChannelz, nSlices, nFrames = imgP.getDimensions()
		deconvolvedImp.setDimensions(nChannelz, nSlices, nFrames)

		if combinedChannels:
			if deconChannels is None:
				deconChannels = ImageStack(width,height)
			
			for z in range(nSlices):
				deconChannels.addSlice(deconvolvedImp.getStack().getProcessor(z))
		else:
			IJ.saveAsTiff(deconvolvedImp,os.path.join(outputDirectory.getAbsolutePath(),deconvolvedImp.getTitle()))
	
	if combinedChannels:
		final = ImageStack(width,height)
		for z in range(nSlices):
			for c in range(nChannels):
				i = c*nSlices+z
				final.addSlice(deconChannels.getProcessor(i+1))
				
		hyperstack = ImagePlus(inputFile.getName()+'-series{}-deconvolvedWith-{}-{}iterations.tiff'.format(seriesNumber,psfFile.getName(),numIterations),final)
		hyperstack = HyperStackConverter.toHyperStack(hyperstack,nChannels,nSlices,1)
		hyperstack.copyScale(imgP)
		IJ.saveAsTiff(hyperstack,os.path.join(outputDirectory.getAbsolutePath(),hyperstack.getTitle()))