Detect if images are overlapping - hit test

Hi All,

I have two irregular shaped PNG images and would like to detect when they overlap. How would I go about this .

Thanks in advance…

What do you mean by “irregular shaped” PNG images?

Hi Christopher - I have images that don’t fill the bounding box, for example a polygon and an line at an angle. I can detect when the bounding boxes overlap, but not sure how to detect when the pixels overlap. The images will be more complex than the examples…

Hi there!

I think I would go with a shader and then analyse the output.

You could compare, per fragment, if you have two (or more) colours. If so, you output white. Else, you output black. You do this to a small texture and then you loop around those pixels. As soon as you find something that is not black, you know they are overlapping.

Don’t think is elegant or smart, but is fast and pixel-precise.

Do you, by chance have the polygon outlines of the shapes? If so, you can iterate through the polygon points and test them using ofPolyline’s isInside function.

Usually these tests (collisions testing in Box2d for instance) are done using simple convex polyline outlines. To reduce the search space (it doesn’t scale well to test all polygon points against all other polygon points) the space can be partitioned using spatial hashes (e.g. ofxSpatialHash). Testing for AABB (axis aligned bounding box) overlap is also a good fast way to reduce the search space.

If you’re looking for purely graphical ways of doing it … I’m not really sure what the best solution would be – perhaps using an openGl depth buffer w/in an FBO? If I only had shapes and was doing collision testing, I’d probably find the convex hull (or blob contour) of the shape using OpenCV and thresholding on the alpha background to to just find the contours / blobs of the foreground.

This all depends on how precise you need to be though … and if you need to know exactly which shapes are overlapping and how they are overlapping, etc.

ofPolyline’s inside function does the trick!

I think I’ll still explore the graphical / texture methods as well