Macro assistance for identifying object and best fit circle

Hi,

I am using this macro to detect an object in an image, fit circle to it, and measure centroid, area, perimeter. (F1-Classified image.tif)
Then I would like to use the extracted centroid coordinates from (F1-Classified image.tif) (X,Y) to measure radial profile in 360 degrees from another image , and also the area, perimeter. (F1.tif)

F1.tif (4.4 MB)
F1- Classified image.tif (4.5 MB) F1- Classified image-fit circle.tif (4.4 MB)

selectWindow(“F1-Classified image.tif”);
run(“8-bit”);
doWand(1196, 1160);
setBackgroundColor(0, 0, 0);
run(“Clear”, “slice”);
run(“Clear Outside”);
run(“Invert”);
run(“Fit Circle”);
run(“Set Measurements…”);
run(“Measure”);

How can I correct the doWand to be able to pick the largest circular object instead of the coordinates from the previous trial, and automate this process.

The idea behind this workflow is to be able to determine direction (angle) for dominant migration direction. The attached image does not show any preferential direction, and so the radial measurements should be more or less random.
thanks

Hi,

I would do :

selectImage("F1- Classified image.tif");
//thresholding of objects of interest
setThreshold(1, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "exclude clear add");
// looking for the biggest object
for (cpt=0, max=0, ind = 0; cpt<nResults(); cpt++)
{
	if (getResult("Area", cpt) > max)
	{
		max = getResult("Area", cpt);
		ind = cpt;
	}
}
//selection of this biggest object and removing of everything outside
roiManager("Select", ind);
setBackgroundColor(0, 0, 0);
run("Clear Outside");
run("Fit Circle");
run("Set Measurements...", "area redirect=None decimal=2");
run("Measure");
roiManager("Reset");

Nico

Hi Nico,

Thanks for the support it worked very well. I ran the macro and was able to add centroid and standard to the measurements as well.

I am new to programming altogether, so I would like to ask a couple of questions about the structure of this code and what its doing.
Now would it be possible to make the macro batch process the entire folder and save the results in a table? I want to be able to label differently the output image and results, such as:
input image: F1- Classified image.tif
output image: F1- Classified image- fit circle.tif
results table: F1- classified image- fit circle.csv , F2- classified image- fit circle.csv etc. (separate tables or in on single table?)

something like this maybe:
Whats is being done here? and what are the editable parts that can be customised.

/*

  • Macro to count nuclei in multiple images in a folder/subfolders.
    */

#@File(label = “Input directory”, style = “directory”) input
#@File(label = “Output directory”, style = “directory”) output
#@String(label = “File suffix”, value = “.tif”) suffix
#@int(label = “Minimum size”) minSize

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder("" + input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
//saves results for all images in a single file
saveAs(“Results”, output + “/All_Results.csv”);
}
classified images.zip (1.1 MB)

Thanks a mil

So this is the macro for batch processing that I have put together from other threads on this website.

function process_images_classified (imageFile)
{
// store the number of results before executing the commands
// so we can add the filename just to the new results
prevNumResults= nResults;

Open (imageFile)
// Get the filename from the title of the image that's open for adding to the results table
// We do this instead of using the imageFile parameter so that the
// directory path is not included on the table
filename = getTitle();
// Now loop through each of the new results, and add the filename to the "Filename" column
for (row = prevNumResults; row < nResults; row++)
{
    setResult("Filename", row, filename);
}

close("*");  // Closes all images

}
// The next step is to add code to ask the user of the macro to select a directory of images to process, and then to call our process_images_classified () function on each image in the directory, and finally to save the results. The code is shown below:

run(“Clear Results”); // clear the results table of any previous measurements

// The next line prevents ImageJ from showing the processing steps during
// processing of a large number of images, speeding up the macro
setBatchMode(true);

// Show the user a dialog to select a directory of images
inputDirectory = getDirectory(“Choose a Directory of Images”);

