Login  Register

overlays

Posted by dobensl on Jan 03, 2017; 10:36pm
URL: http://imagej.273.s1.nabble.com/overlays-tp5017831.html

I’ve written a macro that identifies hairs in a photomicrograph, outlines them and draws colored arrows across their Feret’s width with the arrowhead pointing to the thin end of the hair and with a color assigned correlating with the Feret’s angle (calculated over 360 degrees).

I’d like to overlay these arrow on the original image. It works well with a simpler macro making all the arrows are black or all are white, but when I used the colored arrows macro, the overlay is converted to green. Any idea why?

Here’s the very end of the macro (whole macro below)

for (i=0; i<nResults; i++) {
    r = getResult("R",i);
    g = getResult("G",i);
    b = getResult("B",i);
xh = getResult("xh",i);
xt = getResult("xt",i);
yh = getResult("yh",i);
yt = getResult("yt",i);

run("Draw", "slice");
      setTool("arrow");
      run("Color Picker...");
setForegroundColor(r, g, b);
      makeArrow(xt, yt, xh, yh, "notched");
Roi.setStrokeWidth(width);
Overlay.addSelection();
Overlay.copy;
}
run("Close");
open(path_name);
run("Color Picker...");
setForegroundColor(r, g, b);
Overlay.paste;

/////////////////////////////////////////

Here’s the whole macro

var path_name = getDirectory("image")+ getInfo("image.filename");

run("Revert");


  setAutoThreshold("Default");
 run("Set Measurements...", "centroid center feret's redirect=None decimal=3");
 run("Analyze Particles...", "  show=[Overlay Outlines] display exclude clear");
 run("To ROI Manager");
 roiManager("Show All without labels");

title = "Untitled";
  width=512; height=512;
  Dialog.create("Set Arrow");
  Dialog.addNumber("Width:", 51);
  Dialog.show();
  width = Dialog.getNumber();

  resetThreshold();
  Overlay.drawLabels(false);

for (i=0; i<nResults; i++) {
     x11 = getResult("FeretX",i);
     y11 = getResult("FeretY",i);
     length = getResult("Feret",i);
     degrees = getResult("FeretAngle",i);
     if (degrees>90)
        degrees -= 180;
     angle = degrees*PI/180;
     x21 = x11 + cos(angle)*length;
     setResult("x21", i, x21);
     y21 = y11 - sin(angle)*length;
     setResult("y21", i, y21);

     x31 = getResult("X",i);
     y31 = getResult("Y",i);

     makeLine(x31, y31, x11, y11);
     getPixelSize(unit, pw, ph);
     dz = (x31 - x11)*pw;
     da = (y31 - y11)*ph;
     distance1 = sqrt(dz*dz + da*da);
     setResult("D1", i, distance1);
     updateResults;


  makeLine(x31, y31, x21, y21);
     getPixelSize(unit, pw, ph);
     dx = (x31 - x21)*pw;
     dy = (y31 - y21)*ph;
     distance2 = sqrt(dx*dx + dy*dy);
     setResult("D2", i, distance2);
     updateResults;

}

for (i=0; i<nResults; i++) {

     D1 = getResult("D1",i);
     D2 = getResult("D2",i);
     x11 = getResult("FeretX",i);
     y11 = getResult("FeretY",i);
     x21 = getResult("x21",i);
     y21 = getResult("y21",i);

     if (D1 > D2) {
      setResult("xh", i, x11);
  setResult("xt", i, x21);
  setResult("yh", i, y11);
  setResult("yt", i, y21);
  updateResults;setTool("arrow");

   } else {

setResult("xh", i, x21);
  setResult("xt", i, x11);
setResult("yh", i, y21);
setResult("yt", i, y11);
  }
}

for (i=0; i<nResults; i++) {
xh = getResult("xh",i);
xt = getResult("xt",i);
yh = getResult("yh",i);
yt = getResult("yt",i);
degrees = getResult("FeretAngle",i);


if (xh > xt && yh < yt){
angle2 = degrees + 180;
setResult("angle360", i, angle2);
updateResults;
}

if (xh < xt && yh > yt){
angle2 = degrees + 0;
setResult("angle360", i, angle2);
updateResults;
}

if (xh > xt && yh > yt){
angle2 = degrees + 0;
setResult("angle360", i, angle2);
updateResults;
}

if (xh < xt && yh < yt){
angle2 = degrees + 180;
setResult("angle360", i, angle2);
}
}
run("RGB Color");

for (i=0; i<nResults; i++) {
a = getResult("angle360",i);

if (0 < a && a < 60){
r = 255;
g = 4.25*a;
b = 0;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}

if (61 < a && a < 120){
r = -4.25*a + 510;
g = 255;
b = 0;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}
if (121 < a && a< 180){
r = 0;
g = 255;
b = 4.25*a - 510;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}
if (181 < a && a < 240){
r = 0;
g = -4.25*a + 1020;
b = 255;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}
if (241 < a && a < 300){
r = 4.25*a - 1020;
g = 0;
b = 255;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}
if (301 < a && a < 360){
r = 255;
g = 0;
b = -4.25*a + 1530;
setResult("R", i, r);
setResult("G", i, g);
setResult("B", i, b);
updateResults;
}
}

for (i=0; i<nResults; i++) {
    r = getResult("R",i);
    g = getResult("G",i);
    b = getResult("B",i);
xh = getResult("xh",i);
xt = getResult("xt",i);
yh = getResult("yh",i);
yt = getResult("yt",i);

run("Draw", "slice");
      setTool("arrow");
      run("Color Picker...");
setForegroundColor(r, g, b);
      makeArrow(xt, yt, xh, yh, "notched");
Roi.setStrokeWidth(width);
Overlay.addSelection();
Overlay.copy;
}
run("Close");
open(path_name);
run("Color Picker...");
setForegroundColor(r, g, b);
Overlay.paste;



Len Dobens


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