How not to crop image when using ThinPlateSplineShapeTransformer
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..
the ThinPlateSplineShapeTransformer class there is not a general warping solution, it's an internal part of the shape matching process
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?
"is there a way to use the TPS transformation with another warping function" -- no.
thanks for your time and help berak. Well now it seems a bit difficult what I want to achieve here..
"what I want to achieve here.." -- well, that information was missing in the 1st place !!
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.
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
Ups you are 100% right I added some extra info in my question, I hope that helps now..
"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.
sounds like you need to build your own TPS then. see remap