首页 > 解决方案 > 我想保存到 CSV 但数组是 4D

问题描述

我有一个滑动窗口,我想将每个窗口的像素平均值的结果保存在 CSV 文件中,但是 numpy 给我一个错误,即无法保存 4D 数组并需要 1D-2D。请问有什么建议吗?

import cv2
import matplotlib.pyplot as plt
import numpy as np


image2 = cv2.imread("image.jpg")

image = cv2.resize(image2, (224, 224))
tmp = image  
stepSize = 50
(w_width, w_height) = (100, 100 ) 
for x in range(0, image.shape[1] - w_width, stepSize):
    for y in range(0, image.shape[0] - w_height, stepSize):
        window = image[x:x + w_width, y:y + w_height, :]

       
        cv2.rectangle(tmp, (x, y), (x + w_width, y + w_height), (255, 0, 0), 2)  
        plt.imshow(np.array(tmp).astype('uint8'))
       
        plt.show()
        mean_values=[]
        mean_val, std_dev = cv2.meanStdDev(image)
        mean_val = mean_val[:3]
        mean_values.append([mean_val])
        mean_values = np.asarray(mean_values)

        print(mean_values.reshape(3))
        
        print(mean_values.shape)
        np.reshape(mean_values,(1,3))
        np.savetxt("stats.csv", mean_values, delimiter=",", fmt='%s')

标签: python

解决方案


np.save首先使用文件保存任何numpy数组

np.save('yourFileName', your_array)

然后使用加载它np.load

load_in_array = np.load('yourFileName.npy')

这可能对你有用。


推荐阅读