954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

image rotation

hi,
i have to rotate an image. the image is stored in an image[rows][cols] array..
i am trying to rotate it using trignometric functions.
i am not sure what happens when i rotate the image,
does the address of the pixel vary or the value of the pixel vary.
and when i rotate an image by say 45 degrees, then wont it fall out of the gird?
please clear my doubts :-|

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

>>does the address of the pixel vary or the value of the pixel vary.
Depends how you see this matter: let's assume the rotation center is the center of the image. So, rotating the image means the pixel p[x][y] will have the value that was allocated to the pixel p[x1][y1] - the value of the pixel vary. Or, the value GREEN that was on the pixel p[x][y] will be, after rotation, in the pixel p[x1][y1] - the address of the pixel vary () (I assumed "the address of the pixel" is the position of a pixel with a certain value)

>>when i rotate an image by say 45 degrees, then wont it fall out of the gird
If your image is rectangular, imagine that rotating it will change it's position on the screen like you will rotate a sheet of paper on the table. How will differ these two positions?

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

i tried this code for rotating,
but for r1 and c1, i am getting negative values..
and the rotation is also not proper..
------------------------------------
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{
r1=(i*cos(angle))-(j*sin(angle));
c1=(i*sin(angle))+(j*cos(angle));
rot_image[r1][c1]=image[i][j];
}
}
---------------------------------------
:-?

dalaharp
Light Poster
31 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

