15
$\begingroup$

I am looking to reproduce the functionality of Photoshop's "Twirl" filter, which twists an image that originally looks like this (pulled from this page):

enter image description here

Into this:

enter image description here

Ideally, I'd like it to approximate the way the Photoshop Twirl filter behaves as much as possible, or at least be flexible enough to be able to reproduce the way the filter works.

$\endgroup$
  • $\begingroup$ Do you care more about speed or quality? $\endgroup$ – Jens Jun 7 '13 at 0:57
  • $\begingroup$ Both! Although it shouldn't take tremendously long to run. Primarily I was just interested in the form of the ImageTransformation, which @cormullion nicely shows. $\endgroup$ – Guillochon Jun 7 '13 at 0:59
  • $\begingroup$ I hope another answer will be able to make the twirl start within the boundaries of the image, so that the edges of the original appear mostly unaffected. I've no idea... $\endgroup$ – cormullion Jun 7 '13 at 7:54
  • $\begingroup$ That would be great, but your answer is definitely pretty close. $\endgroup$ – Guillochon Jun 7 '13 at 22:42
20
$\begingroup$

A slightly better one:

f[x_, y_] := With[
   {r = N@Sqrt[(x )^2 + (y)^2], a = ArcTan[y, x ]},
   { 0.6 r (Sin[(a + 12  r)]), 0.6 r (Cos[(a + 12 r)])}];
ImageTransformation[i, f[#[[1]], #[[2]]] &, 350, 
 DataRange -> {{-1, 1}, {-1, 1}}, Padding -> None]

enter image description here

$\endgroup$
  • $\begingroup$ +1 Nicely done. You could define f[{x_,y_}]:= and then just use f in the ImageTransformation. $\endgroup$ – Simon Woods Jun 6 '13 at 21:07
  • $\begingroup$ @simon thanks! This was some old code from last year - I've learnt a bit more since then... $\endgroup$ – cormullion Jun 6 '13 at 21:17
14
$\begingroup$

Upsampling prior to the transformation, followed by downsampling after gives you a higher quality image. This requires more computational expense.

Based on @cormullion's excellent work

i = Import["http://i.stack.imgur.com/Di28d.png"]

f[x_, y_] := 
  With[{r = N@Sqrt[(x)^2 + (y)^2], 
    a = ArcTan[y, x]}, {0.6 r (Sin[(a + 12 r)]), 
    0.6 r (Cos[(a + 12 r)])}];

ImageResize[
 ImageTransformation[ImageResize[i, 1000, Resampling -> "Gaussian"], 
  f[#[[1]], #[[2]]] &, 1000, DataRange -> {{-1, 1}, {-1, 1}}, 
  Padding -> None], 500, Resampling -> "Gaussian"]

enter image description here

$\endgroup$
  • $\begingroup$ Definitely a lot nicer looking. $\endgroup$ – Guillochon Jun 7 '13 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.