python - Get the second channel from a three channels image as a Numpy array
问题描述
I'm using Python 3.7.7.
I have a three channels image as a Numpy array with this shape: (200, 200, 3)
.
Because it is so huge, I have tried to guess what I have to do with this example:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(a[1]) # Output [4, 5, 6]
But I'm not sure if I'm doing right.
What do I have to do to get the second channel from the image array (output shape (200, 200, 1))?
解决方案
如果您询问使用约定,在机器学习的图像处理中,我通常会看到每个图像被展平,以便每个图像是一个长行,按行优先顺序,然后是通道顺序。Numpy 有 obj.flatten() 命令使这变得容易。然后要检索中间通道,可以使用 Numpy 切片或索引。每个处理过的批次都有很多图像(行),每个图像都是一个很长的扁平行。
例子:
b = a.flatten()
print(b)
# output array([1, 2, 3, 4, 5, 6, 7, 8, 9])
channel2 = b[3:6]
print(channel2)
# output array([4, 5, 6])
对于其他用例,可能会有不同的约定。
使用具有 3 个通道的 3x3 图像阵列的更长示例。
请注意,数值按行优先顺序排列,然后是通道顺序。
img_a = np.arange(0, 27).reshape(3, 3, 3)
''' output
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
'''
# Flatten into one long row
row_a = img_a.flatten()
# output array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
# 17, 18, 19, 20, 21, 22, 23, 24, 25, 26])
# Select middle channel using Numpy slicing
channel_mid = row_a[9:18]
# output array([ 9, 10, 11, 12, 13, 14, 15, 16, 17])
# Convert middle channel back into a matrix shape (if needed).
matrix_mid = channel_mid.reshape(3, 3)
''' output
array([[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]])
'''
推荐阅读
- java - 错误:java:包 org.springframework.boot.loader 不存在
- sql-server - 对通过 ODBC 链接的 MSSQL 使用 VBA 中的 Database.Execute 方法的最佳方式
- elasticsearch - 使用 Elasticsearch 实现搜索
- c++ - 如何强制模板参数为枚举或枚举类?
- python-3.x - 我无法在嵌套循环中打印出来。我试图打印只有 2 个阶乘但无法打印出来的数字
- c - scanf ("%s", string) 的漏洞
- excel - 删除#NULL!使用 Excel 宏
- bash - Bash - 解析ini文件,通过值查找节名
- android - Android Studio 编译器无法编译我的 AdsClass
- javascript - 如何在订阅中返回 Observable?