I think the correct relations are more like
r1 = iorig + (i - iorig)sin(angle)
c1 = jorig + (j - jorig)cos(angle)
where iorig, jorig are th ecoordinates of the rotation center.
(but I'm not too sure - it's a long time since I did some trigonometry:(()
And, also, there is possible to obtain negative values for the new coordinates, since they are reported to the coordinates of the rotation origin.

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

If you rotate a square image about it's center, clipping will occur on all sides. The most clipping will occur when the image is rotated by a multiple of 45 degress. At a multiple of 45 degrees, any part of the image extending past the original boundries will clip, which is anything about ImageWidth*cos(PI/4) (or ImageWidth*sqrt(2)/2) along the line from the center to a corner's worth of pixels. The only way to do it and keep all of the data is to make the boundries of the image larger. The easiest way is to have a source and dest images, where dest is approximately sqrt(2) larger in each dimension for all rotations, or variations of 1.0f/cos(Angle) or 1.0f/sin(Angle) depending on the range of angles to avoid asymptotes.

glSuccinct
Newbie Poster
13 posts since Aug 2004
Reputation Points: 13
Solved Threads: 2
 

hey u can rotate an image by using c,,,just use geometric equations and u must have a good knowkedge about cordinate systems , and frame of referances ..i achived it .but the problem is that it losses quality..
there is a problem when rounding the digit .. if u have any doubt u pls mail me...

....here im doing a project on that.............

sarinsukumar
Newbie Poster
3 posts since Feb 2008
Reputation Points: 4
Solved Threads: 0
 

hi i am also doing a project on image rotation and need to know how it is done. could you please tell me how to go about it.your email address was not posted though. would be obliged if u could inform me as soon as possible.am also trying image translation and warping.any ideas???

mj_crookshanks
Newbie Poster
1 post since Feb 2009
Reputation Points: 4
Solved Threads: 0
 

If its' on Windows, just use win32 api
(1 line of code..)

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 
If its' on Windows, just use win32 api (1 line of code..)

Go ahead then, tell us how to do it in one line.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

I have a problem i wanted to rotate a picture and calculate the time taken using "C" programming language ..Its really very urgent .. Please help me out ..

Karan_111
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
I have a problem i wanted to rotate a picture and calculate the time taken using "C" programming language ..Its really very urgent .. Please help me out ..


Start a new thread, and show your current effort. It might be 'urgent' for you, but it's not for anybody else.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 
I have a problem i wanted to rotate a picture and calculate the time taken using "C" programming language ..Its really very urgent .. Please help me out ..


USE the trigonometric equation on every pixel location and find out new location then place the pixel there,, u can achieve it simply like this. take care of hight and width changes,
zero padding at line endings of the BMP, BMP data structure.

sarinsukumar
Newbie Poster
3 posts since Feb 2008
Reputation Points: 4
Solved Threads: 0
 
I have a problem i wanted to rotate a picture and calculate the time taken using "C" programming language ..Its really very urgent .. Please help me out ..

There is clear description for image rotation in the book titled "Digital Media Processing:DSP Algorithms using C", from Newnes publishers

mahana
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
There is clear description for image rotation in the book titled "Digital Media Processing:DSP Algorithms using C", from Newnes publishers


do u have a source to get it

sarinsukumar
Newbie Poster
3 posts since Feb 2008
Reputation Points: 4
Solved Threads: 0
 

use linear algebra for that check wikipedia, this is a kind of pseudo-code, fill in the blanks...

for (y = 0; y < height; y++) {
  for (x = 0; x < width; x++) {
    x1 = x0 * cos(theta) - y0 * sin(theta);
    y1 = x0 * sin(theta) + y0 * cos(theta);
    // check boundaries, use different width and height if you want
    if (x1 >= 0 && x1 < width && y1 >= 0 && y1 < height) {
      // if you are using a linear array then linear_idx = y1 * width + x1
      output[x][y] = input[x1][y1]; // or maybe output[x][y] = bilinear_interpolation(input, x1, y1);
    } 
    else {
      output[x][y] = 0; // or output[x][y] = boundary_constant;
    }
  }
}

this rotation is not centered so you may want to add to (x0, y0) the coordinates of the center where you want to rotate like x0 = x0 - xc, y0 = y0 - yc. The bilinear interpolation is a must if you don't want those nasty artifacts in the output.

arielbernal
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

The code in your else statement will segfault if your output array is only height * width. Otherwise it works, but obviously there's a lot of distortion due to a lack of resampling. I'm trying really hard to find meaningful information on resampling, but I can't.

xenonscreams
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
The code in your else statement will segfault if your output array is only height * width. Otherwise it works, but obviously there's a lot of distortion due to a lack of resampling. I'm trying really hard to find meaningful information on resampling, but I can't.

the else statement won't go out of bounds since x and y are within the width and height, so be sure you are setting in the else statement input[x][y] = 0, and not input[x1][y1] because that will segfault for sure :). This is just pseudo code ...

With regards to the resampling, use bilinear interpolation it just fine and fast, using bicubic will get you deal with splines. I'll try to post the bilinear next.

arielbernal
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Oh my bad, I read it as x1 and y1. Dummy mistake, makes more sense now.

Please do post it. I can't find anything that's easy to understand/any pseudo-code anywhere. For some reason the internet is great for finding implementations of all sorts of algorithms, but horrible for finding image processing ones :(

xenonscreams
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Here it is the non tested code for bilinear interpolation, the same as before it is kind of pseudo code, to give you an idea. One important remark is that we are not checking out of bounds inside of the bilinear interpolation, so when you calculate x1 = x + 1 or y1 = y + 1, it can go out of boundaries. I found that it is better to check that once outside but again is up to you, you can actually return input[x0][y0].

This code can be totally optimized but I would keep it simple as an example.

float bilinear_interpolation(float** input, float x, float y) 
{
  int x0 = int(x);
  int y0 = int(y);
  int x1 = x0 + 1;
  int y1 = y1 + 1; 
  
  float v0 = input[x0][y0];
  float v1 = input[x1][y0];
  float v2 = input[x0][y1];
  float v3 = input[x1][y1];

  float dx = x - float(x0);
  float dx1 = 1 - dx;
  float vx0 = v1 * dx + v0 * dx1;
  float vx1 = v3 * dx + v2 * dx1;

  float dy = y - float(y0);
  return vx1 * dy + vx0 * (1 - dy);
}
arielbernal
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Ok here it is a version with boundaries check, again it can be optimized because we are calculating the product even though the dx or dy could be 0.

float bilinear_interpolation(float** input, float x, float y, int width, int height) 
{
  int x0 = int(x);
  int y0 = int(y);
  int x1 = x0 + 1;
  int y1 = y1 + 1; 
  if (x1 >= width) {
    x1 = x0;
  } 
  if (y1 >= height) {
    y1 = y0;
  }
  
  float v0 = input[x0][y0];
  float v1 = input[x1][y0];
  float v2 = input[x0][y1];
  float v3 = input[x1][y1];

  float dx = x - float(x0);
  float dx1 = 1 - dx;
  float vx0 = v1 * dx + v0 * dx1;
  float vx1 = v3 * dx + v2 * dx1;

  float dy = y - float(y0);
  return vx1 * dy + vx0 * (1 - dy);
}
arielbernal
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You