zoukankan      html  css  js  c++  java
  • tensor2im

    def tensor2im(image_tensor, imtype=np.uint8, normalize=True):
        if isinstance(image_tensor, list):
            image_numpy = []
            for i in range(len(image_tensor)):
                image_numpy.append(tensor2im(image_tensor[i], imtype, normalize))
            return image_numpy
        image_numpy = image_tensor.cpu().float().numpy()
        if normalize:
            image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0
        else:
            image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0      
        image_numpy = np.clip(image_numpy, 0, 255)
        if image_numpy.shape[2] == 1 or image_numpy.shape[2] > 3:        
            image_numpy = image_numpy[:,:,0]
        return image_numpy.astype(imtype)
    

    image_tensor里面的数值为(-1,1),现在需要将图像换成图像格式(0,255)

    1、np.transpose()

    代码:

    import numpy as np
    
    a = np.array(range(30)).reshape(2, 3, 5)
    b = a.transpose(1, 0, 2)
    print ("a = ")
    print (a)
    print (a.shape)
    print ("===================== \n")
     
    print ("a.transpose() = ")
    print (b)
    print (b.shape)
    

    运行结果:

     2、np.clip()

    代码:

    import numpy as np
    x=np.array([1,2,3,5,6,7,8,9])
    y=np.clip(x,3,8)
    print (y)

    运行结果:

  • 相关阅读:
    java 接口中的成员变量与方法
    Spring BeanPostProcessor
    MySQL更改命令行默认分隔符
    java 动态代理
    《剑指offer》:[62]序列化二叉树
    group by语法
    Mysql两种引擎
    线程池大小设置
    Synchronized及其实现原理
    CAS ABA问题
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/12326208.html
Copyright © 2011-2022 走看看