ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python:敲代码做笔记

2021-04-24 12:57:12  阅读:279  来源: 互联网

标签:img Python 代码 transpose three 笔记 数组 图像 np


1. np.transpose()

函数原型

transpose(a, axes=None)

参数

  • a:输入数组
  • axes:可选的一组list,根据给定的list调换数组各位置的值(我也不知道怎么表述,直接看下面的例子吧),默认将数组各维度反转(矩阵转置)

返回值ndarray类型,变换后的数组视图

示例1:一维数组

import numpy as np
t = np.arange(4)
print(t)
print(t.transpose())

输出:

[0, 1, 2, 3]
[0, 1, 2, 3]

可以看出这个方法对一维数组不管用

示例2:二维数组

two = np.arange(16).reshape(4, 4)
print(two)
print(two.transpose())

输出:

[[0, 1, 2, 3],
 [4, 5, 6, 7],
 [8, 9, 10, 11],
 [12, 13, 14, 15]]
[[0, 4, 8, 12],
 [1, 5, 9, 13],
 [2, 6, 10, 11],
 [3, 7, 11, 15]]

对二维数组的操作默认就是矩阵转置;

示例3:三维数组

three = np.arange(18).reshape(2, 3, 3)
print(three)
print(three.transpose())

输出:

[[[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]],
 [[9, 10, 11],
  [12, 13, 14],
  [15, 16, 17]]]

[[[0, 9],
  [3, 12],
  [6, 15]],
 [[1, 10],
  [4, 13],
  [7, 16]]

详解:
数组three的原始axes(0, 1, 2),那么transpose()默认的参数axes为:(2, 1, 0)
先看原始数组各值对应的索引:

three[0][0][0]=0; three[0][0][1]=1; three[0][0][2]=2;
three[0][1][0]=3; three[0][1][1]=4; three[0][1][2]=5;
three[0][2][0]=6; three[0][2][1]=7; three[0][2][2]=8;
three[1][0][0]=9; three[1][0][1]=10; three[1][0][2]=11;
three[1][1][0]=12; three[1][1][1]=13; three[1][1][2]=14;
three[1][2][0]=15; three[1][2][1]=16; three[1][2][2]=17;

再看经过transpose()后的数组x,主要操作就是将各个元素的对应下标换一下

three[0][0][0]=0; three[1][0][0]=1; three[2][0][0]=2;
three[0][1][0]=3; three[1][1][0]=4; three[2][1][0]=5;
three[0][2][0]=6; three[1][2][0]=7; three[2][2][0]=8;
three[0][0][1]=9; three[1][0][1]=10; three[2][0][1]=11;
three[0][1][1]=12; three[1][1][1]=13; three[2][1][1]=14;
three[0][2][1]=15; three[1][2][1]=16; three[2][2][1]=17;

看一下对图像的操作:

import cv2 as cv
import numpy as np

img = cv.imread("C:\\Users\\mac\\Pictures\\0.jpg")
cv.imshow('img', img)

img = np.transpose(img, (1, 0, 2))

cv.namedWindow('img', cv.WINDOW_AUTOSIZE)
cv.imshow('img2', img)
cv.waitKey(0)
cv.destroyAllWindows()

原始图像:
在这里插入图片描述变换后的图像:
在这里插入图片描述

参考链接:

  1. np.transpose的作用及在图像操作中的使用
  2. np.transpose

2. torchvision.utils.make_grid()

用于把几个图像按照网格排列的方式绘制出来。
函数原型:

make_grid(
    tensor: Union[torch.Tensor, List[torch.Tensor]],
    nrow: int = 8,
    padding: int = 2,
    normalize: bool = False,
    range: Optional[Tuple[int, int]] = None,
    scale_each: bool = False,
    pad_value: int = 0,
)

参数:

  • tensor4D张量,形状为(BxCxHxW),分别表示样本数,通道数,图像高度,图像宽度。或者是一个图像列表
  • nrow:每行的图片数量,默认为8
  • padding:相邻图像之间的间隔,默认为2
  • normalize:如果为True,则把图像的像素值通过range指定的最大值和最小值归一化到0-1;默认为False
  • scale_each:如果为True,就单独对每张图像进行normalize;如果是False,统一对所有图像进行normalize;默认为False
  • pad_valuefloat,图像之间的空隙,默认为0

示例1

from torchvision.utils import make_grid
import numpy as np
import torch
import matplotlib.pyplot as plt

def show(img):
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)), interpolation='nearest')

images = torch.FloatTensor(100 * np.random.normal(0, 1, (25, 1, 28, 28)))

show(make_grid(images, nrow=5, padding=10, pad_value=0))
plt.show()

显示结果:
在这里插入图片描述### 参考链接:

  1. Pytorch save_image和make_grid函数详解

标签:img,Python,代码,transpose,three,笔记,数组,图像,np
来源: https://blog.csdn.net/qq_45465526/article/details/116085544

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有