gpt4 book ai didi

python - 保存多个csv文件并从数据集中加载图像

转载 作者:行者123 更新时间:2023-12-02 16:23:44 28 4
gpt4 key购买 nike

我正在尝试加载具有5k图像的数据集,但出现此错误:

image = cv2.resize(image2, (224, 224))
TypeError: Expected Ptr<cv::UMat> for argument 'src'
但是,当我只放置一张图像时,一切正常。
最后一个问题:当加载了这么多图像的数据集以某种CSV文件形式保存其特征(从我正在使用的滑动窗口中)时,是否可以以某种方式进行?例如: fashion1.png-> fashion1.csvfashion2.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))


BTW:如果我理解代码,它可能会更简单
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)

关于python - 保存多个csv文件并从数据集中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62867987/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com