ImageJ base code to get access to all the classes and their methods to test new Plugins. https://imagejdocu.tudor.lu/howto/plugins/the_imagej_eclipse_howto
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

3 years ago
  1. //Smooth Wand version 1d
  2. //N.Vischer
  3. //01.07.17 20:20
  4. var count, polyX, polyY
  5. getSelectionBounds(x, y, w, h);
  6. if (w<2 || h<2)
  7. exit("Width and height must be > 1 px");
  8. if (selectionType != 4)
  9. exit("No traced selection found");
  10. getThreshold(lowThr, hiThr);
  11. getSelectionCoordinates(xx, yy);
  12. len = xx.length;
  13. polyX = newArray(len);
  14. polyY = newArray(len);
  15. count = 0;
  16. for(jj = 0; jj < len; jj++) {
  17. x1 = xx[jj];
  18. y1 = yy[jj];
  19. x2 = xx[(jj+1)%len];
  20. y2 = yy[(jj+1)%len];
  21. dd = 1;
  22. if (y1 == y2) {//horizontal separator
  23. if (x1 > x2) dd = -1;
  24. for(x = x1; x != x2; x+= dd){
  25. processPixelPair(x, y1, x+dd, y1);
  26. }
  27. }
  28. else {//vertical separator
  29. if (y1 > y2) dd = -1;
  30. for(y = y1; y != y2; y+= dd){
  31. processPixelPair(x1, y, x1, y+dd);
  32. }
  33. }
  34. }
  35. polyX = Array.trim(polyX, count);
  36. polyY = Array.trim(polyY, count);
  37. makeSelection("polygon", polyX, polyY);
  38. run("Interpolate", "interval=1 adjust"); //after button released
  39. //processes neighbors of this separator line and adds vertex
  40. function processPixelPair(x1, y1, x2, y2) {
  41. if (x1 == x2) {
  42. val1 = getPixel(x1, minOf(y1, y2));
  43. val2 = getPixel(x1 - 1, minOf(y1, y2));
  44. }
  45. if (y1 == y2){
  46. val1 = getPixel(minOf(x1, x2), y1);
  47. val2 = getPixel(minOf(x1, x2), y1 -1);
  48. }
  49. bright = maxOf(val1, val2);
  50. dark = minOf(val1, val2);
  51. if (bright>=lowThr && dark<=lowThr)
  52. thr = lowThr;
  53. if (bright>=hiThr && dark<=hiThr)
  54. thr = hiThr;
  55. if (dark==bright)
  56. fraction = 0.5;
  57. else
  58. fraction = (thr - dark)/(bright - dark);
  59. if (val1 < val2)
  60. fraction = 1 - fraction;
  61. if (y1 == y2) {
  62. newY = minOf(y1, y2) + fraction - 0.5;
  63. newX = (x1 + x2)/2;
  64. }
  65. if (x1 == x2) {
  66. newX = minOf(x1, x2) + fraction - 0.5;
  67. newY = (y1 + y2)/2;
  68. }
  69. polyX[count] = newX;
  70. polyY[count] = newY;
  71. count++;
  72. if (count == polyX.length){
  73. polyX = Array.concat(polyX, polyX);
  74. polyY = Array.concat(polyY, polyY);
  75. }
  76. }