Javascript Centroid

jersmi's picture

Can anyone advise on how to get this javascript running in QC? Thought it might work in combo with openCV point structures. I know folks are moving towards openCL. Any input on some way to smooth tracking point structures for this non-programmer would be greatly appreciated.

function (__number x, __number y) main (__structure pts, __index nPts)
 
{
var result = new Object();
 
// Point object
function Point(x,y) {
   this.x=pts[0];
   this.y=pts[1];
}
 
// Contour object
function Contour(a) {
   this.pts = []; // an array of Point objects defining the contour
}
// ...add points to the contour...
 
Contour.prototype.area = function() {
   var area=0;
   var pts = this.pts;
   var nPts = pts.length;
   var j=nPts-1;
   var p1; var p2;
 
   for (var i=0;i<nPts;j=i++) {
      p1=pts[i]; p2=pts[j];
      area+=p1.x*p2.y;
      area-=p1.y*p2.x;
   }
   area/=2;
   return area;
};
 
Contour.prototype.centroid = function() {
   var pts = this.pts;
   var nPts = pts.length;
   var x=0; var y=0;
   var f;
   var j=nPts-1;
   var p1; var p2;
 
   for (var i=0;i<nPts;j=i++) {
      p1=pts[i]; p2=pts[j];
      f=p1.x*p2.y-p2.x*p1.y;
      x+=(p1.x+p2.x)*f;
      y+=(p1.y+p2.y)*f;
   }
 
   f=this.area()*nPts;
   return new Point({x:x/f,y:y/f});
}
   return result;
}

This is from Paul Bourke's site: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

cybero's picture
Re: Javascript Centroid

Were you wanting to output a structure or just a series of positions for x and y ?

jersmi's picture
Re: Javascript Centroid

Hi cybero. For my setup I'd like separate outputs for x centroid / y centroid.

Thinking this through a bit more, I could see setting a limit to find centroid for n points.

jersmi's picture
Re: Javascript Centroid

And I would also like to know a good method in this patch to check for NaNs -- if NaN, then use the previous value.

cwright's picture
Re: Javascript Centroid

checking for nans is simple: if it equals itself, it's not a nan (if X is nan, then X != X)

jersmi's picture
Re: Javascript Centroid

Thanks, Chris -- always reassuring to hear from you. I did see your other post from a while back regarding NaN's. Sometimes in my inexperience I'm not 100% about these solutions in context.

jersmi's picture
Re: Javascript Centroid

Just noticed that the Paul Bourke page mentions that this method will fail for polygons that are self intersecting -- in the case of OpenCV structures, because there is no order for points, polygons made of the last n points would intersect often (perhaps most of the time). Here I guess points would have to be sorted, which would add yet another step, convoluting once more this process. sigh....

there are a lot of javascript applications where a centroid is derived from a polygon (maps, browser mouse hovers, feature sets, etc.). just have to land upon the appropriate method.

maybe averaging n x's, averaging n y's....

dust's picture
Re: Javascript Centroid

i just posted a simple blob tracker to the repository. it would be cool to get this js patch working with it. the way i'm doing it is using a 1D blob structure to get my grid index that is then used to pull out x and y from the grid in pixel's. i have checked the blob edge count and it is correct meaning that there are n points where a white pixel precedes a black one as well as the inverse so all you need to do is store those points.

it helps to do a % edge index. that way you can get a second blob. meaning if there are two points on a line that have white pixels preceding a black pixel that means there are two blobs. so using the % helps derive an odd even type of system that is helpful when tagging your blobs.

i think the bourke's c implementation is easier to read.