将图像添加到一个阵列中,该数组会回馈Python中图像的数量和图像的尺寸
如何将这些图像添加到(95,95)阵列中的这些图像中,使我拥有(在我的情况10)和这些图像的尺寸(95,95)的阵列中? 我所需的输出将是一个阵列< 10,95,95>。
到目前为止,这是我的代码,谢谢! 代码:
import cv2
import os
from matplotlib import pyplot as plt
# https://www.ocr2edit.com/convert-to-txt
x_train = "C:/Users/cuevas26/ae/crater_images_test"
categories = ["crater"]
#for category in categories:
path = x_train
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
imgs = cv2.resize(img_array, (95, 95))
plt.imshow(imgs, cmap="gray")
plt.show()
print(type(imgs))
print(imgs.shape)
How can I add these images which I have converted to a (95,95) array into an array that gives me the number of images I have (in my case 10) and the dimensions of those images (95,95)?
My desired output would be an array <10,95,95>.
This is my code so far, thank you! code:
import cv2
import os
from matplotlib import pyplot as plt
# https://www.ocr2edit.com/convert-to-txt
x_train = "C:/Users/cuevas26/ae/crater_images_test"
categories = ["crater"]
#for category in categories:
path = x_train
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
imgs = cv2.resize(img_array, (95, 95))
plt.imshow(imgs, cmap="gray")
plt.show()
print(type(imgs))
print(imgs.shape)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以将图像附加到列表中,然后使用 numpy.stack 。
从一个空列表开始:
在循环中,
img = cv2.Resize
,将调整大小的图像附加到列表:循环结束后,将列表转换为3D numpy数组:
images.shape
is(10,95,95)
。。
images.shape [0]
是图像的数量。images.shape [1:]
是图像尺寸(95,95)
。使用
图像[i]
在索引i
中访问图像。代码样本:
We may append the images to a list, and convert the final list into NumPy array using numpy.stack.
Start with an empty list:
In the loop, after
img = cv2.resize
, append the resized image to the list:After the end of the loop, convert the list into 3D NumPy array:
images.shape
is(10, 95, 95)
.images.shape[0]
is the number of images.images.shape[1:]
is the image dimensions(95, 95)
.Use
images[i]
for accessing the image in indexi
.Code sample: