// OHSU Ca Imaging - Morphological Segmentation // This is temporary while I figure out how to implement CNMF into a scriptable format. // ================================================================ // Written by Wilson Adams | Siciliano Lab | Sept 2021 // ================================================================ // Assumed stack to process is already open stk = getTitle(); Stack.getDimensions(width, height, channels, slices, frames); /*f_avg = 50; if (frames<50) { f_avg = frames; run("Z Project...", "stop=&f_avg projection=[Average Intensity]"); } else {} */ // Make an average intensity projection avgip = getTitle(); run("Enhance Contrast", "saturated=0.35"); // Generate a copy of image to bandpass filter run("Duplicate...", avgip+"_filt"); filt = getTitle(); FFC_OHSU(); run("Median...", "radius=4"); run("Subtract Background...", "rolling=5 "); run("Gaussian Blur...", "sigma=6"); run("Bandpass Filter...", "filter_large=15 filter_small=8 suppress=None tolerance=5 autoscale saturate"); //run("Morphological Filters", "operation=[White Top Hat] element=Disk radius=9"); filt = getTitle(); run("Duplicate...", avgip+"_mask"); setAutoThreshold("Otsu dark"); run("Threshold..."); run("Convert to Mask"); mask = getTitle(); run("Distance Transform Watershed", "distances=[Weights (5,7)] output=[16 bits] normalize dynamic=1 connectivity=8"); roimap = getTitle(); run("Duplicate...", avgip+"_dist"); setThreshold(1, 65535); setOption("BlackBackground", false); run("Convert to Mask"); run("Analyze Particles...", "size=20-Infinity clear add"); run("Set Measurements...", "area mean centroid redirect=None decimal=3"); selectWindow(avgip); roiManager("show none"); roiManager("measure"); //run("Tile"); /* // Prompt Saving Dialog.create("Save Measurements?"); Dialog.addChoice("Do you want to save the measurments from this script to the image directory?", Array.concat("yes", "no"), "yes"); Dialog.show(); saveit = Dialog.getChoice(); // Save stuff if prompted if (matches(saveit, "yes")) { saveAs("Results", dir+name+"_Results.csv"); roiManager("save", dir+name+"_Roiset.zip"); } */ // =================================== function FFC_OHSU() { // Automated Flatfield Correction Macro // Post-hoc flatfield correction for fluorescence microscopy images. // Effectively a lowpass filter. Blurs image with a large gaussian kernel, // subtracts blurred image from starting image, adding back the mean // of the blurred image to re-establish baseline/bkgd fluorescence. // Intended to be applied to static images, timeseries images, and z-stacks. // Not intented for application to multiple color channels. // run("Subtract Background...") works better for that. // Generates new image/stack based on most recent selected image/stack. // Written by Wilson Adams, Vanderbilt Biophotonics Center, July 2019 start = getTitle() getDimensions(width, height, channels, slices, frames); //print(height); print(width) getVoxelSize(vwidth, vheight, vdepth, vunit) blur= floor(width*0.2); Dialog.create("Set Blur Kernel") Dialog.addMessage("Enter Blur Kernel Size") Dialog.addNumber("Blur Kernel (px) = ", blur) Dialog.show() blur = Dialog.getNumber() if (frames>1) { // For time series where the flat field is relatively stable run("Z Project...", "stop=5 projection=[Average Intensity]"); avgBkgd = getTitle(); run("Gaussian Blur...", "sigma=&blur"); getStatistics(area, mean, min, max, std, histogram); mean = floor(mean); run("Calculator Plus","i1=&start i2=&avgBkgd operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=&mean create"); rename(start+"_bkgdSub"); stk_bkgd = getTitle(); } if (slices>1) { // for z stacks where each image may have a different Flat field run("Duplicate...", "duplicate"); avgBkgd = getTitle(); run("Gaussian Blur...", "sigma=&blur stack"); getStatistics(area, mean, min, max, std, histogram); mean = floor(mean); run("Calculator Plus","i1=&start i2=&avgBkgd operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=&mean create"); rename(start+"_bkgdSub"); stk_bkgd = getTitle(); } else { // Single images run("Duplicate...", start+"_bkgdsub"); avgBkgd = getTitle(); run("Gaussian Blur...", "sigma=&blur"); getStatistics(area, mean, min, max, std, histogram); mean = floor(mean); run("Calculator Plus","i1=&start i2=&avgBkgd operation=[Subtract: i2 = (i1-i2) x k1 + k2] k1=1 k2=&mean create"); rename(start+"_bkgdSub"); stk_bkgd = getTitle(); } close(avgBkgd); selectWindow(stk_bkgd); setVoxelSize(vwidth, vheight, vdepth, vunit); }