How to measure the area of the cells within each detected colonies

I have the following image:

And with this code:


infile = "/Users/pdubois/Desktop/my_image.jpg";
open(infile);
fileparts = split(infile,"/");
nofpart = fileparts.length;
bases = split(fileparts[nofpart-1],".");
base = bases[0];
outpath = File.getParent(infile) + "/fiji/";
File.makeDirectory(outpath); 
run("Set Scale...", "distance=189.0238 known=2006 pixel=1 unit=µm global");
setForegroundColor(255, 255, 255);
setBackgroundColor(0, 0, 0);
run("Duplicate...", "title=ImageCOPY");
makeOval(156, 54, 3342, 3342);
run("Clear Outside");
run("Select None");
run("Duplicate...", "title=Clusters");

// Color Thresholder 2.0.0-rc-59/1.51j
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB Stack");
run("Convert Stack to Images");
selectWindow("Hue");
rename("0");
selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=125;
max[0]=255;
filter[0]="pass";
min[1]=7;
max[1]=255;
filter[1]="pass";
min[2]=65;
max[2]=255;
filter[2]="pass";
for (i=0;i<3;i++){
  selectWindow(""+i);
  setThreshold(min[i], max[i]);
  run("Convert to Mask");
  if (filter[i]=="stop")  run("Invert");
}
imageCalculator("AND create", "0","1");
imageCalculator("AND create", "Result of 0","2");
for (i=0;i<3;i++){
  selectWindow(""+i);
  close();
}
selectWindow("Result of 0");
close();
selectWindow("Result of Result of 0");
rename(a);

// Colour Thresholding-------------
setOption("BlackBackground", true);
run("Make Binary");
run("Options...", "iterations=6 count=3 black do=Dilate");
run("Options...", "iterations=20 count=1 black do=Close");
run("Remove Outliers...", "radius=40 threshold=50 which=Bright");
setThreshold(1, 255);
run("Analyze Particles...", "size=1000-Infinity show=Nothing exclude clear include summarize add");

//SELECT ORIGINAL OR BLACK-BACKGROUNDED IMAGE HERE
selectWindow("ImageCOPY");
run("Add Slice");
run("Select All");
run("Fill", "slice");
run("Select None");

// This is where we count and measure cells in each colony
// by looping through ROI manager
//for (z=0;z<roiManager("count"); z++){

// for showing we just look at first two colonies (cluster)
for (z=0;z<2; z++){
  setSlice(1);
  run("Duplicate...", "title="+z);
  roiManager("Select", z);
  run("Clear Outside");
  run("Select None");
  // Color Thresholder 2.0.0-rc-59/1.51j
  // Autogenerated macro, single images only!
  min=newArray(3);
  max=newArray(3);
  filter=newArray(3);
  a=getTitle();
  run("HSB Stack");
  run("Convert Stack to Images");
  selectWindow("Hue");
  rename("0");
  selectWindow("Saturation");
  rename("1");
  selectWindow("Brightness");
  rename("2");
  min[0]=125;
  max[0]=255;
  filter[0]="pass";
  min[1]=7;
  max[1]=255;
  filter[1]="pass";
  min[2]=65;
  max[2]=255;
  filter[2]="pass";
  for (i=0;i<3;i++){
    selectWindow(""+i);
    setThreshold(min[i], max[i]);
    run("Convert to Mask");
    if (filter[i]=="stop")  run("Invert");
  }
  imageCalculator("AND create", "0","1");
  imageCalculator("AND create", "Result of 0","2");
  for (i=0;i<3;i++){
    selectWindow(""+i);
    close();
  }
  selectWindow("Result of 0");
  close();
  selectWindow("Result of Result of 0");
  rename(a);
  // Colour Thresholding-------------
  run("Make Binary");
  run("Watershed");
  run("Make Binary");
  run("Watershed");
  setThreshold(1, 255);
  run("Analyze Particles...", "size=0-Infinity show=Masks display exclude include summarize");


  // Save the cells within each colony and the area for each cell
  if (isOpen("Results")) {
   selectWindow("Results");
   saveAs("Measurements", outpath + base + "roi." + z + ".results.tsv");
   run("Close");
  }
  selectWindow("Mask of "+z);
  run("Copy");
  close();
  selectWindow(""+z);
  close();
  //ORIGINAL IMAGE REFERENCE HERE
  selectWindow("ImageCOPY");
  setSlice(2);
  setPasteMode("Transparent-white");
  run("Paste");
}
run("Select None");
rename("Composite Image");

