我有一个形状为(100000,20(的矩阵,由20个图像组成,每个图像的大小为(500*200(。我只需要知道如何提取这些像素,以便获得image shape of (500, 200)
?我尝试了matrix[:,:,19]
,但似乎我只是抓住了最后一个功能的值,只是这不是我的目标。此外,当我尝试用cv2_imshow(matrix[:,:,19]
显示它时,它会返回一个黑色图像。
请帮忙吗?
您只需要重塑它:
import numpy as np
# after loading the images in a numpy array called imgs_array
imgs_array = imgs_array.reshape((500,200,20))
编辑:如果您想在整形步骤后提取单个图像,只需执行single_img = imgs_array[:,:,n]
,其中n
是阵列中的第n个图像
这里是我使用整形的尝试:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 6 16:43:48 2021
@author: Pietro
https://stackoverflow.com/questions/67859613/how-can-i-extract-the-image-pixels-from-2d-numpy-array?noredirect=1#comment119943691_67859613
"""
import numpy as np
import cv2 as cv
image_mia = np.random.choice([0,255] , (100000,20))
print(image_mia.shape) # sono le dimensioni, dimensions
print(image_mia)
print('n',image_mia.size)
image_mia2 = np.copy(image_mia)
print(image_mia2.shape) # sono le dimensioni, dimensions
print(image_mia2)
print('n',image_mia2.size)
image_mia2 = image_mia2.reshape(500,200,20)
print(image_mia2.shape) # sono le dimensioni, dimensions
print(image_mia2)
print('n',image_mia2.size)
img = 0
image_list = []
while img<20:
image_list.append(image_mia2[:,:,img])
img +=1
print(image_list)
for y,i in enumerate(image_list):
print('nshape : ',i.shape,' size : ', i.size)
cv.imwrite(str(y)+'.png',i.astype(np.uint8))
print('nlenght : ',len(image_list))
在没有代码或输入图像的情况下,我试图猜测我最好的