Warping images using control points

72 views
Skip to first unread message

Sid

unread,
Jul 7, 2016, 3:37:16 AM7/7/16
to scikit-image
Hi

I have two images of faces, the source image and the destination image. For each face, I have a set of x and y coordinates stored in two separate numpy arrays. These coordinates correspond to various facial features, as well as the four corners of the image, and points on half the length of the borders.

I want to warp the features of the source image onto the destination, such that the corresponding control points in each image are aligned. I used the PiecewiseAffineTransform method, as in the example here.

My code is:

src_pts = # numpy array of coordinates for the source image
dst_pts
= # numpy array of coordinates for the destination image

image
= # image of dimensions (550, 420, 3)

tform
= skimage.transform.PiecewiseAffineTransform()
tform
.estimate(src_pts, dst_pts)

out = skimage.transform.warp(image, tform)
 

I get an image that is warped but also cropped. To see if I could get the code to work (in case there were errors in my control points), I tried warping the image using the same control points for the source and destination.

tform.estimate(src_pts, src_pts)

The output is still cropped and is also warped slightly, as can be seen in the attached images. Logically, it doesn't make sense as there should not be any warping since I am using the same points. A big difference between the example and my implementation is that the example has uniformly spaced points, whereas I have 76 points which are much more sparse. Is this the right function to achieve what I want or should I be using something else? 


Thanks

--
Sid







Stéfan van der Walt

unread,
Jul 11, 2016, 3:35:36 PM7/11/16
to scikit-image
Hi Sid

Sorry, I think you've been a victim of a known inconsistency---unfortunately, one I'm not sure how to address.

The transforms module uses a (x, y) coordinate convention to be consistent with most of the warping literature out there.  But the rest of scikit-image uses  a (row, column) convention.

The following code works for me:

from skimage import io, transform
import numpy as np

image = io.imread('image.jpg')
h, w = image.shape[:2]

rng = np.random.RandomState(0)
xs = rng.randint(0, w - 1, 76)
ys = rng.randint(0, h - 1, 76)

src_pts = np.column_stack([xs, ys])
dst_pts = src_pts

tform = transform.PiecewiseAffineTransform()
tform.estimate(src_pts, dst_pts)

out = transform.warp(image, tform)

import matplotlib.pyplot as plt
plt.imshow(out)
plt.show()

Best regards
Stéfan


--
You received this message because you are subscribed to the Google Groups "scikit-image" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image...@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/e51ad552-af63-49e0-afbc-2d4b5597d5e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages