Hello,
I am trying to write a macro to process a series of rat brain MRI DTI files (e.g. fractional anisotropy maps, and others) using manually drawn ROIs of various white matter structures. Each image has its own ROIset saved and named identically to the image file, except for the extension. This is the sequence of events that I want to occur: 1. Open image file from specified image directory 2. Load matching image ROIset from specified ROI directory into ROI manager. 3. Measure mean, min, max, etc. for all ROI objects and save the results into a file in a specified results directory. 4. Empty results table. 5. Delete ROIs from ROI manager. 6. Close any error windows that may be generated (for example if ROIsets contain text labels). 7. Return to step 1 and repeat cycle with next image file. I am not that well-versed in the ImageJ macro language but I have been able to cobble together macros that work to either (1) load and measure of folder of image files using one preloaded ROIset, or (2) load and measure a folder of ROIsets on a single preloaded image. I am stuck and trying to combine them so that each image is measured with its corresponding ROIset. I think the problem is that I am using "for" loops to cycle through both the image directory and ROIset directory. If I nest one inside the other, the ROIset loop loads the next ROIset but the next image hasn't been loaded. I don't know how to exit the loop to get back to loading the next image file in the series. Below is my macro so far. For now, I have batch mode=false to try to troubleshoot, but I would anticipate switching back to batchmode=true. If anyone has a solution, I would be most grateful. Thank you, Greg Kujoth macro "Batch Measure DTI [F6]" { run("Set Measurements...", "area mean min integrated limit display redirect=None decimal=3"); dir1 = getDirectory("Choose an Image Directory "); dir2 = getDirectory("Choose an ROI file Directory"); dir3 = getDirectory("Choose an Results Directory"); setBatchMode(false); list = getFileList(dir1); for (i=0; i<list.length; i++) { path = dir1+list[i]; showProgress(i, list.length); if (!endsWith(path,"/")) open(path); if (nImages>=1) { //loading ROIs here list2 = getFileList(dir2); for (i=0; i<list2.length; i++) { path2 = dir2+list2[i]; if (!endsWith(path2,"/")) roiManager("Open", path2); roiManager("Deselect"); roiManager("Measure"); path3 = dir3+list2[i]; saveAs("Results", path3+"Results.xls"); run("Clear Results"); roiManager("Delete"); close(); //closes main image window if (isOpen("Exception")) { selectWindow("Exception"); run("Close"); //close error windows to avoid saving to results. } }//PROBLEM: loop goes back to loading next ROI but next image file isn't open yet } } if (isOpen("Results")) { selectWindow("Results"); run("Close"); } } |
Hi,
you wrote 2 loops with the same counter "i", that's usually doesn't work, you have to choose another variable for next loops like "ii" or "j". Anyway, your macro should work with only one loop, if I understood well, you have one roi set/image, so: macro "Batch Measure DTI [F6]" { run("Set Measurements...", "area mean min integrated limit display redirect=None decimal=3"); dir1 = getDirectory("Choose an Image Directory "); dir2 = getDirectory("Choose an ROI file Directory"); dir3 = getDirectory("Choose an Results Directory"); setBatchMode(false); list = getFileList(dir1); list2 = getFileList(dir2); for (i=0; i<list.length; i++) { path = dir1+list[i]; showProgress(i, list.length); if (!endsWith(path,"/")) open(path); if (nImages>=1) { //loading ROIs here path2 = dir2+list2[i]; if (!endsWith(path2,"/")) roiManager("Open", path2); roiManager("Deselect"); roiManager("Measure"); path3 = dir3+list2[i]; saveAs("Results", path3+"Results.xls"); run("Clear Results"); roiManager("Delete"); close(); //closes main image window if (isOpen("Exception")) { selectWindow("Exception"); run("Close"); //close error windows to avoid saving to results. } } if (isOpen("Results")) { selectWindow("Results"); run("Close"); } } } 2013/5/30 gckujoth <[hidden email]> > Hello, > > I am trying to write a macro to process a series of rat brain MRI DTI files > (e.g. fractional anisotropy maps, and others) using manually drawn ROIs of > various white matter structures. Each image has its own ROIset saved and > named identically to the image file, except for the extension. > > This is the sequence of events that I want to occur: > > 1. Open image file from specified image directory > 2. Load matching image ROIset from specified ROI directory into ROI > manager. > 3. Measure mean, min, max, etc. for all ROI objects and save the results > into a file in a specified results directory. > 4. Empty results table. > 5. Delete ROIs from ROI manager. > 6. Close any error windows that may be generated (for example if ROIsets > contain text labels). > 7. Return to step 1 and repeat cycle with next image file. > > I am not that well-versed in the ImageJ macro language but I have been able > to cobble together macros that work to either (1) load and measure of > folder > of image files using one preloaded ROIset, or (2) load and measure a folder > of ROIsets on a single preloaded image. I am stuck and trying to combine > them so that each image is measured with its corresponding ROIset. > > I think the problem is that I am using "for" loops to cycle through both > the > image directory and ROIset directory. If I nest one inside the other, the > ROIset loop loads the next ROIset but the next image hasn't been loaded. > > I don't know how to exit the loop to get back to loading the next image > file > in the series. > > Below is my macro so far. For now, I have batch mode=false to try to > troubleshoot, but I would anticipate switching back to batchmode=true. > > If anyone has a solution, I would be most grateful. > > Thank you, > > Greg Kujoth > > > > > macro "Batch Measure DTI [F6]" { > > run("Set Measurements...", "area mean min integrated limit display > redirect=None decimal=3"); > dir1 = getDirectory("Choose an Image Directory "); > dir2 = getDirectory("Choose an ROI file Directory"); > dir3 = getDirectory("Choose an Results Directory"); > setBatchMode(false); > > list = getFileList(dir1); > for (i=0; i<list.length; i++) { > path = dir1+list[i]; > showProgress(i, list.length); > if (!endsWith(path,"/")) open(path); > if (nImages>=1) { > > //loading ROIs here > list2 = getFileList(dir2); > for (i=0; i<list2.length; i++) { > path2 = dir2+list2[i]; > if (!endsWith(path2,"/")) > roiManager("Open", path2); > roiManager("Deselect"); > roiManager("Measure"); > path3 = dir3+list2[i]; > saveAs("Results", path3+"Results.xls"); > run("Clear Results"); > roiManager("Delete"); > close(); > //closes main image window > if (isOpen("Exception")) { > selectWindow("Exception"); > run("Close"); //close > error windows to avoid saving to results. > } > }//PROBLEM: loop goes back to loading next > ROI but next image file > isn't open yet > } > } > if (isOpen("Results")) { > selectWindow("Results"); > run("Close"); > } > } > > > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Batch-measuring-images-with-image-specific-ROIs-tp5003162.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Jean-Philippe,
That does seem to work. Thank you! I appreciate your help! Greg <quote author="Jean-Philippe Grossier"> Anyway, your macro should work with only one loop, if I understood well, you have one roi set/image, so: macro "Batch Measure DTI [F6]" { run("Set Measurements...", "area mean min integrated limit display redirect=None decimal=3"); dir1 = getDirectory("Choose an Image Directory "); dir2 = getDirectory("Choose an ROI file Directory"); dir3 = getDirectory("Choose an Results Directory"); setBatchMode(false); list = getFileList(dir1); list2 = getFileList(dir2); for (i=0; i<list.length; i++) { path = dir1+list[i]; showProgress(i, list.length); if (!endsWith(path,"/")) open(path); if (nImages>=1) { //loading ROIs here path2 = dir2+list2[i]; if (!endsWith(path2,"/")) roiManager("Open", path2); roiManager("Deselect"); roiManager("Measure"); path3 = dir3+list2[i]; saveAs("Results", path3+"Results.xls"); run("Clear Results"); roiManager("Delete"); close(); //closes main image window if (isOpen("Exception")) { selectWindow("Exception"); run("Close"); //close error windows to avoid saving to results. } } if (isOpen("Results")) { selectWindow("Results"); run("Close"); } } } |
Free forum by Nabble | Edit this page |