【问题标题】:How to use imwrite taking images from one folder and saving them to another using Opencv?如何使用 imwrite 从一个文件夹中获取图像并使用 Opencv 将它们保存到另一个文件夹?
【发布时间】:2020-09-06 14:28:51
【问题描述】:

我想增加文件夹中的图像。我还想在不同文件夹中扩充后保持图像的名称相同。如何使用 OpenCV 做到这一点?

# Defining path

INPUT_IMG_DIR = 'NORMAL'
OUTPUT_AUG_DIR = 'AUGMENT'

seq = iaa.Sequential([iaa.Affine(rotate=5)
#                           iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
#                           iaa.Multiply((0.5, 1.5), per_channel=0.5),
#                           iaa.Add((-10, 10), per_channel=0.5)
                         ])

for image in os.listdir(INPUT_IMG_DIR):
    image = image
    print(image)
    print(len(image))
    print(type(image))
    image = cv2.imread(image)
    
    seq_det = seq.to_deterministic()
    image_aug = seq.augment_images(image)
    print(image_aug)
    cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)

此代码不适用于我。它正在抛出这样的错误,

NORMAL_IMG_0.jpeg

<class 'str'>

None
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-28-b6ca6f4c6834> in <module>
      9     image_aug = seq.augment_images(image)
     10     print(image_aug)
---> 11     cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)

error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

【问题讨论】:

    标签: python opencv keras deep-learning computer-vision


    【解决方案1】:

    替换:

    cv2.imwrite(OUTPUT_AUG_DIR, image, image_aug)
    

    与:

    cv2.imwrite(os.path.join( OUTPUT_AUG_DIR, image), image_aug)
    

    【讨论】:

    • 感谢回复,其实现在就是这个错误,TypeError: join() argument must be str or bytes, not 'NoneType'
    • @KuldeepSinghChouhan 您必须将image = cv2.imread(image) 更改为其他名称,这样您就不会覆盖文件名,以后可以使用它。
    猜你喜欢
    • 2018-04-06
    • 2010-11-26
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多