Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i can't test with 2.4, but opencv3 can compare binary images, using matchShapes:

>>> m = np.eye(10,10,dtype=np.uint8) # just the diagonal
>>> m2 = m[2:6,2:6]=1                # add a little rect
>>> cv2.matchShapes(m,m2,1,0)        # compare
9.974179503561947

to test against multiple shapes, you'll have to apply a nearest neighbour search:

s0 = ... # template shape
best_shape = None
best_dist = 9999999999 
for s in shapes: # image or contour, no matter
     dist = cv2.matchShapes(s0, s, 1,0)
     if dist < best_dist:
          best_dist = dist
          best_shape = s

# now use best_shape ..

but again, if you want to use images here, the assumption is, that there's a single shape in the image.

(you cannot calculate moments() for multiple shapes)

i can't test with 2.4, but opencv3 can compare binary images, using matchShapes:

>>> m = np.eye(10,10,dtype=np.uint8) # just the diagonal
>>> m2 = m[2:6,2:6]=1                # add a little rect
>>> cv2.matchShapes(m,m2,1,0)        # compare
9.974179503561947

to test against multiple shapes, you'll have to apply a nearest neighbour search:

s0 = ... # template shape
best_shape = None
best_dist = 9999999999 
for s in shapes: # image or contour, no matter
     dist = cv2.matchShapes(s0, s, 1,0)
     if dist < best_dist:
          best_dist = dist
          best_shape = s

# now use best_shape ..

but again, if you want to use images here, the assumption is, that there's a single shape in the image.

(you (as you cannot calculate moments() for multiple shapes)

shapes in the same image)