selectWindow("Summary");
saveAs("Measurements", outpath + base + ".Summary.tsv");
run("Close");

////// Saving Clusters
selectWindow("Clusters");
run("Capture Image");
saveAs("Jpeg", outpath + base +  ".Clusters.jpeg");


selectWindow("Composite Image");
saveAs("Jpeg", outpath + base +  ".Composite_Image.jpeg");
run("Close");

selectWindow("Clusters");
run("Close");
selectWindow("ROI Manager");
run("Close");
//selectWindow("Results");
//run("Close");
selectWindow("Clusters-1");
run("Close");
selectWindow(base +".jpg");
run("Close");

I can detect the colonies (cluster) as shown here

And the corresponding cell detected is here:


My code can count and measure the area for 2nd colonies onward but
somehow the first colony it seems to show the wrong answer (see figure above).

This is the “Summary” table:

This is the wrong “Result” table for 1st cluster (index = 0);

This is the correct “Result” table for 2nd cluster (index = 1);

How can I resolve the issue?

I don’t have time to go through the code, but it looks like your first Excel table is shifted due to the cell that says “Clusters.”
So you have a count of 2, followed by a count of 1043 in lines 3 and 4. The count of 39 is for something prior to the two areas you are indicating in your image.

Specifically. The 2 cells are at index 1, and the 1043 cells are at index 2 (as shown on your first excel sheet).
This matches the 2 I see in the red masked image in the same shape as the 1043.

The perimeter is much larger for cluster 0, so it is probably worth trying to determine where that came from.

2 Likes

Ok @Peverall_Dubois

How are you doing your processing once you have the larger regions delineated? You show that you have working code up to that point… but are you using another, follow-up script to do the cell counting / area measurements that is then not working correctly? If so… add some print() statements to double-check the indexes when moving through the script.

1 Like

Oh wait, duh, still processing my caffeine.
image
You have 39 ROIs. The first row is the count of the ROIs, and the total perimeter and total area.
So everything is working correctly, it is just a matter of how to interpret it.

2 Likes

Thanks for your reply. But the problem is that it doesn’t produce “Result” table that account for cluster index 0 (or number 1 in red segmented figs) that contains only 2 cells.

When you step through your code one line at a time, does the results table look like you would expect it to before you save it?

1 Like

Not quite. As I go step by step, somehow the code skip cluster index 0 (or number 1 in red segmented figs) that contains only 2 cells.

So clusters 2, 3, 4 etc are all correct as well then?

1 Like

That’s right. They’re all correct.

It seems that “Result” table with index 0 (or number 1 in red segmented figs) combine 39 ROI colonies and two index 0 cells (39 + 2 = 41).
But I don’t know how to separate that.

Can you try clearing the results table before starting the first cycle of the code? I suspect the original results are for that first combined line. So prior to accessing the ROI with 2 spots, you need to clear out the old stuff.
If I do this with Record on I get:
run("Clear Results");

1 Like

How can I do that?

I tried to put run("Close"); after this line:

setThreshold(1, 255);
run("Analyze Particles...", "size=1000-Infinity show=Nothing exclude clear include summarize add");

and before this line:


//SELECT ORIGINAL OR BLACK-BACKGROUNDED IMAGE HERE
selectWindow("ImageCOPY");

But doesn’t work.

I suspect close would just close the window, not clear the results like the above line of code.

What’s the command for clearing the result table?

That one, 20 characters.

Yes. It works! Thanks a million.

1 Like