def translate_single_stack_using_imglib2(imp, dx, dy, dz): # wrap into a float imglib2 and translate # conversion into float is necessary due to "overflow of n-linear interpolation due to accuracy limits of unsigned bytes" # see: https://github.com/fiji/fiji/issues/136#issuecomment-173831951 img = ImagePlusImgs.from(imp.duplicate()) extended = Views.extendBorder(img) converted = Converters.convert(extended, RealFloatSamplerConverter()) interpolant = Views.interpolate(converted, NLinearInterpolatorFactory()) # translate if imp.getNDimensions()==3: transformed = RealViews.affine(interpolant, Translation3D(dx, dy, dz)) elif imp.getNDimensions()==2: transformed = RealViews.affine(interpolant, Translation2D(dx, dy)) else: IJ.log("Can only work on 2D or 3D stacks") return None cropped = Views.interval(transformed, img) # wrap back into bit depth of input image and return bd = imp.getBitDepth() if bd==8: return(ImageJFunctions.wrapUnsignedByte(cropped,"imglib2")) elif bd == 16: return(ImageJFunctions.wrapUnsignedShort(cropped,"imglib2")) elif bd == 32: return(ImageJFunctions.wrapFloat(cropped,"imglib2")) else: return None
def translate_using_imglib2(imp, dx, dy, dz): print "imp channels",imp.getNChannels() # todo: # if multiple channels use Duplicator to translate each channel individually ## wrap # http://javadoc.imagej.net/ImgLib2/net/imglib2/img/imageplus/ImagePlusImg.html img = ImagePlusImgs.from(imp.duplicate()) print "dimensions:",img.numDimensions() print img.getChannels() ## prepare image print "img",img ddd extended = Views.extendBorder(img) #print "extended",extended #print "extended",extended.dimension(1) dims = zeros(4, 'l') img.dimensions(dims) print "dims",dims converted = Converters.convert(extended, RealFloatSamplerConverter()) composite = Views.collapseReal(converted, imp.getNChannels()) print "composite",composite interpolant = Views.interpolate(composite, NLinearInterpolatorFactory()) #print "interpolant",interpolant transformed = RealViews.affine(interpolant, Translation3D(dx, dy, dz)) print "transformed", transformed cropped = Views.interval(transformed, img) print "cropped.numDimensions()", cropped.numDimensions() print "cropped",cropped ## wrap back and return bd = imp.getBitDepth() # maybe simply wrap works? if bd==8: return(ImageJFunctions.wrapUnsignedByte(cropped,"imglib2")) elif bd == 16: return(ImageJFunctions.wrapUnsignedShort(cropped,"imglib2")) elif bd == 32: return(ImageJFunctions.wrapFloat(cropped,"imglib2")) else: return None
redgreen= ops.add(red32,green32) display.createDisplay("redgreen", data.create(redgreen)) # make a copy of the red + green image copy=redgreen.copy() # wrap as ImagePlus imp=ImageJFunctions.wrap(copy, "wrapped") # create and call background subtractor bgs=BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap as Img and display iplus=ImagePlus("bgs", imp.getProcessor()) print type(imp) imgBgs=ImageJFunctions.wrapFloat(iplus) display.createDisplay("back_sub", data.create(ImgPlus(imgBgs))) kernel = DetectionUtils.createLoGKernel( 3.0, 2, array([1.0, 1.0], 'd' ) ) print type(kernel) print type(imgBgs) print type(red32.getImg()) log = ops.convolve(ops.create( dimensions2D, FloatType()), imgBgs, kernel) display.createDisplay("log", data.create(ImgPlus(log))) otsu=ops.run("threshold", ops.create( dimensions2D, BitType()), log, Otsu()) display.createDisplay("thresholded", data.create(ImgPlus(otsu)))
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()))
display.createDisplay("redgreen", data.create(redgreen)) # make a copy of the red + green image copy = redgreen.copy() # wrap as ImagePlus imp = ImageJFunctions.wrap(copy, "wrapped") # create and call background subtractor bgs = BackgroundSubtracter() bgs.rollingBallBackground(imp.getProcessor(), 50.0, False, False, True, True, True) # wrap as Img and display iplus = ImagePlus("bgs", imp.getProcessor()) print type(imp) imgBgs = ImageJFunctions.wrapFloat(iplus) display.createDisplay("back_sub", data.create(ImgPlus(imgBgs))) kernel = DetectionUtils.createLoGKernel(3.0, 2, array([1.0, 1.0], 'd')) print type(kernel) print type(imgBgs) print type(red32.getImg()) log = ops.convolve(ops.create(dimensions2D, FloatType()), imgBgs, kernel) display.createDisplay("log", data.create(ImgPlus(log))) otsu = ops.run("threshold", ops.create(dimensions2D, BitType()), log, Otsu()) display.createDisplay("thresholded", data.create(ImgPlus(otsu)))