Discussion:
Smoothing a ROI
Gabriel Landini
2011-02-13 13:43:23 UTC
Permalink
Hi,
I am trying to smooth an ROI by doing a running a series of average of size 3
on the coordinates of the ROI:

//------------8<----------------------
getSelectionCoordinates(x, y);
run("Select None");
makeSelection("polygon", x, y);

l=x.length;
print(l);
nx=newArray(l);
ny=newArray(l);

for (i=1;i<l-1;i++){
nx[i]=round(((x[i-1]+x[i]+x[i+1])/3)+random-0.5);
ny[i]=round(((y[i-1]+y[i]+y[i+1])/3)+random-0.5);
}

nx[0]=round(((x[l-1]+x[0]+x[1])/3)+random-0.5);
ny[0]=round(((y[l-1]+y[0]+y[1])/3)+random-0.5);

nx[l-1]=nx[0];
ny[l-1]=ny[0];

run("Select None");
makeSelection("polygon", nx, ny);
//------------8<----------------------

Although this kind-of-works to show the idea, the number of ROI coordinates
remains constant while the ROI slowly contracts, which means that there must
be several repeated ROI coordinates (which cannot be seen as they overlap).
So this has the disadvantage that after several passes some coordinates are
made of many superimposed points that do not get smoothed at the rate one
would expect.

Is there any way of getting rid of the repeated ROI coordinates other than
creating a new array which is populated with only the unique coordinates?

I also tried a version of ROI closing, but this is not exactly what I want:
for (i=1;i<10;i++) {
run("Enlarge...", "enlarge="+i);
run("Enlarge...", "enlarge=-"+i);
}

Thanks for any pointers

Cheers

Gabriel
Johannes Schindelin
2011-02-13 16:07:27 UTC
Permalink
Hi,
Post by Gabriel Landini
I am trying to smooth an ROI by doing a running a series of average of
It might be easier to smooth the mask, apply an automatic threshold, and
create a new selection.

Ciao,
Dscho
Gabriel Landini
2011-02-13 20:59:27 UTC
Permalink
Thanks Dscho for the suggestion.
I ended up thinning the ROI coordinates. Maybe this is useful to somebody
else, so below is the macro.
If somebody spots any problems or improves it please let me know.

Cheers
Gabriel

//---------------------8<-------------------
// smooth_ROI.txt
// G. Landini at bham. ac. uk
// 13/2/2011
// Performs a running average of size 3 on a closed ROI to smooth it

sel=selectionType();
if (sel <2 ||sel>4)
exit("Only freehand closed ROIs are supported.");

getSelectionCoordinates(x, y);
run("Select None");
makeSelection("polygon", x, y);

l=x.length;
print(l);
nx=newArray(l);
ny=newArray(l);

for (i=1;i<l-1;i++){
nx[i]=round(((x[i-1]+x[i]+x[i+1])/3)+random-0.5);
ny[i]=round(((y[i-1]+y[i]+y[i+1])/3)+random-0.5);
}

// l-2 because last coordinate==first
nx[0]=round(((x[l-2]+x[0]+x[1])/3)+random-0.5);
ny[0]=round(((y[l-2]+y[0]+y[1])/3)+random-0.5);
//last
nx[l-1]=nx[0];
ny[l-1]=ny[0];

run("Select None");

// count how many are overlapping
tot=0;
for (i=1;i<l;i++){
if ( (nx[i]== nx[i-1]) && (ny[i] == ny[i-1] ) )
tot++;
}

if (tot>0) {
nex=newArray(l-tot);
ney=newArray(l-tot);
nex[0]=nx[0];
ney[0]=ny[0];

st=0;
for (j=1;j<l;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);
}
else
makeSelection("polygon", nx, ny);

//---------------------8<-------------------
giuseppe3
2014-09-09 01:41:17 UTC
Permalink
Hi Gabriel,

I had a play with your ROI smoothing macro and it works well for me.

However, I don't understand what the code is doing in the second part of the
macro when you check for overlapping coordinates.
Could please explain.

I also made a version where the average is out of the 5 neighboring points
of the selection. This also works but I didn't include the code that checks
for overlapping coordinates.

Thanks,

Giuseppe Lucarelli


