Posted by
Audrey O'Neill on
May 15, 2013; 12:20am
URL: http://imagej.273.s1.nabble.com/Analyzing-a-grid-of-ROIs-tp5002945p5003009.html
Thanks for working on this! Your output is a really nice visual representation (though I think it has some problems near the edges).
I was working on making up a macro myself, and I came up with something that is similar to yours but has slightly different outputs. I wanted to get the number of stained (white) neighbors of each stained square, not the number of stained squares that have 4 stained neighbors.
Also, I decided that I will not need spacing between the squares and to convert all the images to 8-bit, so that simplifies matters.
This is a macro where I set the threshold manually, but I have another one where I set it based on the median of the means of the ROIs.
Thanks for your help!
Audrey
/* convert to 8-bit */
run("8-bit");
/* set desired variables */
imageName = getInfo("image.filename");
x = 0;
y = 0;
cellDiam = 80; // set according to average cell diameter in pixels in your image
threshold = 20; // set threshold according to dimmest positive cell
imageWidth = getWidth();
imageHeight = getHeight();
numRow = floor(imageHeight / cellDiam);
numCol = floor(imageWidth / cellDiam);
countSquares = numRow * numCol;
countStainedSquares = 0;
countStainedNeighbors = 0;
/* crop image to a multiple of the cell diameter */
makeRectangle(x, y, (numCol * cellDiam), (numRow * cellDiam));
roiManager("Add");
run("Crop");
/* first for loop: convert each square to black or white depending on whether its mean is above the threshold */
for(i = 0; i < numRow; i++)
{
for(j = 0; j < numCol; j++)
{
xOffset = j * cellDiam;
yOffset = i * cellDiam;
makeRectangle(x + xOffset, y + yOffset, cellDiam, cellDiam);
roiManager("Add");
getRawStatistics(dummy, mean, dummy, dummy, dummy, dummy2);
if (mean >= threshold) { // set cells above threshold to white and count them
setColor(255);
fillRect(x + xOffset, y + yOffset, cellDiam, cellDiam);
countStainedSquares++;
} else { // end of if statement to set color to white and test nearest neighbors
setColor(0);
fillRect(x + xOffset, y + yOffset, cellDiam, cellDiam);
} // end of else statement to set color to black
} //end of j for loop
} // end of i for loop
/* end of first for loop */
/* second for loop: test and count neighbors of stained squares */
for(i = 0; i < numRow; i++)
{
for(j = 0; j < numCol; j++)
{
xOffset = j * cellDiam;
yOffset = i * cellDiam;
testSquare=getPixel((x + xOffset + (0.5 * cellDiam)), (y + yOffset + (0.5 * cellDiam)));
if (testSquare == 255) { // for stained squares, score neighbors
/* test square to the right and count if stained */
if (j < numCol) {
testRight = getPixel((x + xOffset + (1.5 * cellDiam)),(y + yOffset + (0.5 * cellDiam)));
if (testRight == 255) {
countStainedNeighbors++;
}
}
/* test square to the left and count if stained */
if (j > 0) {
testLeft = getPixel((x + xOffset - (0.5 * cellDiam)),(y + yOffset + (0.5 * cellDiam)));
if (testLeft == 255) {
countStainedNeighbors++;
}
}
/* test square above and count if stained */
if (i > 0) {
testAbove = getPixel((x + xOffset + (0.5 * cellDiam)),(y + yOffset - (0.5 * cellDiam)));
if (testAbove == 255) {
countStainedNeighbors++;
}
}
/* test square below and count if stained */
if (i < numRow) {
testBelow = getPixel((x + xOffset + (0.5 * cellDiam)),(y + yOffset + (1.5 * cellDiam)));
if (testBelow == 255) {
countStainedNeighbors++;
}
}
} // end of if statement to test nearest neighbors
} //end of j for loop
} // end of i for loop
/* end of second for loop */
/* calculate and display sorting scores */
stainedSortingScore = countStainedNeighbors / (countStainedSquares * 4);
randomSortingScore = (0.95 * (countStainedSquares / countSquares) - 0.0213)
print(imageName + ", " + threshold + ", " + countSquares + ", " + countStainedSquares + ", " + (countStainedSquares * 100 / countSquares) + ", " + stainedSortingScore + ", " + randomSortingScore);
rename(imageName + "binary");
run("Tiff...");
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html