如何将空间尺寸更改为颜色尺寸?Pytorch中的光流使图像变形

问题描述

我有一些图像因光流而变形的问题。

import torch

from torch.autograd import Variable

import cv2

import numpy as np

import torch.nn as nn

from PIL import Image

import matplotlib.image as img

import matplotlib.pyplot as pp

    
    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,W] flow
        """
        B,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,W).repeat(B,1)
        yy = yy.view(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[:,:,:] = 2.0*vgrid[:,:].clone() / max(W-1,1)-1.0
        vgrid[:,:].clone() / max(H-1,1)-1.0
    
        vgrid = vgrid.permute(0,3,1)
        output = nn.functional.grid_sample(x.float(),vgrid)
        mask = torch.autograd.Variable(torch.ones(x.size())).cuda()
        mask = nn.functional.grid_sample(mask,vgrid)
    
        # if W==128:
            # np.save('mask.npy',mask.cpu().data.numpy())
            # np.save('warp.npy',output.cpu().data.numpy())
    
        mask[mask<0.9999] = 0
        mask[mask>0] = 1
    
    
        return output*mask
    
    optical_path = '.../000000.flo'
    test = cv2.readOpticalFlow(optical_path)
    
    test = torch.from_numpy(test)
    print(test.size())
    H,W,C = test.size()
    test = test.view(H,1)
    # test = test.view(1,W)
    
    test = test.permute(3,1)
    test = Variable(test).cuda()
    
    img_path='.../test.png'
    # test_img2 = cv2.imread(img_path)
    im = img.imread(img_path)
    im = np.array(im)
    test_img2 = torch.from_numpy(im)
    
    print(test_img2.size())
    H,C = test_img2.size()
    test_img2 = test_img2.view(1,W)
    # test_img2 = test_img2.view(H,1)
    # test_img2 = test_img2.permute(3,1)
    
    print(test_img2.size())
    test_img2 = Variable(test_img2).cuda()
    
    
    
    out = warp(test_img2,test)
    out = out.view(H,3)
    out = out.cpu()
    out = np.uint8(out)
    
    img_name='test3.png'
    
    image = Image.fromarray(out*255)
    image.save('.../test3.png')
    # cv2.imshow(out*255)

wrong result image

我已经运行了上面的代码并得到了一张图片

图像为无色,相隔9个空格。

那不是我期望的结果,我想要一张彩色的图像。

请帮我解决这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)