如何以亚像素精度翻译图像?
我有一个需要在屏幕上移动图像的系统。我目前正在使用 png 并将其放置在所需的屏幕坐标处。
由于屏幕分辨率和所需帧速率的结合,某些帧是相同的,因为图像尚未移动完整像素。不幸的是,屏幕的分辨率是没有商量余地的。
我对子像素渲染如何平滑边缘有一个大致的了解,但我一直无法找到资源(如果存在)来了解如何使用着色将图像平移小于一个像素。
理想情况下,这适用于任何图像,但如果仅适用于圆形或环形等简单形状,那也是可以接受的。
I have a system that requires moving an image on the screen. I am currently using a png and just placing it at the desired screen coordinates.
Because of a combination of the screen resolution and the required frame rate, some frames are identical because the image has not yet moved a full pixel. Unfortunately, the resolution of the screen is not negotiable.
I have a general understanding of how sub-pixel rendering works to smooth out edges but I have been unable to find a resource (if it exists) as to how I can use shading to translate an image by less than a single pixel.
Ideally, this would be usable with any image but if it was only possible with a simple shape like a circle or a ring, that would also be acceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子像素插值相对简单。通常,您应用相当于具有恒定相移的全通滤波器,其中相移对应于所需的子像素图像移位。根据所需的图像质量,您可以使用例如 5 点 Lanczos 或其他窗口 sinc 函数,然后将其应用于一个或两个轴,具体取决于您是否需要 X 偏移或 Y 偏移或两者。
例如,对于 0.5 像素移位,系数可能为
[ 0.06645, 0.18965, 0.27713, 0.27713, 0.18965 ]
。 (请注意,系数已标准化,即它们的总和等于 1.0。)要生成水平移位,您可以将这些系数与从
x - 2
到x + 2
的像素进行卷积代码>,例如Sub-pixel interpolation is relatively simple. Typically you apply what amounts to an all-pass filter with a constant phase shift, where the phase shift corresponds to the required sub-pixel image shift. Depending on the required image quality you might use e.g. a 5 point Lanczos or other windowed sinc function and then apply this in one or both axes depending on whether you want an X shift or a Y shift or both.
E.g. for a 0.5 pixel shift the coefficients might be
[ 0.06645, 0.18965, 0.27713, 0.27713, 0.18965 ]
. (Note that the coefficients are normalised, i.e. their sum is equal to 1.0.)To generate a horizontal shift you would convolve these coefficients with the pixels from
x - 2
tox + 2
, e.g.从概念上讲,操作非常简单。首先,放大图像(根据需要使用任何插值方法),然后平移结果,最后进行二次采样到原始图像大小。
比例因子取决于您想要执行的亚像素平移的精度。如果要平移 0.5 度,则需要将原始图像放大 2 倍,然后将结果图像平移 1 像素;如果要平移 0.25 度,则需要放大 4 倍,依此类推。
请注意,此实现效率不高,因为当您放大时,您最终会计算实际上不会使用的像素值,因为当您将子采样回原始图像大小时,它们会被丢弃。 Paul 的回答中的实现效率更高。
Conceptually, the operation is very simple. First you scale up the image (using any method of interpolation, as you like), then you translate the result, and finally you subsample down to the original image size.
The scale factor depends on the precision of sub-pixel translation you want to do. If you want to translate by 0.5 degrees, you need scale up the original image by a factor of 2 then you translate the resulting image by 1 pixel; if you want to translate by 0.25 degrees, you need to scale up by a factor of 4, and so on.
Note that this implementation is not efficient because when you scale up you end up calculating pixel values that you won't actually use because they're just dropped when you subsample back to the original image size. The implementation in Paul's answer is more efficient.