python - 保存多个 csv 文件并从数据集中加载图像
问题描述
我正在尝试加载具有 5k 图像的数据集,但出现此错误:
image = cv2.resize(image2, (224, 224))
TypeError: Expected Ptr<cv::UMat> for argument 'src'
但是当我只放一张图片时,一切正常。
最后一个问题:当加载了这么多图像的数据集时,是否有可能以某种方式将它们的特征(从我正在使用的滑动窗口)保存在多个 CSV 文件中?例如:fashion1.png
--> fashion1.csv
, fashion2.png
--> fashion2.csv
, ... fashion5000.png
--> fashion5000.csv
?
import cv2
import matplotlib.pyplot as plt
import numpy as np
#image2 = cv2.imread("fashion1.png")# your image path
image2 = r"C:\Users\John\Desktop\new\fashion\img\*.png"
image = cv2.resize(image2, (224, 224))
tmp = image # for drawing a rectangle
stepSize = 20
(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, :]
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)
mean_values2 = mean_values.reshape(1,3)
result = mean_values2.flatten()
print(result)
for i in range(len(result)):
result_file = open('fashion1.csv', 'a')
result_file.write("{}{}".format(result[i], '\n'))
解决方案
第一:要调整图像大小,您必须将其读入内存 -resize()
不能使用文件名。
第二:要读取*.png
,您必须使用glob.glob()
获取与此模式匹配的所有文件名,然后使用for
-loop 单独读取每个图像,调整其大小,甚至创建带有扩展名的文件名.csv
import glob
import cv2
pattern = r"C:\Users\John\Desktop\new\fashion\img\*.png"
for filename in glob.glob(pattern):
print('PNG:', filename)
image = cv2.imread(filename)
image = cv2.resize(image, (224, 224))
csv_filename = filename.replace('.png', '.csv')
print('CSV:', csv_filename)
result = [1,2,3,4,5,6]
with open(csv_filename, 'w') as fh:
for value in result:
fh.write('{}\n'.format(value))
顺便说一句:如果我理解代码,它会更简单
import glob
import cv2
# --- functions ---
def process(filename):
print('image:', filename)
image = cv2.imread(filename)
image = cv2.resize(image, (224, 224))
step_size = 20
window_width = 60
window_height = 60
width = image.shape[1] - window_width
height = image.shape[0] - window_height
# --- get all results ---
results = []
for x in range(0, width, step_size):
for y in range(0, height, step_size):
image_temp = image.copy() # for drawing a rectangle
window = image[y:y+window_height, x:x+window_width]
cv2.rectangle(image_temp, (x, y), (x+window_width, y+window_height), (255, 0, 0), 2)
cv2.imshow('image', image_temp.astype('uint8'))
# cv2.imshow() needs cv2.waitKey() to update image in window
cv2.waitKey(1) # wait only 1 ms for key
#cv2.waitKey(0) # wait until you press any key
mean_val, std_dev = cv2.meanStdDev(window)
mean_val = mean_val[:3].flatten()
#print(mean_val)
results.extend(mean_val)
cv2.destroyAllWindows() # close window at the end
# --- write all results ---
csv_filename = filename.replace('.png', '.csv')
print(' csv:', csv_filename)
with open(csv_filename, 'w') as fh:
for value in results:
fh.write("{}\n".format(value))
print('------')
# --- main ---
pattern = r"C:\Users\John\Desktop\new\fashion\img\*.png"
for filename in glob.glob(pattern):
process(filename)
推荐阅读
- java - 错误:无法启动守护进程。此问题可能是由于守护程序配置不正确造成的
- eclipse - 在 Eclipse 中更改控制台大小
- php - 使用 REST API 的自动完成功能在本地工作,但不能在 Web 服务器上工作
- c - 零填充字符串到有限长度
- python - 如何在图像识别模型中上传新文件
- couchbase - 如何获取 couchbase 中的文档列表,我们必须根据属性过滤另一个数组中的数组?
- excel - 对于给定工作簿的给定工作表,根据其名称上的条件删除该工作表中的所有命名范围
- websocket - Socket IO 监听器在 Android 中不起作用
- scala - 为什么在写入 Hive 表期间 Spark 数据框中的时间戳列发生了变化?
- asp.net - 如何在 ASP.NET MVC 应用程序中正确使用会话?