How not to crop image when using ThinPlateSplineShapeTransformer

asked 2018-03-09 05:59:22 -0600

pakoun gravatar image

updated 2018-03-12 05:39:26 -0600

Dear all,

I am using the python bindings to cv and I perform a thin plate spline transformation using cv2.createThinPlateSplineShapeTransformer()

My problem is how to display the whole image in the case of warping beyond the matrix dimensions. If i do so then the image is cropped. Hope the following example code will illustrate this issue and help to find an answer. Unfortunately seems that I can not attach an example file...

import cv2
import numpy as np
import matplotlib.pyplot as plt

tps = cv2.createThinPlateSplineShapeTransformer()
pnt1 = [30,0]
pnt2 = [2900,120]
pnt3 = [2600,2600]
pnt4 = [0,2600]
pnt5 = [10,190]
pnt6 = [10,240]
sshape = np.array([[0,0],[2600,0],[2600,2600],[0,2600],[10,190],[10,240]],np.float32)
tshape = np.array([pnt1, pnt2, pnt3, pnt4, pnt5, pnt6],np.float32)
sshape = sshape.reshape(1,-1,2)
tshape = tshape.reshape(1,-1,2)
matches = list()
matches.append(cv2.DMatch(0,0,0))
matches.append(cv2.DMatch(1,1,0))
matches.append(cv2.DMatch(2,2,0))
matches.append(cv2.DMatch(3,3,0))
matches.append(cv2.DMatch(4,4,0))
matches.append(cv2.DMatch(5,5,0))
tps.estimateTransformation(tshape,sshape,matches)
img = cv2.imread('test.tiff', 1)
out_img = tps.warpImage(img)

fig, ax = plt.subplots()
ax.set_xlim([-100,3000])
ax.set_ylim([-100,3000])
plt.imshow(out_img)
plt.savefig("warped")

Some more information after realizing that this is quite important as it is kindly been mentioned to me. With respect the user case, I have some remote sensing sea-ice images. My goal is to apply suitable vectors at specified random points (first case e.g. only at the corners) in order to investigate how the ice features moves with the time. Since I want to allow them to be very flexible with the movement I think the TPS method is the most appropriate one for that. Hope that helps a bit more..

edit retag flag offensive close merge delete

Comments

the ThinPlateSplineShapeTransformer class there is not a general warping solution, it's an internal part of the shape matching process

berak gravatar imageberak ( 2018-03-09 21:56:25 -0600 )edit

From what I see there is the cv2.warpAffine method which by selecting appropriate bounds the warped image will be not cropped. However this makes an affine transformation. But is there a way to use the TPS transformation with another warping function that allows me to get the whole image?

pakoun gravatar imagepakoun ( 2018-03-12 04:31:47 -0600 )edit

"is there a way to use the TPS transformation with another warping function" -- no.

berak gravatar imageberak ( 2018-03-12 04:34:11 -0600 )edit
1

thanks for your time and help berak. Well now it seems a bit difficult what I want to achieve here..

pakoun gravatar imagepakoun ( 2018-03-12 05:03:31 -0600 )edit

"what I want to achieve here.." -- well, that information was missing in the 1st place !!

berak gravatar imageberak ( 2018-03-12 05:08:12 -0600 )edit

You mean its not clear my question? If thats the case what I would like to do is to use the TPS method to warp an image.The above code is doing so. Please correct me but if I understand correctly the procedure, TPS is basically mapping source and target pixels and performs an interpolation. So basically sets the rules how to warp the image. The problem is that the image is moved to a certain direction and it is cropped, meaning that there are pixels that are not displayed any more. I am wondering how I can warp the image without being cropped even for more extreme transformations.

pakoun gravatar imagepakoun ( 2018-03-12 05:20:22 -0600 )edit

why do you want to use TPS ? (why not warpAffine or perspective ?)

what do your points signify ? what is your use-case ? context ?

all of it missing above

berak gravatar imageberak ( 2018-03-12 05:23:19 -0600 )edit
2

Ups you are 100% right I added some extra info in my question, I hope that helps now..

pakoun gravatar imagepakoun ( 2018-03-12 05:40:26 -0600 )edit
2

"why not warpAffine or perspective" -- Affine for example is a linear transformation, hence the parallel lines will remain parallel after warping. However I need the flexibility to warp points in every possible direction and also if I set a point as a stable point, then it should not move.

pakoun gravatar imagepakoun ( 2018-03-12 05:59:28 -0600 )edit

sounds like you need to build your own TPS then. see remap

berak gravatar imageberak ( 2018-03-12 06:25:15 -0600 )edit