正弦/余弦图像失真

发布于 2024-09-15 16:05:49 字数 146 浏览 1 评论 0原文

是否可以使用正弦余弦等三角函数来扭曲图像,使其呈现波浪状。

如果是这样,怎么办。

PHP首选语言,但它可以是任何...

Is it possible to distort an image using trigonometric functions like sine and cosine so that it comes out wavy.

If so, how.

PHP is the preferred language but it can be any...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情稳全场 2024-09-22 16:05:49

是的,这是可能的。图像只是像素的二维数组,可以自由地重新组织它们。一种简单的方法是创建新图像并通过某种失真函数从原始图像中采样像素。

$original = read_image_pixels(); // using GD or some other way
for ($x = 0; $x < $width; $x++) {
  for ($y = 0; $y < $height; $y++) {
    // we are adding $height and taking modulo
    // to insure that $distorted_y is positive and less then $height.
    $distorted_y = ($y + round(10*sin($x/20)) + $height) % $height;

    $distorted[$x][$y] = $original[$x][$distorted_y];
  }
}

编辑:这可以进一步概括。许多熟悉的效果(例如模糊和反锐化)都是卷积滤镜。 GameDev 文章对它们进行了很好的解释。我们可以将上述正弦失真视为具有空间可变内核(系数矩阵)的卷积滤波器。

Yes it's possible. An image is just a two dimensional array of pixels and it's possible to reorganize them freely. One easy way is to create new image and sample pixels from original image thru some distortion function.

$original = read_image_pixels(); // using GD or some other way
for ($x = 0; $x < $width; $x++) {
  for ($y = 0; $y < $height; $y++) {
    // we are adding $height and taking modulo
    // to insure that $distorted_y is positive and less then $height.
    $distorted_y = ($y + round(10*sin($x/20)) + $height) % $height;

    $distorted[$x][$y] = $original[$x][$distorted_y];
  }
}

Edit: This can be generalized even further. Many familiar effects like blur and unsharpen are convolution filters. They are pretty well explained at the GameDev article. We can think the above sin-distortion as a convolution filter with spatially variable kernel (coefficient matrix).

情绪失控 2024-09-22 16:05:49

使用 phadej 的答案我得到了一个解决方案...

图片是这样的...

alt text

代码 -

<?php
    header("Content-type: image/png");
    $im = imagecreatefrompng('pic.png');
    $newim = imagecreatetruecolor(imagesx($im),imagesy($im));
    for ($x = 0; $x < imagesx($im); $x++) {
        for ($y = 0; $y < imagesy($im); $y++) {

        $rgba = imagecolorsforindex($im, imagecolorat($im, $x, $y));
        $col = imagecolorallocate($newim, $rgba["red"], $rgba["green"], $rgba["blue"]);


        $distorted_y = ($y + round(100*sin($x/50)) + imagesy($im)) % imagesy($im);
        imagesetpixel($newim, $x, $distorted_y, $col);
        }
    }

    imagepng($newim);
    ?>

输出

alt text

Using phadej's answer I got a solution...

The picture is this...

alt text

The code -

<?php
    header("Content-type: image/png");
    $im = imagecreatefrompng('pic.png');
    $newim = imagecreatetruecolor(imagesx($im),imagesy($im));
    for ($x = 0; $x < imagesx($im); $x++) {
        for ($y = 0; $y < imagesy($im); $y++) {

        $rgba = imagecolorsforindex($im, imagecolorat($im, $x, $y));
        $col = imagecolorallocate($newim, $rgba["red"], $rgba["green"], $rgba["blue"]);


        $distorted_y = ($y + round(100*sin($x/50)) + imagesy($im)) % imagesy($im);
        imagesetpixel($newim, $x, $distorted_y, $col);
        }
    }

    imagepng($newim);
    ?>

The output

alt text

蝶舞 2024-09-22 16:05:49

这很大程度上取决于你的形象如何运作。我不会说 PHP,所以这是一个通用解决方案。

如果我们可以移动单个像素,那么概念上最简单的方法就是说

yold = 像素的旧 y 位置
ynew = 像素的新 y 位置
x = 像素的 x 位置
L = 图像长度(以像素为单位)
N = 要应用的三角循环数(即正弦波数)

然后我们只需迭代图像。对于 x 的每个值,我们移动 y 像素:

ynew = yold * (1+sin(Nπx/L )) / 2

It depends a lot on how your image works. I don't speak PHP, so this is a general solution.

If we can move individual pixels, the conceptually simplest way to do this would be to say

yold = old y-position of a pixel
ynew = new y-position of the pixel
x = x-position of the pixel
L = length of the image in pixels
N = number of trigonometric cycles to apply (ie number of sin waves)

Then we just iterate through the image. For each value of x, we move the y-pixel:

ynew = yold * (1+sin(Nπx/L)) / 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文