Ask Your Question

Revision history [back]

How to use createThinPlateSplineShapeTransformer with Python?

I am trying to warp some irregular shapes from images to rectangles to ease processing and make the objects in the shape easier to visually inspect, and I am having troubles with the Python implementation of openCV createThinPlateSplineShapeTransformer.

I have read the answers here, with a full C++ implementation and here, with an uncomplete Python, but I am unable to make it work.

This is what I am doing, for testing purpose I am converting a square shape into the very same square with one of its corners displaced:

pts1 = []
pts1.append((0,0))
pts1.append((10,0))
pts1.append((10,10))
pts1.append((0,10))

pts2 = []
pts2.append((0,0))
pts2.append((10,0))
pts2.append((20,10))
pts2.append((0,10))

P1 = np.array(pts1,np.int32)
P2 = np.array(pts1,np.int32)

P1 = P1.reshape((-1,4,2))
P2 = P2.reshape((-1,4,2))

matches = []
for i in range(4):
    matches.append(cv2.DMatch(i,i,0))
tps.estimateTransformation(P1,P2,matches)

So far so good, even though I do not understand the format of the input points. But when I try to transform any points, the result is always [0,0] (or something by e-44):

tps.applyTransformation(P1)
    (0.0, array([[[  0.00000000e+00,   0.00000000e+00],
        [  1.40129846e-44,   0.00000000e+00],
        [  1.40129846e-44,   1.40129846e-44],
        [  0.00000000e+00,   1.40129846e-44]]], dtype=float32))

What am I doing wrong?