本文介绍了如何旋转图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不是特定于语言的,而且是一个数学问题。然而,我将使用一些C ++代码来解释我需要什么,因为我对数学不太热。

This question isn't language specific, and is a math problem. I will however use some C++ code to explain what I need as I'm not too hot on math.

以下是图像的组成方式:

Here's how the image is composed:

ImageMatrix image;
image[0][0][0] = 1;
image[0][1][0] = 2;
image[0][2][0] = 1;
image[1][0][0] = 0;
image[1][1][0] = 0;
image[1][2][0] = 0;
image[2][0][0] = -1;
image[2][1][0] = -2;
image[2][2][0] = -1;

这是我正在尝试创建的函数的原型:

Here's the prototype for the function I'm trying to create:

ImageMatrix rotateImage(ImageMatrix image, double angle);

我想只旋转前两个索引(行和列)而不是通道。

I'd like to rotate only the first two indices (rows and columns) but not the channel.

推荐答案

解决这个问题的通常方法是向后做。您不必计算输入图像中每个像素在输出图像中的最终位置,而是计算输出图像中每个像素在输入图像中的位置(通过在另一个方向上旋转相同的量 。这样你可以确保输出图像中的所有像素都有一个值。

The usual way to solve this is by doing it backwards. Instead of calculating where each pixel in the input image ends up in the output image, you calculate where each pixel in the output image is located in the input image (by rotationg the same amount in the other direction. This way you can be sure that all pixels in the output image will have a value.

output = new Image(input.size())

for each pixel in input:
{
  p2 = rotate(pixel, -angle);
  value = interpolate(input, p2)
  output(pixel) = value
}

有多种方法可以进行插值。对于旋转公式,我认为你应该查看

There are different ways to do interpolation. For the formula of rotation I think you should check https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

但是为了好看,这里是(点(x,y)角度/弧度的旋转):

But just to be nice, here it is (rotation of point (x,y) angle degrees/radians):

 newX = cos(angle)*x - sin(angle)*y
 newY = sin(angle)*x + cos(angle)*y

这篇关于如何旋转图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:50
查看更多