/* Smooths a selection based on a median or average of adjacent points Original based on G. Landini at bham. ac. uk macro of 13/2/2011 that performed a running average of size 3 on a closed ROI to smooth it: http://imagej.1557.x6.nabble.com/Smoothing-a-ROI-td3685699.html This variant adds median and average options and allows you so choose the range over which smoothing is applied PJL v180606 */ sel=selectionType(); if (sel <2 ||sel>4) exit("Only freehand closed selections are supported."); getSelectionCoordinates(x, y); run("Select None"); makeSelection("polygon", x, y); l=x.length; Dialog.create("Selection Smoothing Options"); Dialog.addMessage(l + " original coordinates"); Dialog.addNumber("Smoothing radius in coordinate points", 5); Dialog.addNumber("Iterations", 1); smoothingChoice = newArray("Average", "Median"); Dialog.addRadioButtonGroup("Smoothing type", smoothingChoice, 1, 2, "Average"); Dialog.show; sRad = Dialog.getNumber(); iterations = Dialog.getNumber(); smoothing = Dialog.getRadioButton(); n = sRad*2+1; if(n>(l-2*n)) exit ("Too few points describing selection"); x = Array.concat(x,x,x); /* for continuous ends at the expense of additional time */ y = Array.concat(y,y,y); /* for continuous ends at the expense of additional time */ nX=newArray(3*l); nY=newArray(3*l); nXAvg = newArray(n); nYAvg = newArray(n); for (it=0; it0) { nex=newArray(l-tot); ney=newArray(l-tot); nex[0]=nX[0]; ney[0]=nY[0]; st=0; for (j=1;j<(3*l-n);j++){ if ((nX[j] != nX[j-1]) || (nY[j] != nY[j-1]) ) { st++; nex[st]=nX[j]; ney[st]=nY[j]; } } nex[l-tot-1]=nex[0]; ney[l-tot-1]=ney[0]; makeSelection("polygon", nex, ney); showStatus(l + "original coordinates reduced to " + lengthOf(nex)); } else { makeSelection("polygon", nX, nY); showStatus(l + "original coordinates reduced to " + lengthOf(nX)); }