MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Fast way to Rotate a bitmap in Mathematica 6.0

  • To: mathgroup at
  • Subject: [mg102172] Re: [mg102083] Fast way to Rotate a bitmap in Mathematica 6.0
  • From: Patrick Scheibe <pscheibe at>
  • Date: Fri, 31 Jul 2009 05:56:43 -0400 (EDT)
  • References: <>


a simple thing to do is to take the array of pixels, interpolate each
color-channel and then create a new array for each channel where the
pixel values at {x,y} are given by

ip[ M.{x,y}]

Here ip is an interpolating function and M is the rotation-matrix around
your desired angle:

MyImageRotate[data_List, phi_?NumericQ] := 
 Module[{nx, ny, dim, ip, ipfunc}, 
  If[Length[dim = Dimensions[data]] =!= 3, 
   Print["This version is only for color-images"];
  {ny, nx} = Most@dim;
  ip = ListInterpolation[#, {{-nx/2, nx/2}, {-ny/2, ny/2}}] & /@ 
    Transpose[data, {3, 2, 1}];
  ipfunc[x_, y_] := 
   If[x < -nx/2 || x > nx/2 || y < -ny/2 || y > ny/2, 
    Array[0 &, Last@dim], Round@Through[ip[x, y]]];
  Table[ipfunc[x Cos[phi] - y Sin[phi], 
    y Cos[phi] + x Sin[phi]], {y, -ny/2, ny/2 - 1}, {x, -nx/2, 
    nx/2 - 1}]]

img = Import["";, 
GraphicsRow[ArrayPlot[#, ColorFunction -> RGBColor] & /@ {img,
   MyImageRotate[img, 12.1231/180*2 Pi]

There are several things needing your attention, e.g. are your pixels
8-bit, then they should have this type after rotation too, Graylevel
images just have a value for each pixel and not a list of values, ...
but these are minor things.

But, as you can see while you are waiting, this version is far from
beeing *fast*. A MathLink-call to a parallel C-implementation of, for
instance "Two-pass image and volume rotation" is, to put it mildly, in
another league of speed.


On Wed, 2009-07-29 at 05:08 -0400, olliH wrote:
> Hi,
> i import a bitmap with 500x500 points with:
> Import["filename.bmp","Data"]
> With ArrayPlot i can take a look at the image:
> ArrayPlot[%]
> Now i would like to rotate the image with an angle of for example
> 12.1231 Degree...
> How are you doing this?

  • Prev by Date: Re: Re: The audience for Mathematica (Was: Show doesn't work inside Do
  • Next by Date: Re: A simple error
  • Previous by thread: Fast way to Rotate a bitmap in Mathematica 6.0
  • Next by thread: Re: Fast way to Rotate a bitmap in Mathematica 6.0