You are on page 1of 3

//BEFORE STARTING

//Open the branch image for analysis "branch image.jpg"


//Now go to ImageJ main menu > Plugins > Macros > Run ...
//The macro file "branch macro.txt" should be in the same folder as the image
//Run the macro
rename("branch image.jpg");
//PART 1 - FINDING YOUR PARTICLE (same as before)
run("Select None");
run("Make Binary");
run("Set Measurements...", "area bounding redirect=None decimal=3");
//you want to find the area of each particle, as well as
//its bounding rectangle (smallest rectangle that completely encloses the partic
le)
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing
display clear in_situ");
//you get a Results Table with columns:
//Area, BX, BY, Width, Height
//Area is the Area of each particle found
//BX and BY are the coordinates of the top-left corner of bounding rectangle
//Width and Height are the dimensions of the bounding rectangle
largest_Area=0;
for(i=0;i<nResults;i++) {
this_Area = getResult("Area",i);
if(this_Area > largest_Area) {
largest_Area = this_Area;
largest_index=i;
}
}
my_particle = largest_index;
run("Select None");
run("Invert");
for(i=0;i<nResults;i++) {
if(i!=my_particle) {
BX = getResult("BX", i);
BY = getResult("BY", i);
Width = getResult("Width", i);
Height = getResult("Height", i);
makeRectangle(BX, BY, Width, Height);
run("Clear");
}
}
run("Select None");
run("Invert");

//find the particle with the largest Area, which will be the one you want
//remove all the other particles
//from now on, BX, BY, Width, Height refer to the values in YOUR PARTICLE'S ROW
only
BX = getResult("BX", my_particle);
BY = getResult("BY", my_particle);
Width = getResult("Width", my_particle);
Height = getResult("Height", my_particle);
run("Fill Holes");
makeRectangle(BX, BY, Width, Height);
waitForUser("Your particle is selected on-screen");
wait(100);
run("Select None");

//PART 2 - LENGTH MEASUREMENT


run("Skeletonize");
waitForUser("Skeletonize operation shrinks the branch down to a connected line g
raph");
wait(100);
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=Nothing
display clear in_situ");
total_Length = getResult("Area",0);
waitForUser("The area of the skeletonized branch is the total length of the bran
ch (" + total_Length + " pixels)");
wait(100);

//PART 3 - NODE FINDING


//using a moving window of 3x3 pixels (i+/-1, j+/-1)
//only check pixels on the branch (i, j)
//node may exist if branch pixel has >2 nearest neighbors
node_xpoints = newArray();
node_ypoints = newArray();
for (i=BX+1;i<BX+Width-1;i++) {
for (j=BY+1;j<BY+Height-1;j++) {
NNcounter = 0;
if (getPixel(i,j)
NNcounter
NNcounter
NNcounter
NNcounter
NNcounter
NNcounter
NNcounter
NNcounter

==
+=
+=
+=
+=
+=
+=
+=
+=

255) {
getPixel(i-1,j-1);
getPixel(i,j-1);
getPixel(i+1,j-1);
getPixel(i-1,j);
getPixel(i+1,j);
getPixel(i-1,j+1);
getPixel(i,j+1);
getPixel(i+1,j+1);

NNcounter /= 255;
if (NNcounter > 2) {
node_xpoints = Array.concat(node_xpoints, i);
node_ypoints = Array.concat(node_ypoints, j);
}
}
}
}
run("Duplicate...", "title=[branch image-1.jpg]");
run("Select All");
run("Clear");
makeSelection("point", node_xpoints,node_ypoints)
setForegroundColor(255, 255, 255);
run("Draw");
waitForUser("Make a blank image of the same size as the original image a
nd draw the node points (zoom in if you can't see them)");
run("Invert");
run("Ultimate Points");
waitForUser("Shrink the nodes down to single points (zoom in if you can'
t see them)");
run("Multiply...", "value=255");
run("Make Binary");
run("Set Measurements...", "centroid redirect=None decimal=0");
run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00 show=[Count M
asks] display clear in_situ");
close();
for (n=0;n<nResults;n++) {
X=getResult("X",n);
Y=getResult("Y",n);
makeRectangle(X-6, Y-6, 10, 10);
setKeyDown("shift");
}
setKeyDown("none");
waitForUser(nResults + " nodes are displayed on-screen. See Results Table for co
ordinates. End of macro");
wait(100);
run("Revert");

You might also like