Ask Your Question
0

applyTransformation not working in python?

asked 2019-05-22 09:52:25 -0500

nbubis gravatar image

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

berak gravatar image

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.

edit retag flag offensive close merge delete

Comments

I'd expect that the transform from points in pts1 should lead to points in pts2

it's the other way round

berak gravatar imageberak ( 2019-05-22 11:49:00 -0500 )edit

@berak fine, but different points should still map differently, and to reasonable coordinates.

nbubis gravatar imagenbubis ( 2019-05-22 12:08:36 -0500 )edit

@berak - Moreover, all points map to a single point - that is clearly wrong behavior.

nbubis gravatar imagenbubis ( 2019-05-22 13:07:16 -0500 )edit

Did you solve your problem? I am also in trouble with this.

kian gravatar imagekian ( 2019-06-03 21:13:50 -0500 )edit

@kian -- however, please do not post answers here, if you have a question or a comment, thank you.

berak gravatar imageberak ( 2019-06-04 00:18:56 -0500 )edit

I'm also having the same issue. warpImage() works fine but applyTransformation() gives wrong mapped points.

zachzeyuwang gravatar imagezachzeyuwang ( 2020-04-01 20:55:10 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-06-12 01:46:56 -0500

You did not declare the input image (img).
cv2.imread("input.png")//-->must this line be img = cv2.imread("input.png")

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)]

img = 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)
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-05-22 09:52:25 -0500

Seen: 544 times

Last updated: May 22 '19