简体   繁体   English

如何在 python 中将 2D 图像重塑为 3D

[英]How to reshape 2D image into 3D in python

I have the following code:我有以下代码:

import numpy as np
import os
import cv2

mask_red = np.load('C:/Users/kjbaili/.spyder-py3/GMM/gmm_color_object_detection- 
master/roipoly_annotate/Neuer Ordner/2.2.npy')

img = cv2.imread('C:/Users/kjbaili/.spyder-py3/GMM/gmm_color_object_detection- 
master/roipoly_annotate/Neuer Ordner/2.2.png')

pixel_red = img[mask_red]

print(pixel_red.shape)

cv2.imshow('red_pixel',pixel_red)

cv2.waitKey(0)

img has shape of (900x1200x3) mask_red is an (900x1200) array. img 的形状为 (900x1200x3) mask_red 是一个 (900x1200) 数组。

pixel_red is the extracted roi containing only red pixels ans has the shape (51605,3) What I'm trying to do is to view the pixel_red as an image using imshow and therefore it needs to be reshaped to (height,width,channels). pixel_red 是仅包含红色像素的提取 roi,形状为 (51605,3) 我要做的是使用 imshow 将 pixel_red 视为图像,因此需要将其重新调整为(高度、宽度、通道) .

Please note when i view pixel_red as it is i get an uninterpretable rectangular line which makes sense due to the actuall shape of the red_pixel请注意,当我按原样查看 pixel_red 时,我得到一条无法解释的矩形线,由于 red_pixel 的实际形状,这是有道理的

So anyone know how to view the image correctly所以任何人都知道如何正确查看图像

Thanks in advance提前致谢

To display red channel of img use this (imread load in BGR format thats why 2)要显示 img 的红色通道,请使用此(以 BGR 格式加载 imread,这就是为什么 2)

   pixel_red = img[:,:,2] 

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

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