简体   繁体   English

从张量中提取矩阵

[英]Extracting matrices from tensors

So I am trying to write a function that converts RGB to HSI on Python.所以我正在尝试编写一个函数,在 Python 上将 RGB 转换为 HSI。

I have an image that is saved in np.ndarray (tensor I suppose?) with dimensions (1080, 1920, 3), that is - 1080x1920 pixels in RGB.我有一个保存在 np.ndarray(我想是张量?)中的图像,尺寸为(1080、1920、3),即 - RGB 中的 1080x1920 像素。 How can I extract matrix of R/G/B;如何提取 R/G/B 矩阵; after I get H/S/I, how do I concatenate the matrices to get back the tensor (1080, 1920, 3)?获得 H/S/I 后,如何连接矩阵以取回张量 (1080, 1920, 3)?

Assuming your_image contains the RGB channels, you can extract each channel using the corresponding index:假设your_image包含 RGB 通道,您可以使用相应的索引提取每个通道:

r = your_image[..., 0]
g = your_image[..., 1]
b = your_image[..., 2]

Note: You may need to normalize the values to the interval [0.0, 1.0].注意:您可能需要将值标准化为区间 [0.0, 1.0]。 If so, divide them by 255.0.如果是这样,将它们除以 255.0。

Conversely, you can stack the three channels together as follows:相反,您可以将三个通道堆叠在一起,如下所示:

import numpy as np
your_image = np.dstack((r, g, b))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM