保存多个 csv 文件并从数据集中加载图像
Saving in multiple csv files and loading images from a dataset
我正在尝试加载一个包含 5k 个图像的数据集,但出现此错误:
image = cv2.resize(image2, (224, 224))
TypeError: Expected Ptr<cv::UMat> for argument 'src'
但是当我只放一张图片时一切正常。
最后一个问题:当加载了如此多图像的数据集时,是否有可能以某种方式将它们的特征(来自我正在使用的滑动 window)保存在多个 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)
我正在尝试加载一个包含 5k 个图像的数据集,但出现此错误:
image = cv2.resize(image2, (224, 224))
TypeError: Expected Ptr<cv::UMat> for argument 'src'
但是当我只放一张图片时一切正常。
最后一个问题:当加载了如此多图像的数据集时,是否有可能以某种方式将它们的特征(来自我正在使用的滑动 window)保存在多个 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)