正弦/余弦图像失真
是否可以使用正弦和余弦等三角函数来扭曲图像,使其呈现波浪状。
如果是这样,怎么办。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是可能的。图像只是像素的二维数组,可以自由地重新组织它们。一种简单的方法是创建新图像并通过某种失真函数从原始图像中采样像素。
编辑:这可以进一步概括。许多熟悉的效果(例如模糊和反锐化)都是卷积滤镜。 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.
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).
使用 phadej 的答案我得到了一个解决方案...
图片是这样的...
代码 -
输出
Using phadej's answer I got a solution...
The picture is this...
The code -
The output
这很大程度上取决于你的形象如何运作。我不会说 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