【发布时间】:2020-10-08 01:11:35
【问题描述】:
我正在对图像使用滑动窗口技术,并且我正在提取每个窗口的像素的平均值。所以结果是这样的[[[[215.015625][123.55036272][111.66057478]]]]。现在的问题是如何将每个窗口的所有这些值保存到txt文件或CSV中,因为我想使用它们进一步比较相似之处?无论我尝试过什么错误都是一样的..它是一个 4D 数组而不是 1D 或 2D。我真的很感激任何帮助。!提前谢谢你
import cv2
import matplotlib.pyplot as plt
import numpy as np
# read the image and define the stepSize and window size
# (width,height)
image2 = cv2.imread("bird.jpg")# your image path
image = cv2.resize(image2, (224, 224))
tmp = image # for drawing a rectangle
stepSize = 10
(w_width, w_height) = (60, 60 ) # window size
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, :]
# classify content of the window with your classifier and
# determine if the window includes an object (cell) or not
# draw window on image
cv2.rectangle(tmp, (x, y), (x + w_width, y + w_height), (255, 0, 0), 2) # draw rectangle on image
plt.imshow(np.array(tmp).astype('uint8'))
# show all windows
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)
【问题讨论】:
-
您需要文本文件是人类可读的还是仅仅存储数组?
-
先生,我想要一个人类可读的文本文件!非常感谢
-
@chmarios 我提出的解决方案有意义吗?
-
@MaxFeinberg 是的,先生,这绝对有道理!我更喜欢使用 npy 的第二种方法,因为我是新手,所以我更容易理解它!再次非常感谢您
-
@chmarios 你介意接受我的回答吗?
标签: python numpy-ndarray sliding-window