Login  Register

Re: Measuring Angles between adjacent particles

Posted by dobensl on Feb 02, 2017; 7:43pm
URL: http://imagej.273.s1.nabble.com/Measuring-Angles-between-adjacent-particles-tp5018001p5018008.html

I have been trying to develop a macro to do this to detect changes in the orientation of bristles in the Drosophila wing - it relies on a beautiful “nearest Neighbor” macro written by Michael Cammer - keep in mind that I am a rank amateur in writing these macros, and I would welcome advice on improvements, but the following macro works for a “test image” that I’ve been using

Len Dobens



*************************


run("Revert");

run("Make Binary");
 run("Set Measurements...", "centroid center feret's redirect=None decimal=3");

Dialog.create("Minimum particle size");
  Dialog.addNumber("Width:", 5);
    Dialog.show();
  z = Dialog.getNumber();

 run("Analyze Particles...", "size=z-Infinity show=Overlay display exclude clear add");


 run("To ROI Manager");
 roiManager("Show All without labels");

run("RGB Color");

title = "Nearest neighbor";
  width=1024; height=1024;
  Dialog.create("Nearest neighbor");
  Dialog.addNumber("Angle difference:", 10);
  Dialog.addChoice("number of neighbors:", newArray("1", "2", "3"));

  Dialog.show();
  angle = Dialog.getNumber();
  neighbors = Dialog.getChoice();

/*  Next step is to save all 1st and 2nd shortest pairs with their distances.
     If distance greater than certain criteria, such as 15% longer than avg dist, then sever it.
     Then sort into clusters and do convex hull.

     Michael Cammer  20150707 for ImageJ 1.50a
*/

var shortestColor1 = "yellow";
var shortestColor2 = "red";
var shortestColor3 = "blue";
var shortestColor4 = "green";

macro "shortest and 2nd shortest [q]" {
    run("Select None");
  // This part of the macro gets the XY locations of the points.  It could
  //  be replaced with some other method such as opening a text file or measuring
  //  centroids of objects by some other method.
  run("Clear Results");
  run("Set Measurements...", "centroid redirect=None decimal=3");
  run("Find Maxima...", "noise=10 output=List");
  x = newArray(nResults());
  y = newArray(nResults());
  feret = newArray(nResults());
  for(row=0; row<nResults(); row++) {
    x[row] = getResult("X", row);
    y[row] = getResult("Y", row);
    feret[row] = getResult("FeretAngle", row);

  }  // for row to get XY locations

  //  calculate shortest and 2nd shortest
  run("Remove Overlay");
  dist = newArray(x.length);
  for(i=0; i<x.length; i++) {
    for(k=0; k<x.length; k++) {
        dist[k] =  sqrt(((y[i] - y[k]) * (y[i] - y[k])) + ((x[i] - x[k]) * (x[i] - x[k])));
    } // for k inside loop
    ranking = Array.rankPositions(dist);
    shortest0 = ranking[0];
    shortest1 = ranking[1];     //  ranking[0] will always give the position of 0 in the distance array
    shortest2 = ranking[2];
    shortest3 = ranking[3];

    // uncomment next three lines to show method
    // Array.print(dist);
// Array.print(ranking);
//  print("");


doWand(x[i],y[i]);
List.setMeasurements;
degrees1 = List.getValue("FeretAngle");

doWand(x[shortest1],y[shortest1]);
List.setMeasurements;
degrees2 = List.getValue("FeretAngle");

doWand(x[shortest2],y[shortest2]);
List.setMeasurements;
degrees3 = List.getValue("FeretAngle");

doWand(x[shortest3],y[shortest3]);
List.setMeasurements;
degrees4 = List.getValue("FeretAngle");

if (neighbors == 1) {

if (abs(degrees1 - degrees2) > angle) {
doWand(x[shortest1],y[shortest1]);
run("Add Selection...", "stroke=none width=0 fill=red");
}
}

if (neighbors == 2) {

if (abs(degrees1 - (degrees2 + degrees3)/2) > angle) {
doWand(x[shortest1],y[shortest1]);
run("Add Selection...", "stroke=none width=0 fill=red");
}
}

if (neighbors == 3) {

if (abs(degrees1 - (degrees2 + degrees3 + degrees4)/3) > angle) {
doWand(x[shortest1],y[shortest1]);
run("Add Selection...", "stroke=none width=0 fill=red");
}
}
makeLine(x[i],y[i], x[shortest1],y[shortest1], 1);
    Overlay.addSelection(shortestColor1);

    makeLine(x[i]+2,y[i]+2, x[shortest2]+2,y[shortest2]+2, 1);  // offset to help show when there are shortest & 2nd shortest
    Overlay.addSelection(shortestColor2);
    makeLine(x[i]-2,y[i]-2, x[shortest3]-2,y[shortest3]-2, 1);  // offset to help show when there are shortest & 2nd shortest
    Overlay.addSelection(shortestColor3);


  }


On Feb 2, 2017, at 12:43 PM, DarthRevan109 <[hidden email]<mailto:[hidden email]>> wrote:

Hi Herbie,

Thanks for the response, that's the answer I expected but feared. Thanks as well for your suggestions I will keep them in mind.

Cheers,



Sent from my iPhone

On Feb 2, 2017, at 11:52 AM, Herbie [via ImageJ] <[hidden email]<mailto:[hidden email]><mailto:[hidden email]>> wrote:

Good day!

Evidently this is a cross-post from the forum.

I'm pretty sure that no such ImageJ-code exists yet.

When thinking about writing such code, a fundamental question concerns
the typical number of particles in an image and their properties (e.g.
are they strictly oriented?).

A _typical_ image may help.

Best

Herbie

:::::::::::::::::::::::::::::::::::::::::::
Am 02.02.17 um 18:32 schrieb DarthRevan109:
There are available and useful plugins such as Directionality to measure the
orientation of particle across an entire image. Does anyone have a plugin
they suggest that can measure the angle between adjacent particles, and
preferably display the data with a histogram and image of the original image
with numbered particles? It would be like an orientation of a nearest
neighbor.

Thanks!

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html


________________________________
If you reply to this email, your message will be added to the discussion below:
http://imagej.1557.x6.nabble.com/Measuring-Angles-between-adjacent-particles-tp5018001p5018004.html
To unsubscribe from Measuring Angles between adjacent particles, click here<
NAML<
http://imagej.1557.x6.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>




--
View this message in context: http://imagej.1557.x6.nabble.com/Measuring-Angles-between-adjacent-particles-tp5018001p5018006.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Leonard Dobens, PhD
Professor of Molecular Biology and Biochemistry
SCB312
School of Biological Sciences
University of Missouri-Kansas City
5007 Rockhill Road
Kansas City, MO 64110
8162356272
[hidden email]<mailto:[hidden email]>




--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html