【问题标题】:How do you add a swirl to an image (image distortion)?如何为图像添加漩涡(图像失真)?
【发布时间】:2016-09-16 01:53:34
【问题描述】:

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

【问题讨论】:

    标签: image jes


    【解决方案1】:

    假设您使用 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));
        }
    }
    

    【讨论】:

    • 什么是arc2?这是在 JES API 中吗?
    • @Synesso arc2 是反正切函数,它接受 2 个参数 y 和 x 并返回一个角度。它在 Jython 数学 API 中:jython.org/javadoc/org/python/modules/math.html 上面的代码是伪代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多