Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

applyTransformation not working in python?

Trying to work with the Thin Plate Spline warping in python. The actual warping works just fine:

tps = cv2.createThinPlateSplineShapeTransformer()
pts1 = [(10, 10), (100, 100), (50, 100), (200, 30), (60, 20), (150, 200), (120, 90)]
pts2 = [(15, 5), (110, 105), (55, 105), (190, 35), (70, 20), (155, 205), (115, 95)]
cv2.imread("input.png")
matches = [cv2.DMatch(i, i, 0) for i in range(7)]
tps.estimateTransformation(np.array(pts1).reshape((-1, 7, 2)), np.array(pts2).reshape((-1, 7, 2)), matches)
dst = tps.warpImage(img)
cv2.imwrite('warped.png', dst)

The problem seems to be in the applyTransformation. I'd expect that the transform from points in pts1 should lead to points in pts2, but it seems the transform maps all points to a single (wrong) point:

>>> tps.applyTransformation(np.array((10,10)).reshape((-1, 1, 2)))
(7.34408968128264e-05, array([[[ 4.386129 , -6.4083347]]], dtype=float32)) 
>>> tps.applyTransformation(np.array((100,100)).reshape((-1, 1, 2)))
(7.34408968128264e-05, array([[[ 4.386129 , -6.4083347]]], dtype=float32))

What can be the issue? OpenCV version is 3.4.2.

click to hide/show revision 2
retagged

updated 2019-05-22 11:49:40 -0600

berak gravatar image

applyTransformation not working in python?

Trying to work with the Thin Plate Spline warping in python. The actual warping works just fine:

tps = cv2.createThinPlateSplineShapeTransformer()
pts1 = [(10, 10), (100, 100), (50, 100), (200, 30), (60, 20), (150, 200), (120, 90)]
pts2 = [(15, 5), (110, 105), (55, 105), (190, 35), (70, 20), (155, 205), (115, 95)]
cv2.imread("input.png")
matches = [cv2.DMatch(i, i, 0) for i in range(7)]
tps.estimateTransformation(np.array(pts1).reshape((-1, 7, 2)), np.array(pts2).reshape((-1, 7, 2)), matches)
dst = tps.warpImage(img)
cv2.imwrite('warped.png', dst)

The problem seems to be in the applyTransformation. I'd expect that the transform from points in pts1 should lead to points in pts2, but it seems the transform maps all points to a single (wrong) point:

>>> tps.applyTransformation(np.array((10,10)).reshape((-1, 1, 2)))
(7.34408968128264e-05, array([[[ 4.386129 , -6.4083347]]], dtype=float32)) 
>>> tps.applyTransformation(np.array((100,100)).reshape((-1, 1, 2)))
(7.34408968128264e-05, array([[[ 4.386129 , -6.4083347]]], dtype=float32))

What can be the issue? OpenCV version is 3.4.2.