使用 numpy 扩充图像 Python cv2 的数组操作

Array Operations to augment image Python cv2 using numpy

给定 X 是一个 Numpy 数组 X.shape =(1, 96, 96, 3),基本上是从 CV2 读取的图像。我正在寻找扩充操作的更简单的表述。

能否解释一下下面几行代码的作用

b=X[:, ::-1, :, :]
c=X[:, ::-1, ::-1, :]
d=X[:, :, ::-1, :]

X[::-1] 索引适用:X 的索引从第一个到最后一个 -1.

的步骤
  • b=X[:, ::-1, :, :] - 反转图像 up/down.
  • c=X[:, ::-1, ::-1, :] - 反转图像 up/down 和 left/right.
  • d=X[:, :, ::-1, :] - 反转图像 left/right.

备注:
::不是运算符,实际上是两个:运算符一个接一个。
X[::-1]X[ : : -1] 相同。
请参阅 Indexing 文档。

The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step.

If i is not given it defaults to 0

If j is not given it defaults to n

写入 [: : -1],省略 ij,并将 k 设置为 -1
语法意思是:“从索引 0 获取所有元素,步长 -1”,以 反向 顺序给出所有元素(沿此轴的所有元素) .


示例:

import cv2
import numpy as np

# Build input:
im = cv2.imread('chelsea.png')
im = cv2.resize(im, (96, 96))
X = np.empty((1, im.shape[0], im.shape[1], im.shape[2])).astype(np.uint8)
X[0, :, :, :] = im

b = X[:, ::-1, :, :]
c = X[:, ::-1, ::-1, :]
d = X[:, :, ::-1, :]

结果:

即时消息:

b:

c:

d:


注:
我有点忽略了拳头索引,因为维度是 1.
在多帧的情况下,拳头索引应用帧数是很常见的。