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)

    运行结果:

  • 相关阅读:
    移动网络介绍
    统一导航路由方案
    负载均衡汇总
    Openfire部署和配置说明
    CDN技术介绍
    流媒体
    WebSocket和HTTP的区别与联系
    zabbix 邮件报警
    Linux系统故障-Repair filesystem
    redhat 6.8 配置yum源
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/12326208.html
Copyright © 2011-2022 走看看