python - 我想保存到 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')
解决方案
np.save
首先使用文件保存任何numpy数组
np.save('yourFileName', your_array)
然后使用加载它np.load
load_in_array = np.load('yourFileName.npy')
这可能对你有用。
推荐阅读
- pandas - 如何从一列包含嵌套字典的csv创建熊猫数据框?
- nginx - NGINX Ingress controller returning 502 with no logs in backend application pod
- android - PendingIntent from Notifications are not coming back to service. Showing java.lang.NullPointerException
- node.js - 基于其他模型自动更新 MongoDB 模型
- find - 移动文件和复制子树
- wordpress - WC 费用 API 后计算运费
- assembly - 一个简单的 hello world shellcode 中的分段错误?
- c# - 如何使用用户输入名称创建文件?
- r - Check if columns of one data frame are present in another data frame with non-zero element in R
- javascript - 将 DIV 子项更改为无显示