【问题标题】:Saving in multiple csv files and loading images from a dataset保存多个 csv 文件并从数据集中加载图像
【发布时间】:2020-07-13 01:13:01
【问题描述】:

我正在尝试加载包含 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'))

【问题讨论】:

  • 发布有关产生异常的代码的问题时,请始终包含完整的 Traceback - 复制并粘贴它,然后将其格式化为代码(选择它并键入 ctrl-k)。有机会为您的问题制作minimal reproducible example 吗? -我们当然无权访问r"C:\Users\John\Desktop\new\fashion\img\*.png"。
  • cv2 文档对.resize 的src 参数有什么看法?它应该是一个字符串吗?
  • 看起来cv2.resize() 不接受包含通配符的src 参数……这并不奇怪。我认为您需要在 for 循环中单独处理它们,使用 glob.glob() 或 os.listdir() 之类的东西来获取每个文件名。
  • resize() 需要在内存中加载图像而不是文件名 - 并且 cv2 无法使用 * 打开文件,但您必须使用 os.listdir() 或 glob.glob() 获取所有文件名并用于 -循环分别打开每个文件并与resize() 一起使用
  • 是的,可以单独保存.csv——当你打开fashion1.png时,你也可以使用这个名字来创建名字fashion1.csv,和其他名字一样。

标签: python opencv cv2


【解决方案1】:

首先:要调整图像大小,您必须将其读入内存 - 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多