def getC1Projection(imp, norm): stack = imp.getStack() projStack = ImageStack(W,H) for t in range(1,T+1): proj = FloatProcessor(W,H) for z in range(1,Z+1): #max intensity projection ip = stack.getProcessor(imp.getStackIndex(1,z,t)).convertToFloatProcessor() proj.copyBits(ip, 0,0, Blitter.MAX) projStack.addSlice(proj) if norm: #Z normalise stats = StackStatistics(ImagePlus("wrapper",projStack)) for t in range(1,T+1): ip = projStack.getProcessor(t) ip.subtract(stats.mean) ip.multiply( 1.0/(stats.stdDev) ) return projStack
def run(): """ Loads an image stack which contains both reference and target images for the registration Scales the images to have their largest side equal to longSide Registration is performed: - translation (brute force optimization) - rotation (brute force optimization) - sift registration - bunwarpj registration Calculation of the errors by different methods """ # load the input stack as an ImagePlus imp = IJ.openImage(filePath.getAbsolutePath()) stack = imp.getStack() sizeZ = imp.getStackSize() LAND_MARKS = 0 # Copy the reference and target slice refId = 1 ref = stack.getProcessor(refId).duplicate() if (Scale == 1): [ref, s] = scaleLongSide(ref, longSide) sizeZ = min(sizeZ, maxSlice) stackReg = ImageStack(ref.getWidth(), ref.getHeight()) stackReg.addSlice(ref) # = stack.duplicate() for i in range(2, sizeZ+1): targetId = i target = stack.getProcessor(targetId).duplicate() # Scale the slices: scale the reference slice using the longSide parameter, and use the same scale for the target slice. if (Scale == 1): target = scale(target, s) #ImagePlus('Ref',ref).show() #ImagePlus('Target',target).show() if (Reg == 1): if (translationReg == 1): target = translation(ref, target) if (rotationReg == 1): [rot, target] = rotationSingle(ref,target,rotationStep) if (siftReg == 1): [roiRef, roiTarget] = siftSingle(ref, target) impTarget = ImagePlus('Target',target) impTarget.setRoi(roiTarget) #impTarget.show() impRef = ImagePlus('Ref',ref) impRef.setRoi(roiRef) #impRef.show() LAND_MARKS = 1 if (bunwarpjReg == 1): target = bunwarpjSingle(impRef, impTarget, LAND_MARKS, 'direct_transfo_' + str(i) + '.txt', 'inverse_transfo_' + str(i) + '.txt') impTarget = ImagePlus('unwarpj_target', target) #impTarget.show() fileName = 'target_id' + str(targetId) + '.tif' IJ.saveAs(impTarget, "Tiff", os.path.join(saveDir.getAbsolutePath(), fileName)) #stackReg.setProcessor(target.convertToShortProcessor(), i) stackReg.addSlice(target) if (calculateError == 1): eCorrelation = zeros(sizeZ, 'f') eMSE = zeros(sizeZ, 'f') eMSE_ROI = zeros(sizeZ, 'f') eRMSE = zeros(sizeZ, 'f') eNRMSE = zeros(sizeZ, 'f') eCVRMSE = zeros(sizeZ, 'f') for i in range(1, sizeZ+1): ip = stackReg.getProcessor(i).duplicate() #ImagePlus('test',ip).show() eCorrelation[i-1], eMSE[i-1], eMSE_ROI[i-1], eRMSE[i-1], eNRMSE[i-1], eCVRMSE[i-1] = measureError(ref,ip) errorFileName = 'error.txt' errorFilePath = os.path.join(saveDir.getAbsolutePath(), errorFileName) writeCSV( errorFilePath, [eCorrelation,eMSE, eMSE_ROI, eRMSE,eNRMSE,eCVRMSE], ["Correlation","MSE","MSE_ROI","RMSE","N_RMSE","CV_RMSE"] )
def run(): """ Loads an image stack which contains both reference and target images for the registration Scales the images to have their largest side equal to longSide Registration is performed: - translation (brute force optimization) - rotation (brute force optimization) - sift registration - bunwarpj registration Calculation of the errors by different methods """ # load the input stack as an ImagePlus imp = IJ.openImage(filePath.getAbsolutePath()) stack = imp.getStack() sizeZ = imp.getStackSize() LAND_MARKS = 0 # Copy the reference and target slice refId = 1 ref = stack.getProcessor(refId).duplicate() if (Scale == 1): [ref, s] = scaleLongSide(ref, longSide) sizeZ = min(sizeZ, maxSlice) stackReg = ImageStack(ref.getWidth(), ref.getHeight()) stackReg.addSlice(ref) # = stack.duplicate() for i in range(2, sizeZ + 1): targetId = i target = stack.getProcessor(targetId).duplicate() # Scale the slices: scale the reference slice using the longSide parameter, and use the same scale for the target slice. if (Scale == 1): target = scale(target, s) #ImagePlus('Ref',ref).show() #ImagePlus('Target',target).show() if (Reg == 1): if (translationReg == 1): target = translation(ref, target) if (rotationReg == 1): [rot, target] = rotationSingle(ref, target, rotationStep) if (siftReg == 1): [roiRef, roiTarget] = siftSingle(ref, target) impTarget = ImagePlus('Target', target) impTarget.setRoi(roiTarget) #impTarget.show() impRef = ImagePlus('Ref', ref) impRef.setRoi(roiRef) #impRef.show() LAND_MARKS = 1 if (bunwarpjReg == 1): target = bunwarpjSingle(impRef, impTarget, LAND_MARKS, 'direct_transfo_' + str(i) + '.txt', 'inverse_transfo_' + str(i) + '.txt') impTarget = ImagePlus('unwarpj_target', target) #impTarget.show() fileName = 'target_id' + str(targetId) + '.tif' IJ.saveAs(impTarget, "Tiff", os.path.join(saveDir.getAbsolutePath(), fileName)) #stackReg.setProcessor(target.convertToShortProcessor(), i) stackReg.addSlice(target) if (calculateError == 1): eCorrelation = zeros(sizeZ, 'f') eMSE = zeros(sizeZ, 'f') eMSE_ROI = zeros(sizeZ, 'f') eRMSE = zeros(sizeZ, 'f') eNRMSE = zeros(sizeZ, 'f') eCVRMSE = zeros(sizeZ, 'f') for i in range(1, sizeZ + 1): ip = stackReg.getProcessor(i).duplicate() #ImagePlus('test',ip).show() eCorrelation[i - 1], eMSE[i - 1], eMSE_ROI[i - 1], eRMSE[ i - 1], eNRMSE[i - 1], eCVRMSE[i - 1] = measureError(ref, ip) errorFileName = 'error.txt' errorFilePath = os.path.join(saveDir.getAbsolutePath(), errorFileName) writeCSV( errorFilePath, [eCorrelation, eMSE, eMSE_ROI, eRMSE, eNRMSE, eCVRMSE], ["Correlation", "MSE", "MSE_ROI", "RMSE", "N_RMSE", "CV_RMSE"])
refImp = IJ.openImage(refpath) width = refImp.width height = refImp.height roim = RoiManager() roim.runCommand("open", roipath) roiArray = roim.getRoisAsArray() nRoi = len(roiArray) roim.close() bwStack = ImageStack(width, height, nRoi) for i in xrange(1, nRoi+1): bwStack.setProcessor(FloatProcessor(width, height, zeros('f', width * height), None), i) for i in xrange(1, nRoi+1): roi = roiArray[i-1] fp = bwStack.getProcessor(i) fp.setValue(1.0) fp.fill(roi) roiImp = ImagePlus("roi", bwStack) outfn = "roi_" + os.path.splitext(roifn)[0] + ".tif" outpath = os.path.join(roidir, outfn) if os.path.exists(outpath): print "Skipped, already exists: ", outfn else: IJ.saveAsTiff(roiImp, outpath)
refImp = IJ.openImage(refpath) width = refImp.width height = refImp.height roim = RoiManager() roim.runCommand("open", roipath) roiArray = roim.getRoisAsArray() nRoi = len(roiArray) roim.close() bwStack = ImageStack(width, height, nRoi) for i in xrange(1, nRoi + 1): bwStack.setProcessor( FloatProcessor(width, height, zeros('f', width * height), None), i) for i in xrange(1, nRoi + 1): roi = roiArray[i - 1] fp = bwStack.getProcessor(i) fp.setValue(1.0) fp.fill(roi) roiImp = ImagePlus("roi", bwStack) outfn = "roi_" + os.path.splitext(roifn)[0] + ".tif" outpath = os.path.join(roidir, outfn) if os.path.exists(outpath): print "Skipped, already exists: ", outfn else: IJ.saveAsTiff(roiImp, outpath)
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()))
#ipMIP = newMIPImp.getProcessor() #EDM().toEDM( ipMIP.convertToByte(True) ) #imgMIP = IP( "MIP", ipMIP ) #imgMIP.show() #break print 'Converting to 8 bit...' stack = newImp.getStack() for s in range( 0, slices ): ip = stack.getProcessor(s+1) label = stack.getSliceLabel(s+1) ip.resetMinAndMax() newStack.addSlice( label, ip.convertToByte(True) ) IJ.showProgress(s+1, slices) print 'Applying ultimate erosion...' EDMStack = IMGS( imp.getWidth(), imp.getHeight() ) for s in range( 0, slices ): ip = newStack.getProcessor(s+1) EDM().toEDM( ip ) #EDM().run( ip ) EDMStack.addSlice( label, ip ) IJ.showProgress(s+1, slices) imp.setStack( EDMStack ) imp.show() #fs = FileSaver( imp ) #print "Saving to", outputPath #fs.saveAsTiff( outputPath )
# ACTUAL SCRIPT imp1 = IJ.openImage(sys.argv[1]) imp2 = IJ.openImage(sys.argv[2]) # stabilize stack = ImageStack(imp1.width, imp1.height) stack.addSlice('', imp1.getProcessor()) stack.addSlice('', imp2.getProcessor()) stackimp = ImagePlus('stack', stack) WindowManager.setTempCurrentImage(stackimp) IJ.run( stackimp, "Image Stabilizer", "transformation=Affine maximum_pyramid_levels=4 template_update_coefficient=0.90 maximum_iterations=20000 error_tolerance=0.0000001" ) stack = stackimp.getImageStack() stabilized1 = ImagePlus('stabilized1', stack.getProcessor(1)) stabilized2 = ImagePlus('stabilized2', stack.getProcessor(2)) # subtract ic = ImageCalculator() diff = ic.run('Subtract create', stabilized1, stabilized2) # smudge diff.getProcessor().blurGaussian(2) # enhance ;) hysteresis = ImagePlus('hysteresis', hyst(diff.getProcessor(), HYST_HI, HYST_LO)) # invert hysteresis.getProcessor().invert() # count worms rt = ResultsTable() pa = ParticleAnalyzer(SHOW_NONE, 0, rt, MIN_SIZE, MAX_SIZE, MIN_CIRC, MAX_CIRC) pa.analyze(hysteresis)