/** * MRI Adipocyte Tools * * The Adipocytes Tools help to analyze fat cells in images from histological sections. * * (c) 2020, INSERM * written by Volker Baecker at Montpellier Ressources Imagerie, Biocampus Montpellier, INSERM, CNRS, University of Montpellier (www.mri.cnrs.fr) * * in collaboration with Matthieu Lacroix and Patricia Cavelier */ var helpURL = "https://github.com/MontpellierRessourcesImagerie/imagej_macros_and_scripts/wiki/Adipocytes-Tools" // Preprocessing options var preMinSize = 80; var preMaxSize = 20000; var preThresholdMethod = "Percentile"; var preNumberOfDilates = 10; var preRemoveScale = true; // Simple segmentation options var simpleMinSize = 40; var simpleMaxSize = 6000; var simpleThresholdMethod = "Huang"; var simpleUseBinaryWatershed = true; var simpleFindEdges = true; var simpleClearBackground = true; var simpleRemoveScale = true // Watershed segmentation options var waterMinSize = 50; var waterMaxSize = 20000; var waterSigma = 4; var waterFindEdges = true; var waterClearBackground = true; var waterRemoveScale = true; // Large magnification segmentation options var largeMinSize = 10000; var largeMaxSize = "Infinity"; var largeRemoveScale = true; var largeNumberOfErodes = 3; // Internal variables to set and reset the colors var oldForeground; var oldBackground; // run a function here: exit(); // Main functions start //

- preprocessing clear background function preProcessImage() { roiManager("reset"); storeColors(); setWhiteOnBlack(); if (preRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); clearBackground(preMinSize, preMaxSize, preThresholdMethod, preNumberOfDilates, false); resetColors(); setBatchMode("exit and display"); } function clearBackground(minSize, maxSize, thresholdMethod, numberOfDilates, keepSelection) { run("Duplicate...", " "); saveSettings(); setOption("black background", false); run("Find Edges"); run("8-bit"); run("Smooth"); setAutoThreshold(thresholdMethod); run("Analyze Particles...", "size="+minSize+"-"+maxSize+" circularity=0.00-1.00 show=Masks exclude in_situ"); run("Create Selection"); run("Enlarge...", "enlarge=" + numberOfDilates + " pixel"); roiManager("Add"); close(); roiManager("select", 0); run("Clear Outside"); roiManager("reset"); if (!keepSelection) run("Select None"); restoreSettings(); } // - simple adipocytes segmentation function simpleSegmentation() { run("Duplicate...", " "); run("ROI Manager..."); roiManager("Show All with labels"); setBatchMode(true); roiManager("reset"); storeColors(); setWhiteOnBlack(); if (simpleRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); if (simpleUseBinaryWatershed && simpleClearBackground) clearBackground(preMinSize, preMaxSize, preThresholdMethod, preNumberOfDilates, true); if (simpleRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); if (simpleFindEdges) run("Find Edges"); run("8-bit"); if (simpleFindEdges) { run("Smooth"); run("Invert"); } setAutoThreshold(simpleThresholdMethod+ " dark"); run("Convert to Mask"); if (simpleUseBinaryWatershed) run("Watershed"); run("Clear Results"); run("Analyze Particles...", "size="+simpleMinSize+"-"+simpleMaxSize+" circularity=0.00-1.00 show=Nothing add exclude"); close(); if (simpleRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); resetColors(); setBatchMode("exit and display"); if (isOpen("Results")) { selectWindow("Results"); run("Close"); } roiManager("Show All with labels"); } // - watershed adipocytes segmentation function watershedAdipocytesSegmentation() { run("Duplicate...", " "); run("ROI Manager..."); roiManager("Show All with labels"); setBatchMode(true); roiManager("reset"); storeColors(); setWhiteOnBlack(); if (waterRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); if (waterClearBackground) clearBackground(preMinSize, preMaxSize, preThresholdMethod, preNumberOfDilates, false); title = getTitle(); if (waterRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); if (waterFindEdges) run("Find Edges"); else run("Invert"); run("8-bit"); run("Gaussian Blur...", "sigma=" + waterSigma); run("Watershed Algorithm"); run("Invert"); imageCalculator("AND create", title,"Watershed"); titleResult = getTitle(); selectImage("Watershed"); close(); selectImage(titleResult); setThreshold(1, 255); run("Convert to Mask"); run("Clear Results"); run("Analyze Particles...", "size="+waterMinSize+"-"+waterMaxSize+" circularity=0.00-1.00 show=Nothing add exclude"); selectImage(titleResult); close(); close(); if (waterRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); resetColors(); setBatchMode("exit and display"); if (isOpen("Results")) { selectWindow("Results"); run("Close"); } roiManager("Show All with labels"); } // - large magnification adipocytes segmentation function largeMagnificationSegmentation() { run("Duplicate...", " "); run("ROI Manager..."); roiManager("Show All with labels"); setBatchMode(true); roiManager("reset"); storeColors(); setWhiteOnBlack(); if (largeRemoveScale) run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel"); title = getTitle(); run("Fit Polynomial", "x=2 y=2 mixed=1"); run("8-bit"); setAutoThreshold("Huang dark"); setOption("BlackBackground", false); run("Convert to Mask"); for (i=0; i>16)&0xff, (oldForeground>>8)&0xff, oldForeground&0xff); setBackgroundColor((oldBackground>>16)&0xff, (oldBackground>>8)&0xff, oldBackground&0xff); }