我有一些关于光流图像扭曲的问题。
我想通过grid_sample测试图像的变形,我从PWC-Net得到了样本(两张带flow的图像),如下所示:输入图像来自--> https://github.com/NVlabs/PWC-Net/tree/master/PyTorch/data flow文件来自--> https://github.com/NVlabs/PWC-Net/tree/master/PyTorch/tmp
import torch
import torch.nn as nn
import torch.nn.functional as F
import cv2
import numpy as np
from imageio import imread
def warp(x, flo):
“”"
warp an image/tensor (im2) back to im1, according to the optical flow
x: [B, C, H, W] (im2)
flo: [B, 2, H, W] flow
"""
B, C, H, W = x.size()
# mesh grid
xx = torch.arange(0, W).view(1 ,-1).repeat(H ,1)
yy = torch.arange(0, H).view(-1 ,1).repeat(1 ,W)
xx = xx.view(1 ,1 ,H ,W).repeat(B ,1 ,1 ,1)
yy = yy.view(1 ,1 ,H ,W).repeat(B ,1 ,1 ,1)
grid = torch.cat((xx ,yy) ,1).float()
if x.is_cuda:
grid = grid.cuda()
vgrid = Variable(grid) + flo
# scale grid to [-1,1]
vgrid[: ,0 ,: ,:] = 2.0 *vgrid[: ,0 ,: ,:].clone() / max( W -1 ,1 ) -1.0
vgrid[: ,1 ,: ,:] = 2.0 *vgrid[: ,1 ,: ,:].clone() / max( H -1 ,1 ) -1.0
vgrid = vgrid.permute(0 ,2 ,3 ,1)
flo = flo.permute(0 ,2 ,3 ,1)
output = F.grid_sample(x, vgrid)
mask = torch.autograd.Variable(torch.ones(x.size())).cuda()
mask = F.grid_sample(mask, vgrid)
mask[mask <0.9999] = 0
mask[mask >0] = 1
return output*mask
test = cv2.readOpticalFlow(‘reference_frame_0010.flo’)
test = torch.from_numpy(test)
test = test.view(1, 2, H, W)
test = Variable(test).cuda()
test_img2 = cv2.imread(‘frame_0011.png’)
test_img2 = torch.from_numpy(test_img2)
test_img2 = test_img2.view(1, 3, H, W)
test_img2 = Variable(test_img2).cuda()
out = warp(test_img2, test)
out = out.view(H, W, 3)
out = out.cpu()
out = np.float32(out)
cv2.imwrite(‘test3.png’, out*255)
我运行了上面的代码,得到了一个结果图像。正如你所看到的最后一张图片,这不是我所期望的。它看起来像有网格的图像的几个部分。用光流得到扭曲的目标图像是正确的吗?如果没有,我怎样才能使整个图像看起来像输入的彩色图像?
请帮我解决这个问题。
发布于 2020-12-14 16:33:47
当使用opencv加载图像时,它具有shape [height, width, 3]
。
当您尝试使用以下命令重塑它时
test_img2 = test_img2.view(1, 3, H, W)
它混合了所有的值,而不是仅仅交换维度。您需要的是torch.transpose
函数。
test_img2 = test_img2.transpose(2, 0, 1)[None]
在这里,[None]
添加了批处理维度。
https://stackoverflow.com/questions/60465720
复制相似问题