Thanks Dscho for the suggestion.
I ended up thinning the ROI coordinates. Maybe this is useful to somebody
else, so below is the macro.
If somebody spots any problems or improves it please let me know.

Cheers
Gabriel

//---------------------8<-------------------
// smooth_ROI.txt
// G. Landini at bham. ac. uk
// 13/2/2011
// Performs a running average of size 3 on a closed ROI to smooth it

sel=selectionType();
if (sel <2 ||sel>4)
exit("Only freehand closed ROIs are supported.");

getSelectionCoordinates(x, y);
run("Select None");
makeSelection("polygon", x, y);

l=x.length;
print(l);
nx=newArray(l);
ny=newArray(l);

for (i=1;i&lt;l-1;i++){
nx[i]=round(((x[i-1]+x[i]+x[i+1])/3)+random-0.5);
ny[i]=round(((y[i-1]+y[i]+y[i+1])/3)+random-0.5);
}

// l-2 because last coordinate==first
nx[0]=round(((x[l-2]+x[0]+x[1])/3)+random-0.5);
ny[0]=round(((y[l-2]+y[0]+y[1])/3)+random-0.5);
//last
nx[l-1]=nx[0];
ny[l-1]=ny[0];

run(&quot;Select None&quot;);

// count how many are overlapping
tot=0;
for (i=1;i&lt;l;i++){
if ( (nx[i]== nx[i-1]) &amp;&amp; (ny[i] == ny[i-1] ) )
tot++;
}

if (tot&gt;0) {
nex=newArray(l-tot);
ney=newArray(l-tot);
nex[0]=nx[0];
ney[0]=ny[0];

st=0;
for (j=1;j<l;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(&quot;polygon&quot;, nex, ney);
}
else
makeSelection(&quot;polygon&quot;, nx, ny);

//---------------------8&lt;-------------------
&lt;/quote>




--
View this message in context: http://imagej.1557.x6.nabble.com/Smoothing-a-ROI-tp3685699p5009550.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Gabriel Landini
2014-09-09 11:00:48 UTC
Permalink
Post by giuseppe3
I had a play with your ROI smoothing macro and it works well for me.
However, I don't understand what the code is doing in the second part of the
macro when you check for overlapping coordinates.
Could please explain.
Hi,
It gets rid of overlapping coordinates of the ROI that do not contribute to
the actual shown shape. If you run consecutively the macro several times you
end up with lots of repeated ROI points in the ROI chain that do not make a
difference to the ROI that you see. The code gets rid of those.
If there are no overlapping points it uses the original smoothed chain, if
there are, it uses the thinned chain.

Regards

Gabriel

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
giuseppe3
2014-09-09 11:54:14 UTC
Permalink
Thank you so much Gabriel!

On 9 September 2014 20:56, Gabriel Landini [via ImageJ] <
Post by Gabriel Landini
Post by giuseppe3
I had a play with your ROI smoothing macro and it works well for me.
However, I don't understand what the code is doing in the second part of
the
Post by giuseppe3
macro when you check for overlapping coordinates.
Could please explain.
Hi,
It gets rid of overlapping coordinates of the ROI that do not contribute to
the actual shown shape. If you run consecutively the macro several times you
end up with lots of repeated ROI points in the ROI chain that do not make a
difference to the ROI that you see. The code gets rid of those.
If there are no overlapping points it uses the original smoothed chain, if
there are, it uses the thinned chain.
Regards
Gabriel
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
------------------------------
If you reply to this email, your message will be added to the discussion
http://imagej.1557.x6.nabble.com/Smoothing-a-ROI-tp3685699p5009553.html
To unsubscribe from Smoothing a ROI, click here
<http://imagej.1557.x6.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3685699&code=Z2l1c2VwcGUubHVjYXJlbGxpQG1vbmFzaC5lZHV8MzY4NTY5OXwzMjI5Nzc0MTk=>
.
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>
--
Giuseppe Lucarelli
Biochemistry PhD student
Prescott/Devenish Lab, Bld 13 D224a
PH 990 53785

<giuseppe.lucarelli-***@public.gmane.org>




--
View this message in context: http://imagej.1557.x6.nabble.com/Smoothing-a-ROI-tp3685699p5009554.html
Sent from the ImageJ mailing list archive at Nabble.com.

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