# What kind of algorithm can be used to map image coordinates to cylinder coordinates

I plan to flatten an images on curved surface(eg: jar or door knob) and I found a pretty good post which helps me a lot :)(link below). However, I still confuse on how to map image coordinate to cylindrical coordinate. Help needed on explanation. Or any other simple algorithm that I can use for mapping? Thanks in advance.

How to peel the labels from marmalade jars using Mathematica?

arcSinSeries = Normal[Series[ArcSin[α], {α, 0, 10}]]
Clear[mapping];
mapping[{x_, y_}] :=
{
c1 + c2*(arcSinSeries /. α -> (x - cx)/r) + c3*y + c4*x*y,
top + y*height + tilt1*Sqrt[Clip[r^2 - (x - cx)^2, {0.01, ∞}]] +
tilt2*y*Sqrt[Clip[r^2 - (x - cx)^2, {0.01, ∞}]]
}

• I think you may find this question helpful: QR Code in shopping cart handle – MarcoB May 25 '15 at 8:04
• @MarcoB Thanks :) That really helps. – Chong Vera May 25 '15 at 16:43

Here's how I arrived at this (admittedly, quite ugly) mapping function: The "right" function to map points from cylinder coordinates to image coordinates in a pinhole camera model is the one MarcoB linked to in his comment. In a nutshell:

• {Sin[u],Cos[u],v,1} converts from cylinder coordinates u,v to (homogeneous) 3d coordinates
• which you then multiply by the camera projection matrix
• and use Most[#]/Last[#]& to convert from homogeneous coordinates to 2d euclidean coordinates.

The projection matrix contains several degrees of freedom (for camera position, orientation, "zoom" and so on). If you know the values for these, you can use that formula to project from the cylinder to the image, or solve for $u$ and $v$ and project from image coordinates to cylinder coordinates.

In the "marmalade jars" answer, however, I don't know these degrees of freedom, and instead tried to estimate them using FindMinimum, and that's where I ran into problems:

• estimating some values (especially zoom vs. camera distance) given just the label's outline is ill-conditioned.
• and the inverse mapping from image coordinates to cylinder coordinates contains inverse trigonometric functions, and FindMinimum kept aborting because the mapping returned complex numbers.

To work around these problems, I've come up with the mapping function you posted, which is an ad hoc approximation for vertical cylinders with no perspective distortion. With the added bonus that it's free of trigonometric functions and always returns real values.

• May I know how you derive the approximation for vertical cylinder? Is it using parabolic equation? thanks. – Chong Vera May 25 '15 at 16:48
• @ChongVera: IIRC, I started with a simple 2nd order polynomial in X direction and 1st order polynomial in Y direction. Then I kept adding terms that didn't break FindMinimum and seemed to improve the result image visually. So, basically, by trial and error. – Niki Estner May 25 '15 at 18:00
• Thank you so much, look like I have found the right expert. :) Appreciate if you able to share the how do you able to get the Y direction especially 'tilt1*Sqrt[r^2 - (x - cx)^2 + tilt2*y*Sqrt[r^2 - (x - cx)^2]'. I've been thinking about this but cannot get any clue yet. – Chong Vera May 27 '15 at 16:19