Ask Your Question
0

Rotation of an image with this algorithm

asked 2012-10-10 10:09:40 -0600

simonsk gravatar image

updated 2012-10-10 10:16:18 -0600

I have to do a rotation of an image with this algorithm:

we need to define the amount of rotation in terms of an angle. We denote this angle θ meaning that each pixel in f (x, y) is rotated θ degrees. The transformation is defined as

image description 

I tried to go through some of the core functionality tutorials, but I'm already lost at the 3rd chapter.

I don't know how to do this with c++ and opencv, and I really hope that someone in here can help me.

I don't even know how to access one pixel in the matrix and tell it to do - well just something..

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2012-10-10 10:44:05 -0600

Kirill Kornyakov gravatar image

You have to use warpAffine function. Just grep OpenCV sources (including samples), and you'll find a couple of examples.

And here is detailed tutorial on subject.

edit flag offensive delete link more

Comments

The thing is that, I am not allowed to use those kind of "built-in" functions. It's a school project, and I have to use this exact algorithm. :(

simonsk gravatar imagesimonsk ( 2012-10-12 04:38:32 -0600 )edit
0

answered 2013-06-04 03:13:42 -0600

Ben gravatar image

updated 2013-06-04 03:22:36 -0600

You need to inverse your rotation matrix in order to get a source pixel for every pixel in your destination image. It should not be too hard to find a matrix H with [x,y] = H*[x',y'] (hint: it looks very similar to your rotation matrix above).

Then you iterate over every pixel in your destination image and compute the corresponding source pixel coordinates with H. If the source pixel coordinates are out of bounds, you define some default color like white or black. Otherwise you take the source pixel's color and set it to you destination image. You usually won't get integer values for your coordinates. In case of a rotation the result should be ok if you just round it to the next integer, because you don't have distortions. But the results are nicer if you use an interpolation method (e.g. linear), which means that your destination pixel color is a result of multiple source pixel colors that are merged.

You can access image pixels for a cv::Mat img with Vec3b rgbVal = img.at<Vec3b>(y,x) for color images and uchar grayVal = img.at<uchar>(y,x) for grayscale images.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-10-10 10:09:40 -0600

Seen: 3,103 times

Last updated: Jun 04 '13