How to use createThinPlateSplineShapeTransformer with Python?

asked 2018-01-22 07:09:34 -0500

janonespe gravatar image

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 = []

pts2 = []

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

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

    (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?

edit retag flag offensive close merge delete


just saying, the ThinPlateSplineShapeTransformer class is just a helper class, meant to be used with Shape Context Matching. you probably should not try (ab)use it for arbitrary transformations

getPerspectiveTransform or findHomography would be far more adequate here.

berak gravatar imageberak ( 2018-01-22 07:40:27 -0500 )edit

Yes, yes, I know, I have my shapes defined already elsewhere, I'm just playing with it to see how it works, as the Python documentation is quite poor on the details, and found myself unable to make it work. The problem is, the object I am detecting is not a plane in the world (it's a cylinder seen from a side, which makes it kind of two curves with straight lines) so I can't use homographies.

janonespe gravatar imagejanonespe ( 2018-01-22 07:43:42 -0500 )edit

i see. unfortunately, it's not only the python documentation, which is sparse on this. ;(

maybe you have to attack remap() in a more direct way (or , more specific to your problem)

berak gravatar imageberak ( 2018-01-22 07:57:53 -0500 )edit

hello, when using tps.applyTransformation() function in python, I also meet with this same question . And you have known that's why and how to deal with it ? Maybe you can help me .Thanks

BUAA gravatar imageBUAA ( 2018-07-26 03:56:24 -0500 )edit