【问题标题】:how to read images from my home directory and resize it如何从我的主目录中读取图像并调整其大小
【发布时间】:2019-05-19 20:28:40
【问题描述】:

我正在尝试读取图像并调整我的主目录文件中的图像大小,但没有成功,请帮助如何读取图像并调整它的大小。

    import cv2
    from PIL import Image
    img = cv2.resize(cv2.read('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-103-dab0f11a9e2d> in <module>()
      1 import cv2
      2 from PIL import Image
----> 3 img = cv2.resize(cv2.read('C://Users//NanduCn//jupter1//train-scene classification//train"', (28, 28)))

AttributeError: module 'cv2.cv2' has no attribute 'read'

【问题讨论】:

  • 请接受最能回答您问题的答案。 (或者如果他们都没有,写下你自己的答案并接受它)。

标签: python opencv opencv3.0


【解决方案1】:

读取特定扩展名的所有图像,例如"*.png",可以使用cv::glob函数

void loadImages(const std::string& ext, const std::string& path, std::vector<cv::Mat>& imgs, const int& mode)
{
    std::vector<cv::String> strBuffer;
    cv::glob(cv::String{path} + cv::String{"/*."} + cv::String{ext}, strBuffer, false);

    for (auto& it : strBuffer)
    {
        imgs.push_back(cv::imread(it, mode));
    }
}

std::vector<cv::Mat> imgs;
loadImages("*.png", "/home/img", imgs, cv::IMREAD_COLOR);

然后调整缓冲区中每个图像的大小

for (auto& it : imgs)
{
    cv::resize(it, it, cv::Size{WIDTH, HEIGHT});
}

重写为 python 应该很容易,因为几乎所有函数/数据类型在 python 中都有等价物。

filenames = glob("/home/img/*.png").sort()
images = [cv2.imread(img) for img in filenames]

for img in images:
    cv2.resize(img, (WIDTH, HEIGHT))

代码被分成几部分而不是单行,因为它更具可读性,至少对我来说是这样。

【讨论】:

  • import glob from glob import * ilenames = glob.glob("C://Users//NanduCn//jupter1//train-scene classification//train*.jpg").sort() images = [cv2.imread(img) for img in filenames] for img in images: cv2.resize(img, (28, 28))
  • ------------------------------------------ --------------------------------- AttributeError Traceback(最近一次调用最后一次) in () 1 import glob 2 from glob import * ----> 3 ilenames = glob.glob("C://Users//NanduCn//jupter1//train-scene分类//train*.jpg ").sort() 4 images = [cv2.imread(img) for img in filenames] 5 AttributeError: 'function' object has no attribute 'glob'
  • 首先,我看到一个错字ilenames 而不是filenames。另请参阅编辑后的答案。您已经导入了 glob 函数,因此请使用 glob 而不是 glob.glob
  • @martonmarsuri 如果它解决了您的问题,请不要忘记标记正确答案。或者,如果您还有其他问题,请继续。
【解决方案2】:

使用 glob 模块:

dpath = "C:/your_path/"
fmt = "png"    
size = (28, 28)

imgs = [cv2.resize(cv2.imread(fpath), size) for fpath in glob.glob("{}/*.{}".format(dpath, fmt))]

【讨论】:

    【解决方案3】:

    这里的错误是你给 cv2.imread 一个目录,但它只接受 image-path 作为输入,否则它会抛出错误。

    所以我们可以做的是使用 os 模块解析文件夹中的所有文件,然后一张一张读取图像,然后调整大小并对其进行其他操作。

    import os
    import cv2
    from PIL import Image
    size = (28, 28)
    imagesPath = "C://Users//NanduCn//jupter1//train-scene classification//train"
    
    for imageName in os.listdir(imagesPath):
    
        imageFullPath = os.path.join(imagesPath,imageName)
        img = cv2.resize(cv2.imread(imageFullPath), size)
        #do your processing here
    

    我在这里也认为您在该文件夹中只有图像。如果您有其他类型的文件或其他文件夹,您可以在 os.path.join 行之前进行检查。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2010-11-06
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多