Login  Register

Re: Rotate an image with given angle

Posted by David M Gauntt on Oct 30, 2012; 1:54pm
URL: http://imagej.273.s1.nabble.com/Rotate-an-image-with-given-angle-tp5000544p5000588.html

In the code you wrote, the calculation of xs and ys are equivalent to

xs=(x-centerX)*ca-(y-centerY)*sa+centerX;
ys=(x-centerX)*sa+(y-centerY)*ca+centerY;

which is exactly what you should use to rotate about the center if the
source and destination image sizes were the same.  In your case, they are
not, and they should be calculated as

xs=(x-dCenterX)*ca-(y-dCenterY)*sa+sCenterX;
ys=(x-dCenterX)*sa+(y-dCenterY)*ca+sCenterY;

You code is written to minimize the number of multiplications, but at the
cost of complicating the logic, which may be why you didn't catch this.


--
David M. Gauntt, Ph.D.
Associate Professor,
Division of Medical Physics and Engineering
UAB Department of Radiology

mailto:[hidden email]
205-975-3777







On 10/26/12 11:00 PM, "IMAGEJ automatic digest system"
<[hidden email]> wrote:

>Date:    Fri, 26 Oct 2012 17:36:20 +0530
>From:    Veeranjaneyulu TOka <[hidden email]>
>Subject: Rotate an image with given angle
>
>Hi All,
>
>I am trying to rotate an image with given angle but  really could not
>manage to work this properly... Below is the piece of code i have ported
>from ImageJ to rotate arbitrarly.
>
>for example here
>
>destWidth = 464 and destHeight = 464
>
>source size is uWidth = 405 and uHeight = 227.
>
>
>  double centerX, centerY, angleRadians, ca, sa, tmp1, tmp2, tmp3, tmp4,
>xs, ys, dwidth, dheight;
>  int index, x, y;
>  centerX = destWidth /2.0;
>  centerY = destHeight / 2.0;
>
>  angleRadians = -angle/(180.0/3.14159265358979323846);
>  ca = cos(angleRadians);
>  sa = sin(angleRadians);
>  tmp1 = centerY*sa-centerX*ca;
>  tmp2 = -centerX*sa-centerY*ca;
>  dwidth = uWidth, dheight= uHeight;
>
>  for (y=0; y<destHeight; y++)
>  {
>   index = y*destWidth*4;
>   tmp3 = tmp1 - y*sa + centerX;
>   tmp4 = tmp2 + y*ca + centerY;
>   for (x=0; x<destHeight; x++)
>   {
>    xs = x*ca + tmp3;
>    ys = x*sa + tmp4;
>    if ((xs>=-0.01) && (xs<dwidth) && (ys>=-0.01) && (ys<dheight))
>    {
>     pbOut[index++] = pbInput[uWidth*(int)ys*4+(int)xs*4 + 0];
>     pbOut[index++] = pbInput[uWidth*(int)ys*4+(int)xs*4 + 1];
>     pbOut[index++] = pbInput[uWidth*(int)ys*4+(int)xs*4 + 2];
>     pbOut[index++] = pbInput[uWidth*(int)ys*4+(int)xs*4 + 3];
>    }
>    else
>    {
>     pbOut[index++] = 0xff; pbOut[index++] = 0xff; pbOut[index++] = 0xff;
>pbOut[index++] = 0xff;
>    }
>   }
>
>With the above code, the output that i get is cropping of top-right,
>right-bottom corners. I think something problem with my output buffer
>filling procedure though am not sure.
>
>Could you plz help me to figure the issue plz? I really do not have clue
>and need your help on this.
>
>
>thanks,
>Veeru.
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html