Next Spaceship

Driving into future…

How to Rotate an Image

| Comments

With OpenCV, it’s easy to rotate an image by code.

This is Lena:

Now, I want to rotate this picture 30 degree like this:

Here is the code:

Rotate Lena 30 Degrees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

//Created by Leon
//http://leons.im
//20, Mar, 2011

#define PI acos(-1)
int main() {
  IplImage* img = 0;
  int height,width,step,channels;
  uchar *data, *result;
  int i,j,k,t,x,y;
  double angle = PI / 6;
  double cosa, sina;
  cosa = cos(angle);
  sina = sin(angle);

  // load an image  
  img=cvLoadImage("lena.bmp");
  if(!img){
    printf("Could not load image filen");
    exit(0);
  }

  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels  = img->nChannels;
  data      = (uchar *)img->imageData;
  result   = (uchar *)malloc(height * step);
  printf("Processing a %dx%d image with %d channelsn",height,width,channels);


  // create a window
  cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
  cvMoveWindow("mainWin", 100, 100);

  // invert the image
  for(i=0;i<height;i++) {
      for(j=0;j<width;j++) {
          x = (j - width / 2) * cosa - (i - height / 2) * sina + width / 2;
          y = (j - width / 2) * sina + (i - height / 2) * cosa + height / 2;
          for(k=0;k<channels;k++) {
              t = y * step + x * channels + k;
              if (t >= 0 && t < height * step){
                  result[i * step + j * channels + k] = data[t];
              }
          }
      }
  }

  for (i = 0; i < height * step; ++i) {
      data[i] = result[i];
  }
  // show the image
  cvShowImage("mainWin", img );

  // wait for a key
  cvWaitKey(0);

  // release the image
  cvReleaseImage(&img );
  return 0;
}

Here is my Makefile on Linux:

1
2
3
4
5
6
7
CC = g++
CFLAGS = `pkg-config --cflags opencv` `pkg-config --libs opencv`

hello : hello-world.cpp
  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig &&
  export PKG_CONFIG_PATH &&
  $(CC) $(CFLAGS) hello-world.cpp -o hello

You may change the angle to rotate to any degree you like. As you can see, the picture rotated is aliasing, so if anyone knows how to anti-aliasing, please tell me.

Comments