如何为图像添加漩涡(图像失真)?

时间:2015-05-26 01:22:35

标签: image jes

我试图弄清楚如何在照片中制作一个漩涡,试着到处寻找你对像素的确切做法。我正和一位朋友谈话,我们谈到了使用正弦函数来重定向像素?

1 个答案:

答案 0 :(得分:2)

假设您使用4个参数定义漩涡:

  • 漩涡中心的X和Y坐标
  • 以像素为单位的旋转半径
  • 曲折次数

从源图像开始,创建应用了漩涡的目标图像。对于每个像素(在目标图像中),您需要根据漩涡调整像素坐标,然后从源图像中读取像素。要应用漩涡,请计算出像素与旋涡中心的距离及其角度。然后根据扭曲次数调整角度,该扭曲次数逐渐淡出中心,直到达到旋涡半径时达到零。使用新角度计算要读取的调整后的像素坐标。在伪代码中,它是这样的:

Image src, dest
float swirlX, swirlY, swirlRadius, swirlTwists
for(int y = 0; y < dest.height; y++)
{
    for(int x = 0; x < dest.width; x++)
    {
        // compute the distance and angle from the swirl center:
        float pixelX = (float)x - swirlX;
        float pixelY = (float)y - swirlY;
        float pixelDistance = sqrt((pixelX * pixelX) + (pixelY * pixelY));
        float pixelAngle = arc2(pixelY, pixelX);

        // work out how much of a swirl to apply (1.0 in the center fading out to 0.0 at the radius):
        float swirlAmount = 1.0f - (pixelDistance / swirlRadius);
        if(swirlAmount > 0.0f)
        {
            float twistAngle = swirlTwists * swirlAmount * PI * 2.0;

            // adjust the pixel angle and compute the adjusted pixel co-ordinates:
            pixelAngle += twistAngle;
            pixelX = cos(pixelAngle) * pixelDistance;
            pixelY = sin(pixelAngle) * pixelDistance;
        }
        // read and write the pixel
        dest.setPixel(x, y, src.getPixel(swirlX + pixelX, swirlY + pixelY));
    }
}
相关问题