4

I'm trying to recognize an object in an image from a previously stored contour of the same object.

Having had immense trouble with feature detection (my objects don't seem to have any features, at least not all of them), I've gotten much better results with the matchShapes() function, which apparently uses Hu moments.

When I have found my matching shape, I get its rotation from comparing its major axis with the x axis, according to this answer: Find the orientation of an image

However, this gives only a rotation between -90 and 90 degrees. I need to know if the object is rotated 180 degrees or not. I have provided some images to further illustrate my issue:180 degrees0 degrees

The above two images show roughly the same angle, when they should be 180 degrees apart. Is there any obvious way of calculating their full rotation? Am I missing something here? I've tried a few hacky methods, but none of them are reliable enough.

Any help or suggestions would be greatly appreciated. Thanks!

Community
  • 1
  • 1
user1547040
  • 61
  • 1
  • 3

1 Answers1

0

This idea is hacky, but will work. Once you have located the object in the image using matchShapes, extract the bounding box, use the findContours function, and then do a sum of sqaured differences with the known contour template. Do the same after rotating the object by 180 degrees. The test case that gives you the least sum of differences will be the right orientation.

Zaphod
  • 1,927
  • 11
  • 13
  • Thanks! I'll try it out. Can I make use of the absDiff() function already in openCV for this? – user1547040 Jun 19 '13 at 15:07
  • Sure, definitely worth a shot. Will be faster than sum of squared differences. – Zaphod Jun 19 '13 at 15:41
  • I will continue this tomorrow, but I thought a bit about this. First of all, doesn't this require the shapes to have equal amounts of points? When they are not equally large, should I remove points? From where? Don't the points have to be sorted for this? Are they? I also had another idea for this problem, where I compared centroid distances. I checked if the contour centroid was closer to the flipped centroid than the center. The biggest problem was that the shapes needed to be exactly on top of each other for it to work. Illustrated here: http://hascanvas.com/resolveorientationdirection – user1547040 Jun 19 '13 at 16:02
  • What you're looking for is called scale invariance. Once you have the bounding box of the object you are looking for, compare the size of the box with the size of the template image. Find the scaling factor that relates the two. Before computing the absDiff, scale the object's bounding box to match the size of the template. Then you will be able to do a difference easily. Using the centroid is a good idea, but if the object is horizontally symmetric, then the centroid will end up in the same place even after a 180 degree rotation. – Zaphod Jun 19 '13 at 16:16