将图像保存为numpy数组-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

将图像保存为numpy数组

2019-01-22 15:01:58 2878 1

我无法将图像加载到numpy数组并得到这样的错误...

ValueError:无法将形状(175,217,3)的输入数组广播为形状(100,100,3)

功能代码:

import cv2
import numpy as np
import os

train_data_dir = '/home/ec2-user/SageMaker/malaria-detection-model/malaria/training'
valid_data_dir = '/home/ec2-user/SageMaker/malaria-detection-model/malaria/validation'

# declare the number of samples in each category
nb_train_samples = 22045 # training samples
nb_valid_samples = 5513# validation samples
num_classes = 2
img_rows_orig = 100
img_cols_orig = 100

def load_training_data():

labels = os.listdir(train_data_dir)
total = len(labels)

X_train = np.ndarray((nb_train_samples, img_rows_orig, img_cols_orig, 3), dtype=np.uint8)
Y_train = np.zeros((nb_train_samples,), dtype='uint8')

i = 0
j = 0
for label in labels:
    image_names_train = os.listdir(os.path.join(train_data_dir, label))
    total = len(image_names_train)
    print(label, total)
    for image_name in image_names_train:
        img = cv2.imread(os.path.join(train_data_dir, label, image_name), cv2.IMREAD_COLOR)
        img = np.array([img])
        X_train[i] = img
        Y_train[i] = j

        if i % 100 == 0:
            print('Done: {0}/{1} images'.format(i, total))
        i += 1
    j += 1    
print(i)                
print('Loading done.')

np.save('imgs_train.npy', X_train, Y_train)
return X_train, Y_train

此函数是文件load_data.py的一部分,可以在以下位置的malaria_cell_classification_code.zip文件中找到:

https://ceb.nlm.nih.gov/repositories/malaria-datasets/

我试图将X_train和Y_train更改为list而不是numpy数组。该函数在np.save方法停止。

X_train = Y_train = list()

    X_train.append(img)
    Y_train.append(j)

在numpy中保存图像的正确和标准方法是什么?

调整图像大小后,我得到不同的错误:

Done: 19400/9887 images
Done: 19500/9887 images
Done: 19600/9887 images
Done: 19700/9887 images
Done: 19800/9887 images
19842
Loading done.
Transform targets to keras compatible format.
Done: 19800/9887 images
19842
Loading done.

Transform targets to keras compatible format.

Creating validation images...

Parasitized 1098

error Traceback (most recent call last)
in ()

  2 #load data for training
  3 X_train, Y_train = load_resized_training_data(img_rows, img_cols)

----> 4 X_valid, Y_valid = load_resized_validation_data(img_rows, img_cols)

  5 #print the shape of the data
  6 print(X_train.shape, Y_train.shape, X_valid.shape, Y_valid.shape)

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_resized_validation_data(img_rows, img_cols)

103 def load_resized_validation_data(img_rows, img_cols):
104 

--> 105 X_valid, Y_valid = load_validation_data()

106 
107     # Resize images

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_validation_data()

 75 
 76             img = np.array([img])

---> 77 img2 = cv2.resize(img, (100, 100))

 78             X_valid[i] = img2
 79             Y_valid[i] = j

error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3427: error: (-215:Assertion failed) !dsize.empty() in function 'resize'


Creating validation images...

Parasitized 1098

error Traceback (most recent call last)
in ()

  2 #load data for training
  3 X_train, Y_train = load_resized_training_data(img_rows, img_cols)

----> 4 X_valid, Y_valid = load_resized_validation_data(img_rows, img_cols)

  5 #print the shape of the data
  6 print(X_train.shape, Y_train.shape, X_valid.shape, Y_valid.shape)

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_resized_validation_data(img_rows, img_cols)

103 def load_resized_validation_data(img_rows, img_cols):
104 

--> 105 X_valid, Y_valid = load_validation_data()

106 
107     # Resize images

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_validation_data()

 75 
 76             img = np.array([img])

---> 77 img2 = cv2.resize(img, (100, 100))

 78             X_valid[i] = img2
 79             Y_valid[i] = j

error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3427: error: (-215:Assertion failed) !dsize.empty() in function 'resize'
完整的脚本可以在这里找到......

https://gist.github.com/shantanuo/cfe0913b367647890451f5ae3f6fb691

取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-07-17 23:26:16

    opencv2已经返回一个numpy数组。不要创建一个新的,特别是不具有额外嵌套级别的那个:

    img = cv2.imread(os.path.join(train_data_dir, label, image_name), cv2.IMREAD_COLOR)
    img = cv2.resize(img, (100, 100))

    0 0
相关问答

1

回答

numpy的数组是什么?

2021-11-29 23:09:30 151浏览量 回答数 1

1

回答

numpy-将行添加到数组

2020-02-06 23:30:41 246浏览量 回答数 1

1

回答

如何将布尔数组转换为numpy中的索引数组

2020-02-09 13:12:00 274浏览量 回答数 1

0

回答

Python-无法将float转换为numpy数组

2019-09-25 16:01:31 401浏览量 回答数 0

1

回答

NumPy矩阵与数组类的乘法有何不同?

2020-02-07 01:23:08 242浏览量 回答数 1

1

回答

numpy中将一维数组转换为二维数组

2020-02-08 11:28:55 907浏览量 回答数 1

1

回答

numpy的优点是什么呢?

2021-11-30 18:19:52 234浏览量 回答数 1

1

回答

Python中numpy数组的合并都有哪些方法?

2021-12-01 23:25:25 67浏览量 回答数 1

1

回答

用于C ++的NumPy样式数组?

2020-02-09 13:27:42 310浏览量 回答数 1

1

回答

Numpy数组导向中数学和统计方法下图的代码结果是什么呢?

2022-07-22 11:59:11 62浏览量 回答数 1
47
文章
723
问答
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载