将图像添加到一个阵列中,该数组会回馈Python中图像的数量和图像的尺寸

发布于 2025-01-22 05:38:37 字数 706 浏览 0 评论 0原文

如何将这些图像添加到(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2025-01-29 05:38:37

我们可以将图像附加到列表中,然后使用 numpy.stack

  • 从一个空列表开始:

      images_list = []
     
  • 在循环中,img = cv2.Resize,将调整大小的图像附加到列表:

      images_list.append(img)
     
  • 循环结束后,将列表转换为3D numpy数组:

      images = np.stack(images_list,axis = 0)
     

images.shape is (10,95,95)

images.shape [0]是图像的数量。
images.shape [1:]是图像尺寸(95,95)
使用图像[i]在索引i中访问图像。


代码样本:

import cv2
import os
from matplotlib import pyplot as plt
import numpy as np


# https://www.ocr2edit.com/convert-to-txt
x_train = "C:/Users/cuevas26/ae/crater_images_test"

images_list = []  # List of images - start with an empty list

# 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)
    img = cv2.resize(img_array, (95, 95))
    images_list.append(img)  # Append the new image into a list

# Convert the list to of 2D arrays into 3D NumPy array (the first index is the index of the image).
# https://stackoverflow.com/questions/27516849/how-to-convert-list-of-numpy-arrays-into-single-numpy-array
images = np.stack(images_list, axis=0)

print(type(images))  # <class 'numpy.ndarray'>
print(images.shape)  # (10, 95, 95)

n_images = images.shape[0]

# Show the images (using cv2.imshow instead of matplotlib)
for i in range(n_images):
    cv2.imshow('img', images[i])
    cv2.waitKey(1000)  # Wait 1 second

cv2.destroyAllWindows()

We may append the images to a list, and convert the final list into NumPy array using numpy.stack.

  • Start with an empty list:

     images_list = []
    
  • In the loop, after img = cv2.resize, append the resized image to the list:

     images_list.append(img)
    
  • After the end of the loop, convert the list into 3D NumPy array:

     images = np.stack(images_list, axis=0)
    

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 index i.


Code sample:

import cv2
import os
from matplotlib import pyplot as plt
import numpy as np


# https://www.ocr2edit.com/convert-to-txt
x_train = "C:/Users/cuevas26/ae/crater_images_test"

images_list = []  # List of images - start with an empty list

# 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)
    img = cv2.resize(img_array, (95, 95))
    images_list.append(img)  # Append the new image into a list

# Convert the list to of 2D arrays into 3D NumPy array (the first index is the index of the image).
# https://stackoverflow.com/questions/27516849/how-to-convert-list-of-numpy-arrays-into-single-numpy-array
images = np.stack(images_list, axis=0)

print(type(images))  # <class 'numpy.ndarray'>
print(images.shape)  # (10, 95, 95)

n_images = images.shape[0]

# Show the images (using cv2.imshow instead of matplotlib)
for i in range(n_images):
    cv2.imshow('img', images[i])
    cv2.waitKey(1000)  # Wait 1 second

cv2.destroyAllWindows()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文