Re: Fast way to Rotate a bitmap in Mathematica 6.0
- To: mathgroup at smc.vnet.net
- Subject: [mg102172] Re: [mg102083] Fast way to Rotate a bitmap in Mathematica 6.0
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Fri, 31 Jul 2009 05:56:43 -0400 (EDT)
- References: <200907290908.FAA19489@smc.vnet.net>
Hi,
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"];
Return[$Failed]];
{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["http://sipi.usc.edu/database/misc/4.2.04.tiff",
"Data"];
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.
Cheers
Patrick
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?
>
- References:
- Fast way to Rotate a bitmap in Mathematica 6.0
- From: olliH <oliver.hofherr@googlemail.com>
- Fast way to Rotate a bitmap in Mathematica 6.0