用双线性插值在Pytorch中移动图像
假设我有一个大小[H,W]的输入X,还有一个MU_X和MU_Y(可能是分数),代表X和y方向以移动的X和Y方向。 Pytorch中是否有任何有效的方法,而无需使用C ++将张量X移动使用双线性插值的MU_X和MU_Y单元。
更确切地说,假设我们有一个图像。 mu_x = 5且mu_y = 3,我们可能要移动图像,以使图像向右移动5像素和向下3像素,将像素从[H,W]的边界中删除,并在另一端引入了新像素的边界。边界为0。但是,使用分数MU_X和MU_Y,我们需要使用双线性插值来估计所得图像。
是否可以通过纯Pytorch Tensor操作来实施?还是我需要使用C ++。
Suppose that I have an input x of size [H,W] and also a mu_x and mu_y (which may be fractional)representing the pixels in x and y direction to shift. Is there any efficient way in pytorch without using c++ to shift the tensor x for mu_x and mu_y units with bilinear interpolation.
To be more precise, let's say we have an image. mu_x = 5 and mu_y = 3, we may want to shift the image so that the image moves rightward 5 pixels and downward 3 pixels, with the pixels out of boundary of [H,W] removed and new pixels introduced at the other end of the boundary to be 0. However, with fractional mu_x and mu_y, we need to use bilinear interpolation to estimate the resulting image.
Is it possible to be implemented with pure pytorch tensor operations? Or do I need to use c++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您可以通过在原始输入上应用网格采样并使用网格来指导采样过程来实现这一目标。如果您使用所得图像等于原始图像,则使用图像的坐标网格并进行示例。但是,您可以在此网格上进行轮班,因此可以使用给定的偏移进行采样。当然,网格采样可与浮点网格一起使用,这意味着您可以将任意的非反转移动应用于图像并选择采样模式(biinear是默认值)。
可以使用
f。 grid_sampling
。给定图像张量img
,我们首先使用torch.meshgrid
。请记住,采样器使用的网格必须标准化为[-1,-1]
。因此,像素x = 0,y = 0
应映射到( - 1,-1)
,Pixelx = w,y = h = h
映射到(1,1)
,中心像素最终将在(0,0)
上左右。使用两个
[0,1]
- 正截面,然后重新映射到[ - 1,1]
:因此,结果网格的形状为
(C,H,W,W,W,W )
将是采样过程产生的输出图像的维度。由于我们不使用批处理元素,因此我们需要在
img
和grid
上解开Queize Singleton尺寸。然后,我们可以应用f.grid_sample
:之后您可以应用任意
mu_x
,mu_y
shift shift,甚至可以轻松地将其用于批处理图像和图像和转移。定义采样的方式是通过定义A 移动的网格 :其中mu_x和mu_y是像素(浮点)中的值(浮点),并希望图像在该图像上移动在水平和垂直轴。要获取采样图像,请在由
x_s
和y_s
的网格上应用F.Grid_sampling:I believe you can achieve this by applying grid sampling on your original input and using a grid to guide the sampling process. If you take a coordinate grid of your image and sample using that the resulting image will be equal to the original image. However you can apply a shift on this grid and therefore sample with the given shift. Grid sampling works with floating-point grids of course, which means you can apply an arbitrary non-round shift to your image and choose a sampling mode (bilinear is the default).
This can be implemented out of the box with
F.grid_sampling
. Given an image tensorimg
, we first construct a pixel grid of that image usingtorch.meshgrid
. Keep in mind the grid used by the sampler must be normalized to[-1, -1]
. Therefore pixelx=0,y=0
should be mapped to(-1,-1)
, pixelx=w,y=h
mapped to(1,1)
, and the center pixel will end up at around(0,0)
.Use two
torch.arange
with a[0,1]
-normalization followed by a remapping to[-1,1]
:So the resulting grid has a shape of
(c, h, w)
which will be the dimensions of the output image produced by the sampling process.Since we are not working with batched elements, we need to unsqueeze singleton dimensions on both
img
andgrid
. Then we can applyF.grid_sample
:Following this you can apply your arbitrary
mu_x
,mu_y
shift and even easily use this to batches of images and shifts. The way you would define your sampling is by defining a shifted grid:Where mu_x and mu_y are the values in pixels (floating point) with wish which the image is shifted on the horizontal and vertical axes respectively. To acquire the sampled image, apply F.grid_sampling on a grid made up of
x_s
andy_s
: