Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

computer vision - How can we rotate an RGB image using nearest neighbor interpolation algorithm about a custom pivot point?

I am trying to understand image interpolation algorithms in computer vision. I realize that there are a ton of interpolation techniques like linear, bicubic, nearest neighbor, etc. for image rotation. It seems that nearest neighbor technique is the simplest algorithm in this area.. I understand the basic concepts like when we rotate an image with a rotation matrix, the new image rows and columns go to floating point values because of cosine and sine operations. Thus we have to truncate the floating point values and do interpolations to predict data at missing image coordinates... I am aware of three posts which are very relevant to this question : Post 1; Post 2 and Post 3

In all of these posts, they do not explain how can we rotate an image about a custom pivot point (could be center of image or any other point which is offset from the real image center). Also most of the answers in the above posts just throw some code without much explanation about how the nearest neighbor technique is implemented for an image rotation problem... Can someone explain how to rotate an RGB image (like the image shown below) using nearest neighbor about a custom pivot point (red mark shown in the image below) ?

Lena Sample Image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A simple rotation is always about the origin. A simple rotation (in 2D) is given by the following transformation matrix (I'm using homogenous coordinates here):

    ? r1 -r2 0 ?
R = ? r2  r1 0 ?
    ? 0   0  1 ?

r1 and r2 are related in that together they form a unit vector (r1^2 + r2^2 = 1). When putting coordinates through that transformation, they are rotated about the origin. For example, given a vector p, we rotate it by left-multiplying it by R.

If you want to rotate around another point, say (c1, c2), you need to translate the coordinates such that this new point moves to the origin, then apply the rotation, then translate back:

         ? 1 0 c1 ?  ? r1 -r2 0 ?  ? 1 0 -c1 ?
T' R T = ? 0 1 c2 ?  ? r2  r1 0 ?  ? 0 1 -c2 ?
         ? 0 0 1  ?  ? 0   0  1 ?  ? 0 0  1  ?

Multiplying this out gives:

         ? r1 -r2 -r1*c1+r2*c2+c1 ?   ? 1 0 -r1*c1+r2*c2+c1 ?  ? r1 -r2 0 ?
T' R T = ? r2  r1 -r2*c1-r1*c2+c2 ? = ? 0 1 -r2*c1-r1*c2+c2 ?  ? r2  r1 0 ?
         ? 0   0   1              ?   ? 0 0  1              ?  ? 0   0  1 ?

So, we can see that we can instead simply rotate around the origin, and then translate the result in some appropriate way to get the same result as if we were rotating around our chosen center of rotation.

Given any image processing library function that rotates the image and gives the full result (i.e. its output image contains all input data), we can recreate the result of rotating around an arbitrary point by cutting this result to the input size with the appropriate offset.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...