Login  Register

Re: trouble specifying a ROI in ROI Manager

Posted by Hugo A. M. Torres on Feb 17, 2009; 4:13pm
URL: http://imagej.273.s1.nabble.com/Macros-to-read-and-write-Complex-Fourier-Transform-tp3693680p3693690.html

On Sat, 2009-02-14 at 23:42 -0500, Rasband Wayne wrote:

> On Feb 14, 2009, at 11:07 AM, Hugo A. M. Torres wrote:
>
> > Hi Wayne,
> >
> > Thanks for helping me with that. I can understand from that code  
> > you are
> > doing exactly what I said, namely compute a square of sides whose  
> > length
> >  is the square root of the triangle's area.
> >
> > Yet, If I measure the area of the resulting triangle, it is actually
> > somewhat smaller than the triangle's.
> >
> > So maybe Joris was right about  the area being measured only inside  
> > the
> > selection's boundaries discounting the pixels of the border?
>
> ImageJ measures the area of a selection by doing a polygon fill and  
> counting pixels. You can see what the polygon fill does by using the  
> Edit>Fill command. Zoom in and you will see that some of the pixels  
> extend beyond the selection boundary.
>
> Here is a macro that compares the polygon fill pixel count to the  
> area calculated from the polygon vertices.
>
>     getRawStatistics(count);
>     getSelectionCoordinates(x, y);
>     area = 0;
>     n = x.length;
>     for (i=0; i<n; i++) {
>        iminus1 = i-1;
>        if (iminus1<0) iminus1=n-1;
>        area += (x[i]+x[iminus1])*(y[i]-y[iminus1]);
>     }
>     area = abs(area/2);
>     print("");
>     print("pixel count: "+count);
>     print("area: "+area);
>     print("differencet: "+(count-area)*100/count+"%");
>
> -wayne
>
> >
> > Hugo
> >
> > On Fri, 2009-02-13 at 15:56 -0500, Wayne Rasband wrote:
> >> Here is a macro that creates a square selection that has the same  
> >> area
> >> as an existing selection.
> >>
> >>    getSelectionBounds(x, y, width, height);
> >>    getRawStatistics(area);
> >>    size = sqrt(area);
> >>    x2 = x + width/2 - size/2;
> >>    y2 = y + height/2 - size/2;
> >>    makeRectangle(x2, y2, size, size);
> >>
> >> -wayne
> >>
> >> On Feb 13, 2009, at 3:27 PM, Hugo A. M. Torres wrote:
> >>
> >>> I tried that, didn't work. Maybe I am still having rounding issues
> >>> because of the area being reported in micrometers.
> >>>
> >>> I am not experienced in writing macros, but would it be possible
> at
> >>> least in principle to write a macro that specifies a square ROI
> >>> according to an area value input?
> >>>
> >>> Hugo


Hello again Wayne,

I run the macro that compares pixel count in both ROIs and it seems the
"ROI-converter" ( The one you sent me, I am calling it this way)  misses
around 600pixels. I tryed  to fix the macro by calculating the perimeter
and adding a little extra lengh to the size of the square ROI. Namely,
the extra length should correspond to the to the the area taken by the
perimeter of the triangle, please see if you can understand what I tried
to do:


getSelectionBounds(x, y, width, height);
   getRawStatistics(area, perimeter);
   size = (2*area + perimeter)/(2*sqrt(area));
   x2 = x + width/2 - size/2;
   y2 = y + height/2 - size/2;
   makeRectangle(x2, y2, size, size)


Triangle ROI
pixel count: 94843
area: 94842
differencet: 0.0011%
Measured area in square micrometers: 41412.942

Wayne's square ROI:
pixel count: 94249(misses 594 pixels)
area: 94249
differencet: 0%
Measured area in square micrometers: 41153.574

Hugo's square ROI
pixel count: 94864 (still misses 21 pixels)
area: 94864
differencet: 0%
Measured area in square micrometers: 41422.112

Is it possible to improve this further, or should I consider this normal
computational error?