Posted by
Krs5 on
May 09, 2013; 8:03am
URL: http://imagej.273.s1.nabble.com/Analyzing-a-grid-of-ROIs-tp5002945p5002958.html
Dear Audrey,
I had a play around with some code yesterday evening starting with the reference code you referred to and came up with:
macro analyse_grid{
connected = 0;
width = 4;
height = 4;
spacing = 0;
Dialog.create("ROI");
Dialog.addNumber("ROI width in pixels", width);
Dialog.addNumber("ROI height in pixels", height);
Dialog.addNumber("Spacing between ROIs", spacing);
Dialog.show();
width = Dialog.getNumber();
height = Dialog.getNumber();
spacing = Dialog.getNumber();
run("Select None");
setBatchMode(true);
title=getTitle();
H = getHeight();
W = getWidth();
run("Set Measurements...", " mean redirect=None decimal=3");
getRawStatistics(nPixels, mean, min, max, std, histogram);
numRow = floor((H+spacing)/(height+spacing));
numCol = floor((W+spacing)/(width+spacing));
x = ((W+spacing)-(numCol*(width+spacing)))/2;
y = ((H+spacing)-(numRow*(height+spacing)))/2;
for(i = 0; i < numRow; i++){
for(j = 0; j < numCol; j++){
xOffset = j * (width + spacing);
yOffset = i * (height + spacing);
makeRectangle(x + xOffset, y + yOffset, width, height);
roiManager("Add");
}
}
n = roiManager("count");
newImage("Mean", "RGB Black", W, H, 1);
for(i=0;i<n;i++){
selectWindow(title);
roiManager("select", i);
getRawStatistics(nPixels, meanROI, min, max, std, histogram);
meanROI=mean/meanROI;
selectWindow("Mean");
if(meanROI>1){
setForegroundColor(0, 0, 255);
roiManager("select", i);
roiManager("Fill");
}
if(meanROI<1){
setForegroundColor(255, 0, 0);
roiManager("select", i);
roiManager("Fill");
}
}
//find way to select only red and compare neighbours
selectWindow("Mean");
run("Duplicate...", "title=Mean-1");
run("Split Channels");
selectWindow("Mean-1 (green)");
close();
selectWindow("Mean-1 (blue)");
close();
selectWindow("Mean-1 (red)");
run("Select None");
run("Duplicate...", "title=connected");
run("RGB Color");
setForegroundColor(255, 0, 0);
for (i=2; i<=numRow-1;i++){
for(j=2; j<=numCol-1;j++){
selectWindow("Mean-1 (red)");
run("Select None");
c = (i-1)*numCol+j;
roiManager("select", c);
getRawStatistics(nPixels, meanROI, min, max, std, histogram);
if(meanROI == 255) {
roiManager("select", c-1);
getRawStatistics(nPixels, meanROIa, min, max, std, histogram);
if(meanROIa == 255){
roiManager("select", c+1);
getRawStatistics(nPixels, meanROIb, min, max, std, histogram);
if(meanROIb == 255){
roiManager("select", c-numCol);
getRawStatistics(nPixels, meanROIc, min, max, std, histogram);
if(meanROIc == 255){
roiManager("select", c+numCol);
getRawStatistics(nPixels, meanROId, min, max, std, histogram);
if(meanROId == 255){
connected = connected + 1;
selectWindow("connected");
roiManager("select", c);
roiManager("Fill");
}
}
}
}
}
//print(numCol, c);
}
}
selectWindow("Mean-1 (red)");
close();
setBatchMode("exit and display");
print("number of connected ="+connected);
}
The macro allows you to set the square size as well as the spacing. It will give you two images with one showing grid squares above (red) and below (blue) the mean of the image, and an image showing the grid squares above mean (white) with the "above threshold" square's nearest neighbour squares marked in red + a log file with the total number. Obviously, additional information can be extracted from the data. You have to open an image and run the macro but I guess speed can be increased if you let the macro open the file in Batch mode.
BE AWARE that I have not fully tested the code but have a look if it is useful.
Best wishes
Kees
Dr Ir K.R. Straatman
Senior Experimental Officer
Centre for Core Biotechnology Services
University of Leicester
http://www.le.ac.uk/biochem/microscopy/home.htmlImageJ workshops 29 and 30 July 2013 visit:
http://www.le.ac.uk/biochem/microscopy/ImageJ2013.html
-----Original Message-----
From: ImageJ Interest Group [mailto:
[hidden email]] On Behalf Of Audrey O'Neill
Sent: 07 May 2013 19:18
To:
[hidden email]
Subject: Analyzing a grid of ROIs
Hello -
Does anyone have experience with anaylzing a grid of square ROIs according to mean gray values?
Basically, I am trying to quantify how sorted or patchy a given field of cells (half of which are stained for GFP) is, using nearest-neighbor analysis.
The method is described in more detail in this paper (
http://www.ncbi.nlm.nih.gov/pubmed/9520108?dopt=Abstract), but basically what we want to do is:
1. divide the image into a grid of squares with height and width approximately equal to cell diameter 2. find the mean gray value of the whole image and set it as the threshold 3. count each square as above or below the threshold 4. count how many of each "above threshold" square's nearest neighbor squares (above, below, left, and right) are above threshold
I can do step (1) using a variant of the macro found here (
http://imagej.1557.x6.nabble.com/How-to-create-a-regular-grid-of-rectangular-ROI-s-td3685056.html), and step (2) is fairly trivial, but I am totally lost when it comes to steps (3) and (4).
Thanks very much!
Audrey
Postdoc, Bush lab
Cell and Tissue Biology
UCSF School of Dentistry
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html