// Show the user a dialog to select a directory of images
inputDirectory = getDirectory(“Choose a Directory of Images”);

// Get the list of files from that directory
// NOTE: if there are non-image files in this directory, it may cause the macro to crash
fileList = getFileList(inputDirectory);

for (i = 0; i < fileList.length; i++)
{
process_images_classified(fileList[i]);
}
setBatchMode(false); // Now disable BatchMode since we are finished

updateResults(); // Update the results table so it shows the filenames

// Show a dialog to allow user to save the results file
outputFile = File.openDialog(“Save results file”);
// Save the results data
saveAs(“results”,outputFile);

//thresholding of objects of interest
setThreshold(1, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Analyze Particles…”, “exclude clear add”);
// looking for the biggest object
for (cpt=0, max=0, ind = 0; cpt<nResults(); cpt++)
{
if (getResult(“Area”, cpt) > max)
{
max = getResult(“Area”, cpt);
ind = cpt;
}
}
//selection of this biggest object and removing of everything outside
roiManager(“Select”, ind);
setBackgroundColor(0, 0, 0);
run(“Clear Outside”);
run(“Fit Circle”);
run(“Set Measurements…”, “area standard centroid redirect=None decimal=2”);
run(“Measure”);
roiManager(“Reset”);

My folder is saved on the Desktop and has the following name: “classified images”.
I ran this macro but i get an error: Error: Undefined identifier in line 7:
( imageFile )

// Macro to to select biggest object, and measure area, standard, centroid of directory of images

run(“Clear Results”); // clear the results table of any previous measurements

// The next line prevents ImageJ from showing the processing steps during
// processing of a large number of images, speeding up the macro
setBatchMode(true);

// Show the user a dialog to select a directory of images
inputDirectory = getDirectory(“Choose a Directory of Images”);

// Get the list of files from that directory
// NOTE: if there are non-image files in this directory, it may cause the macro to crash
fileList = getFileList(inputDirectory);

for (i = 0; i < fileList.length; i++)
{
processImage(fileList[i]);
}

setBatchMode(false); // Now disable BatchMode since we are finished
updateResults(); // Update the results table so it shows the filenames

// Show a dialog to allow user to save the results file
outputFile = File.openDialog(“Save results file”);
// Save the results data
saveAs(“results”,outputFile);

function processImage(imageFile)
{
// Store the number of results before executing the commands,
// so we can add the filename just to the new results
prevNumResults = nResults;

open(imageFile);
// Get the filename from the title of the image that's open for adding to the results table
// We do this instead of using the imageFile parameter so that the
// directory path is not included on the table
filename = getTitle();

//thresholding of objects of interest

setThreshold(1, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Analyze Particles…”, “exclude clear add”);
// looking for the biggest object
for (cpt=0, max=0, ind = 0; cpt<nResults(); cpt++)
{
if (getResult(“Area”, cpt) > max)
{
max = getResult(“Area”, cpt);
ind = cpt;
}
}
//selection of this biggest object and removing of everything outside
roiManager(“Select”, ind);
setBackgroundColor(0, 0, 0);
run(“Clear Outside”);
run(“Fit Circle”);
run(“Set Measurements…”, “area redirect=None decimal=2”);
run(“Measure”);
roiManager(“Reset”);

// Now loop through each of the new results, and add the filename to the "Filename" column
for (row = prevNumResults; row < nResults; row++)
{
    setResult("Filename", row, filename);
}

close("*");  // Closes all images

}

This code actually worked, but is was parsing through the images and over writing the previous results, and was left with one result at the end. I tried to edit it but now everything is failing:
fileList * array[34]
i 1
imageFile * “F11- Classified image.tif”
prevNumResults 1
filename * “F11- Classified image.tif”
cpt * 565
max * 307604.8753
ind * 101


Error: Index (101) is outside of the 0–1 range in line 56:
roiManager ( “Select” , ind <)> ;

any ideas on whats going on?