Please,
I would really like some help to write a macro! I am looking for areas in an image (synapses to be precise) where two markers (a pre-synaptic and a post-synaptic one) are close to each other. They will not always overlap. I have taken the images with four channels, but can export them from the imaging program with just a subset of the channels if that makes it any easier. The way I have tried to do it is this: 1. open the image 2. split the channels 3. close the un-needed channel (white) 4. threshold the remaining channels (red, green, blue) 5. create a selection of the blue pixels above threshold and measure the area (I will normalize the number of synapses to this number). 6. remove noise by removing outliers in the red and green channels 7. use the function "Dilate' for the green channel 8. in analyze, set measurements, select redirect to red channel (the name of this will change for the different images, is there a way to name them so that it is automated?) 8. use the function "analyze puncta" after selecting the green channel From the data in the results table, I can then figure out how many of the outlined puncta (dilated) in green contain red pixels. I need to take 3x10 (repeat experiments) images for each of 3 conditions = 90 images, so I would really love to find a way to do this a bit more automated. When I keep the "Macro recorder" open while going through the procedure, I obtain the code below, and it doesn't work. I know nothing about programming, so I am having a really hard time troubleshooting this. Could someone please advice? Thanks in advance! ------------------------------------------------ run("Split Channels"); selectWindow("C1-101.lsm"); close(); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C3-101.lsm"); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C2-101.lsm"); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C4-101.lsm"); run("Create Selection"); run("Measure"); run("Measure"); IJ.deleteRows(88, 100); IJ.deleteRows(75, 87); IJ.deleteRows(61, 74); IJ.deleteRows(48, 60); IJ.deleteRows(34, 47); IJ.deleteRows(20, 33); IJ.deleteRows(6, 19); IJ.deleteRows(0, 5); close(); selectWindow("C2-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); setThreshold(38, 255); run("Convert to Mask"); selectWindow("C3-101.lsm"); setThreshold(32, 255); run("Convert to Mask"); selectWindow("C2-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-101.lsm"); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-101.lsm decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); |
From your code and description I made the following code:
title = getTitle(); run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setThreshold(32, 255); run("Convert to Mask"); selectWindow("C2-"+title); setThreshold(38, 255); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); This might need some further changes. If so, please contact me off list. Kees Dr Ir K.R. Straatman Senior Experimental Officer Centre for Core Biotechnology Services College of Medicine, Biological Sciences and Psychology University of Leicester http://www.le.ac.uk/biochem/microscopy/home.html -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of SUBSCRIBE IMAGEJ Anonymous Sent: 30 March 2012 22:41 To: [hidden email] Subject: proxi-localization macro Please, I would really like some help to write a macro! I am looking for areas in an image (synapses to be precise) where two markers (a pre-synaptic and a post-synaptic one) are close to each other. They will not always overlap. I have taken the images with four channels, but can export them from the imaging program with just a subset of the channels if that makes it any easier. The way I have tried to do it is this: 1. open the image 2. split the channels 3. close the un-needed channel (white) 4. threshold the remaining channels (red, green, blue) 5. create a selection of the blue pixels above threshold and measure the area (I will normalize the number of synapses to this number). 6. remove noise by removing outliers in the red and green channels 7. use the function "Dilate' for the green channel 8. in analyze, set measurements, select redirect to red channel (the name of this will change for the different images, is there a way to name them so that it is automated?) 8. use the function "analyze puncta" after selecting the green channel From the data in the results table, I can then figure out how many of the outlined puncta (dilated) in green contain red pixels. I need to take 3x10 (repeat experiments) images for each of 3 conditions = 90 images, so I would really love to find a way to do this a bit more automated. When I keep the "Macro recorder" open while going through the procedure, I obtain the code below, and it doesn't work. I know nothing about programming, so I am having a really hard time troubleshooting this. Could someone please advice? Thanks in advance! ------------------------------------------------ run("Split Channels"); selectWindow("C1-101.lsm"); close(); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C3-101.lsm"); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C2-101.lsm"); //run("Threshold..."); setAutoThreshold("Default dark"); selectWindow("C4-101.lsm"); run("Create Selection"); run("Measure"); run("Measure"); IJ.deleteRows(88, 100); IJ.deleteRows(75, 87); IJ.deleteRows(61, 74); IJ.deleteRows(48, 60); IJ.deleteRows(34, 47); IJ.deleteRows(20, 33); IJ.deleteRows(6, 19); IJ.deleteRows(0, 5); close(); selectWindow("C2-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); setThreshold(38, 255); run("Convert to Mask"); selectWindow("C3-101.lsm"); setThreshold(32, 255); run("Convert to Mask"); selectWindow("C2-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-101.lsm"); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-101.lsm"); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-101.lsm decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); |
In reply to this post by ktoo
Thanks so much for providing this elegant solution!
It worked very well until I tried to build on this code (see below) to be able to batch-analyze this data... The macro below does run through all the images in a folder, and it generates all the measurements the macro specifies and puts them in the "Summary" and "Results" windows. The problem is that all the data for one image, except the area, are set to zero when the next image is analyzed. This happens for "Results", but not "Summary". I have tried a bunch of different things, but the results still get erased between images. Another problem with the macro is that the Results and Summary tables are not automatically saved the way I have written this, but I can live with that, as long as I get a chance to save them before they get cleared. Could anyone please advice what the problem is? Thanks in advance! *********************************** dir = getDirectory("Choose a Directory "); list = getFileList(dir); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); close; } selectWindow("Summary"); saveAs("Text", getDirectory("home")+"Summary.txt"); selectWindow("Results"); saveAs("Text", getDirectory("home")+"Results.txt"); |
I made some changes that will allow you to save the results table for each image, the summary table and the ROIs for each image in a directory of your choice. See if this does what you want. Using the saved ROI manager data you can check if the selection are as you expect.
dir = getDirectory("Choose the directory with images"); list = getFileList(dir); dir2 = getDirectory("Choose the directory to save the results"); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); title2 = File.nameWithoutExtension; run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=Nothing display summarize add"); selectWindow("Results"); saveAs("txt", dir2+title2); run("Close"); roiManager("Save", dir2+title2+".zip"); roiManager("reset"); run("Close All"); } selectWindow("Summary"); saveAs("txt", dir2+"Summary.txt"); run("Close"); Best wishes Kees -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of SUBSCRIBE IMAGEJ Anonymous Sent: 04 April 2012 00:34 To: [hidden email] Subject: Re: proxi-localization macro Thanks so much for providing this elegant solution! It worked very well until I tried to build on this code (see below) to be able to batch-analyze this data... The macro below does run through all the images in a folder, and it generates all the measurements the macro specifies and puts them in the "Summary" and "Results" windows. The problem is that all the data for one image, except the area, are set to zero when the next image is analyzed. This happens for "Results", but not "Summary". I have tried a bunch of different things, but the results still get erased between images. Another problem with the macro is that the Results and Summary tables are not automatically saved the way I have written this, but I can live with that, as long as I get a chance to save them before they get cleared. Could anyone please advice what the problem is? Thanks in advance! *********************************** dir = getDirectory("Choose a Directory "); list = getFileList(dir); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); close; } selectWindow("Summary"); saveAs("Text", getDirectory("home")+"Summary.txt"); selectWindow("Results"); saveAs("Text", getDirectory("home")+"Results.txt"); |
Forgot. If you want to save all results in a single results file you can take the lines:
selectWindow("Results"); saveAs("txt", dir2+title2); run("Close"); out of the code and add at the end selectWindow("Results"); saveAs("txt", dir2+"results.txt"); run("Close"); As far as I can see that should work. Kees -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Straatman, Kees R. (Dr.) Sent: 04 April 2012 09:37 To: [hidden email] Subject: Re: proxi-localization macro I made some changes that will allow you to save the results table for each image, the summary table and the ROIs for each image in a directory of your choice. See if this does what you want. Using the saved ROI manager data you can check if the selection are as you expect. dir = getDirectory("Choose the directory with images"); list = getFileList(dir); dir2 = getDirectory("Choose the directory to save the results"); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); title2 = File.nameWithoutExtension; run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=Nothing display summarize add"); selectWindow("Results"); saveAs("txt", dir2+title2); run("Close"); roiManager("Save", dir2+title2+".zip"); roiManager("reset"); run("Close All"); } selectWindow("Summary"); saveAs("txt", dir2+"Summary.txt"); run("Close"); Best wishes Kees -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of SUBSCRIBE IMAGEJ Anonymous Sent: 04 April 2012 00:34 To: [hidden email] Subject: Re: proxi-localization macro Thanks so much for providing this elegant solution! It worked very well until I tried to build on this code (see below) to be able to batch-analyze this data... The macro below does run through all the images in a folder, and it generates all the measurements the macro specifies and puts them in the "Summary" and "Results" windows. The problem is that all the data for one image, except the area, are set to zero when the next image is analyzed. This happens for "Results", but not "Summary". I have tried a bunch of different things, but the results still get erased between images. Another problem with the macro is that the Results and Summary tables are not automatically saved the way I have written this, but I can live with that, as long as I get a chance to save them before they get cleared. Could anyone please advice what the problem is? Thanks in advance! *********************************** dir = getDirectory("Choose a Directory "); list = getFileList(dir); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); close; } selectWindow("Summary"); saveAs("Text", getDirectory("home")+"Summary.txt"); selectWindow("Results"); saveAs("Text", getDirectory("home")+"Results.txt"); |
In reply to this post by ktoo
Thanks for all the help on this!
I found the bug that cleared all fields except the area in the "results" table. The code I decided to use is below. dir = getDirectory("Choose a Directory "); list = getFileList(dir); setBatchMode(true); for (i=0; i<list.length; i++) { showProgress(i, list.length); open(dir+list[i]); title = getTitle(); run("Split Channels"); selectWindow("C1-"+title); close(); selectWindow("C3-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C2-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); selectWindow("C4-"+title); setAutoThreshold("Default dark"); run("Convert to Mask"); run("Create Selection"); run("Set Measurements...", "area mean min bounding display redirect=None decimal=3"); run("Measure"); selectWindow("C2-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Remove Outliers...", "radius=2 threshold=50 which=Bright"); selectWindow("C3-"+title); run("Dilate"); run("Set Measurements...", "area mean min bounding display add redirect=C2-"+title+" decimal=3"); run("Analyze Particles...", "size=4-500 pixel circularity=0.50-1.00 show=[Overlay Outlines] display summarize add in_situ"); close; } |
Free forum by Nabble | Edit this page |