【发布时间】:2019-05-11 09:35:13
【问题描述】:
我正在解决一个问题,我有坐标可以像
一样对图像进行切片要裁剪的区域的X坐标、Y坐标、高度、宽度
如果我有使用获得的手电筒图像
img = Variable(img.cuda())
我们如何分割这张图片以获得图片的特定区域 [y:y+height, x:x+width] 。 谢谢
【问题讨论】:
标签: deep-learning pytorch vision
我正在解决一个问题,我有坐标可以像
一样对图像进行切片要裁剪的区域的X坐标、Y坐标、高度、宽度
如果我有使用获得的手电筒图像
img = Variable(img.cuda())
我们如何分割这张图片以获得图片的特定区域 [y:y+height, x:x+width] 。 谢谢
【问题讨论】:
标签: deep-learning pytorch vision
如果我正确理解了你的问题,那么你可以像在 numpy 中那样做。
这是一个简短的例子:
import torch
t = torch.rand(5, 5)
# original matrix
print(t)
h = 2
w = 2
x = 1
y = 1
# cropped out matrix
print(t[x:x+h, y:y+w])
输出:
tensor([[ 0.5402, 0.4106, 0.9904, 0.9556, 0.2217],
[ 0.4533, 0.6300, 0.5352, 0.2710, 0.4307],
[ 0.6389, 0.5660, 0.1582, 0.5701, 0.1614],
[ 0.1717, 0.4071, 0.4960, 0.2127, 0.5587],
[ 0.9529, 0.2865, 0.6667, 0.7401, 0.3372]])
tensor([[ 0.6300, 0.5352],
[ 0.5660, 0.1582]])
如您所见,从t 中裁剪出一个 2x2 矩阵。
【讨论】:
[batch, feature, width, height],因此您想要切片 t[:, :, width, height] 或 t[..., width, height](等效符号)。跨度>
我得到了使用这个符号的解决方案
img[:, :, y:y+height, x:x+width]
所以输出将是一个调整大小的火炬图像。谢谢
【讨论】: