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

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

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[j];
}
}
---------------------------------------
:-?

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.

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.

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...
<email snipped>
....here im doing a project on that.............

commented: Pointless 4-year dredging of the bottom of the pond -6

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

commented: "me too" should try harder with a search engine first -6

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

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

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

commented: marco93 seems to be such a "one-liner", If I recall right. Let's see whether he will answer ... +6

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

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.

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.

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

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

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.

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

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

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);
}

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.