Reputation: 41
I am trying to load a dataset which has 5k images but I am getting this error:
image = cv2.resize(image2, (224, 224))
TypeError: Expected Ptr<cv::UMat> for argument 'src'
But when I put only one image everything works fine.
One last question: Is it possible somehow, when a dataset with so many images loaded, to save their features (from the sliding window that I am using) at multiple CSV files? For example: 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'))
Upvotes: 1
Views: 540
Reputation: 143057
First: to resize image you have to read it to memory - resize()
can't use filename.
Second: to read *.png
you have to use glob.glob()
to get all filenames matching this pattern and later use for
-loop to read every image separatelly, resize it and even create filename with extension .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: If I understand code it can be simpler
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)
Upvotes: 1