Login  Register

Rotate an image with given angle

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Rotate an image with given angle

Veeranjaneyulu TOka
6 posts
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
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Rotate an image with given angle

Kenton Arkill
65 posts
Why not use the rotate function on the image/transform/rotate drop down and tick the box fit window to size?
Or did I miss something?
Kenton

Kenton Arkill
GB U23 Ladies'
Underwater Hockey Coach
GB Masters
Underwater Hockey Manager




On 26 Oct 2012, at 13:06, Veeranjaneyulu TOka wrote:

> 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


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Rotate an image with given angle

David M Gauntt
14 posts
In reply to this post by Veeranjaneyulu TOka
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
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Rotate an image with given angle

Veeranjaneyulu TOka
6 posts
thank you very much for the suggestion..

On Tue, Oct 30, 2012 at 7:24 PM, David M Gauntt <[hidden email]> wrote:

> 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
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Rotate an image with given angle

Veeranjaneyulu TOka
6 posts
Hi,

I am facing one more issue related to this.

source image size is width x height which is getting rendered on the
screen. On top of the source image i am pasting a clipboard image. If i
just paste clipboard image as it easily (without applying rotation), it
pastes in same position as i know (x,y) and (width, height) of clipboard
image where it should be pasted.

In case if i apply rotation, still i get the original (x,y) position only
rather than new (x,y) position which should be based on angle. But app does
not have provision to give this rotated position, due to this clipboard
image gets shifted.

Could anyone tell me how to calculate new position when i apply rotation on
clipboard image, so that it will not get shifted when it gets pasted on
original image?

Thanks,
Veeru.

On Tue, Oct 30, 2012 at 7:32 PM, Veeranjaneyulu TOka
<[hidden email]>wrote:

> thank you very much for the suggestion..
>
>
> On Tue, Oct 30, 2012 at 7:24 PM, David M Gauntt <[hidden email]> wrote:
>
>> 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
>>
>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Rotate an image with given angle

Antoinette
44 posts
In reply to this post by Veeranjaneyulu TOka
You can take a look of this .NET Image Processing SDK which is runtime royalty free for the trial version. Following is the image rotating api method and c# sample code:

public static int ApplyRotate(REImage img, int deg);

REImage reImage = REFile.OpenImageFile(fileName);

ImageProcessing.ApplyRotate(reImage, 60);

REFile.SaveImageFile(reImage, "c:/reimage.png", new PNGEncoder());