finding points inside

I am using the contour finder in ofxopencv. Now I need to know if an object position in my opengl space is inside of the contour array. If it was a rectangle, square, or circle it might be easier but since it is an irregular shape I am confused about how to do it.

[edit] I guess I need to find out if I am inside of vectorpts.
ding

courtesy of paul bourke:
http://local.wasp.uwa.edu.au/~pbourke/g-…-nsidepoly/

:smiley:

  
static bool InsidePolygon(ofPoint *polygon, int N, ofPoint p)  
{  
  int counter = 0;  
  int i;  
  double xinters;  
  ofPoint p1,p2;  
  
  p1 = polygon[0];  
  for (i=1;i<=N;i++) {  
    p2 = polygon[i % N];  
    if (p.y > MIN(p1.y,p2.y)) {  
      if (p.y <= MAX(p1.y,p2.y)) {  
        if (p.x <= MAX(p1.x,p2.x)) {  
          if (p1.y != p2.y) {  
            xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;  
            if (p1.x == p2.x || p.x <= xinters)  
              counter++;  
          }  
        }  
      }  
    }  
    p1 = p2;  
  }  
  
  if (counter % 2 == 0) return false;  
  else return true;  
}  

You sir are righteously awesome! :shock:

[EDIT] I was going to just use an ofxCvGrayscaleImage.threshold and just check my objects against the pixelData[x-position*y-position] if the pixelData returned 0 it was in if it returned 255 it was out. But I think your way is faster or am I wrong?

Something like:

  
bool isItIn(const char*pixels, int _x, int _y) {  
    if (pixels[_x*_y] == 0) return true;  
    else return false;  
}  

Thanks,

ding

yeah that could work too!
It just depends on the situation.

If the shape never changes then doing it with pixels would be pretty fast as you are just looking at one pixel per point. If the shape is changing and you are having to generate the thresholded bitmap in code every frame (and it is not something you are making anyway for another purpose) - then I think the point in poly method is better.

:slight_smile:

  
  
bool isItIn(const char*pixels, int _x, int _y) {  
    if (pixels[_x*_y] == 0) return true;  
    else return false;  
}  
  

looks like it should be:

  
  
bool isItIn(const char*pixels, int _x, int _y) {  
    if (pixels[_y*width + _x] == 255) return true;  
    else return false;  
}  
  

where width is the width of the image you care about. checking for 255, since usually white = true. If you don’t have to redraw the blob, then looking at pixels might be faster, but if you have to draw, I think the paul bourke’d be alot faster.

hope that helps!
zach

Thanks zach I would have been scratching my head and googling alot if it weren’t for you.

[EDIT] I would be getting the image from a camera and either:
a)thresholding it, passing it to the contour finder, getting the contour array and using the paul bourke method to figure out if a graphic object is inside someone.
b)thresholding it, using the pixelData to figure out f a graphic object is inside someone.

Something like if inside do this. if outside do that.

ding

sounds like pixels are the way to go.
thresholding is a lot less expensive than thresholding + contour + bourke.

good code to have though!

theo