Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.

...

Looking at the image it seems that Ch1 is a good candidate to segment each cell.

Bloc de code
languagejava
titleImageJ Macro Segment and Measure
collapsetrue
// It might be easier to process all images ina folder
// You can specify this folder. You need to customize this path to your computer
InputDir="/Users/nicolas/Desktop/Input/";
 
// Or you can get a prompt to select the InputDir
// InputDir = getDirectory("Choose a Directory ");
 
 
// To save results in an output folder
// You can specify the OutputPath. You need to customize this path to your computer
//OutputPath="/Users/nicolas/Desktop/Output/";
 
//Or you can create a new folder based on the name of the input folder
//This is the method I prefer
ParentPath=File.getParent(InputDir);
InputDirName=File.getName(InputDir);
OutputDir=ParentPath+File.separator+InputDirName+"_Results";
i=1;
 
while(File.exists(OutputDir)){
OutputDir=ParentPath+File.separator+InputDirName+"_Results"+"-"+i;
i++;
}
File.makeDirectory(OutputDir);
OutputPath=OutputDir+File.separator;
File.makeDirectory(OutputPath+"Cropped Cells");
OutputCellPath=OutputPath+"Cropped Cells"+File.separator;

//// End of creating a new ouput folder

 
// Prepare some measurements settings and clean up the place
run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding fit shape feret's integrated median skewness kurtosis area_fraction stack display redirect=None decimal=3");
run("Clear Results");


//Then you can start to process all images
 
ListFile=getFileList(InputDir);
 run("ROI Manager...");
// It might be faster to work in batchmode
setBatchMode(true);


 
for (Filei=0; Filei<ListFile.length; Filei++){
FilePath=InputDir+ListFile[Filei];
open(FilePath);
ImageName=getTitle();
ImageNameNoExt=File.getNameWithoutExtension(FilePath);
getDimensions(width, height, channels, slices, frames);

//Remove the Background
selectWindow(ImageName);
ImageNameCorrected=ImageNameNoExt+"_Corrected";
run("Duplicate...", "title=&mageNameCorrected duplicate");
rename(ImageNameCorrected);
selectWindow(ImageNameCorrected);
run("Subtract...", "value=500");
run("Subtract Background...", "rolling=22 sliding stack");

//Adjust the display
for(ChI=1; ChI<channels+1; ChI++){
Stack.setChannel(ChI);
resetMinAndMax();
run("Enhance Contrast", "saturated=0.35");
//setMinAndMax(500, 3000);
}
Stack.setChannel(1);
Property.set("CompositeProjection", "Sum");
Stack.setDisplayMode("composite");

//Combine both color for full cell segmentation
run("RGB Color");
rename("RGB");
run("Duplicate...", "title=Mask duplicate");
run("16-bit");
resetThreshold();
setAutoThreshold("Otsu dark no-reset");
setOption("BlackBackground", true);
run("Convert to Mask");
run("Open");
run("Dilate");
run("Fill Holes");
run("Analyze Particles...", "size=2-12 circularity=0.60-1.00 exclude add");
selectWindow("Mask");
saveAs("TIFF", OutputPath+ImageNameNoExt+"_Cell-Mask.tif");
MaskImage=getTitle(); 
selectWindow(MaskImage);run("Close");
selectWindow("RGB");
run("Remove Overlay");
run("From ROI Manager");
saveAs("TIFF", OutputPath+ImageNameNoExt+"_Segmentation-Control.tif");
ControlImage=getTitle(); 
selectWindow(ControlImage);run("Close");

selectWindow(ImageNameCorrected);
count = roiManager("count");
for (i = 0; i < count; i++) {
roiManager("select", i);
run("Measure Stack...", "channels slices frames order=czt(default)");
}
RoiManager.select(0);

selectWindow("Results");
saveAs("Results", OutputPath+ImageNameNoExt+"_Measurements_Cells.csv");
run("Clear Results");

selectWindow(ImageNameCorrected);
saveAs("TIFF", OutputPath+ImageNameNoExt+"_Background-removed.tif");
ImageCorrected =getTitle();
selectWindow(ImageCorrected);

NbROIs=roiManager("size");
AllROIs=Array.getSequence(NbROIs);
roiManager("Select", AllROIs);
OutputCellName=OutputCellPath+ImageNameNoExt+"_";
RoiManager.multiCrop(OutputCellName, " save tif");
roiManager("Save", OutputPath+ImageNameNoExt+"_Cell-ROIs.zip");
roiManager("Deselect");
roiManager("Delete");
selectWindow(ImageCorrected);run("Close");
selectWindow(ImageName);run("Close");

}// end for FileI



This macro starts to be a bit long but to summarize here are the steps:

  • Open each image
  • Remove the offset from the camera (500 GV) and apply a rolling ball background subtraction
  • Create a RGB composite regrouping both channels
  • Convert this RGB to a 16-bit image
  • Threshold the RGB image using the Otsu alogrythm
  • Process the binary to improve detection (Open, Dilate, Fill Hole)
  • Analyze particles to detect cells with size=2-12 circularity=0.60-1.00
  • Add the results to the ROI manager
  • Save the Mask for the control of segmentation
  • Save an RGB image with the detection overlay as a control for the good detection
  • Save the ROIs
  • Use the ROIs to collect all measurements available and save the result as a csv
  • Save the image with the background removed
  • Crop each ROI from the image with the Background removed to isloate each cell

Few notes:

The camera offset is a value the camera adds to avoid having negative value due to noise. The best way to measure it is to take a dark image and have the mean intensity of the image. If you don't have that in hand you can choose a value sligly lower than the lowest pixel value found in your images.

The rolling ball background subtraction is powerfull tool to help with the segmentation

The values for the Analyze particles detection are the tricky part here. How I choose them? I use the thresholded image (binary) and I select the average guy: the spot that looks like all the others. I use the wand tool to create a selection and then I do Analyze>Measure This will give me a good estimate of the size (area) and the circularity. Then I select the tall/skinny and the short/not so skinny guys: I use again the wand tool to select the spots that would be my upper and lower limits. This will give me the range of spot size (area) and circularity. This is really a critical step that should be performed by hand prior running the script. You should do this on few different images to make sure the values are good enough to account for the variability you will encounter.

Now it is time to check that the job was done properly.

Looking at the control Images the detection isn't bad at all.

Image AddedImage Added


The only thing missing are the individual green foci seen below. Those cells look different as the FOci is very strong but there is not much fluorescence elsewhere (no diffuse green and no red). I might discuss with the scientist to see if it is OK to ignore them. If not I would need to change the threshold values and the detection parameters but let's say it is fine for now.

Image Added

So now you should have a list of files (images, ROIs, and CSV files). We will focus on the CSV files has they contain the number of cells we are looking for and a lot more information we can also use.