OpenCV Warpaffine错误在图像增强期间使用符号符号

发布于 2025-01-29 12:06:32 字数 4106 浏览 3 评论 0原文

我一直在尝试使用名为Albimodations的库进行图像增强。但是在转换图像时,我从OpenCV中遇到了一些错误。我在Kaggle的笔记本上运行了下面的代码。该数据集在Kaggle上称为“英特尔图像分类”。它有6堂课。每个图像为150 * 150 * 3。

import numpy as np
import tensorflow as tf
import albumentations as a

x_train_path = "../input/intel-image-classification/seg_train/seg_train"

train_data =  tf.keras.utils.image_dataset_from_directory(
  x_train_path,
  seed=123,
  image_size=(150, 150),
  batch_size=128)

transforms = Compose([
            a.Rotate(limit=40),
            a.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5),
            a.GaussianBlur(p=0.5),
            a.RandomContrast(limit=0.2, p=0.5),
            a.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3),
            a.HorizontalFlip(),
        ])

#looping through the train_data generator
for image,label in train_data:

    #albumentations requires data to be numpy array and dtype uint8
    image = tf.cast(image,tf.uint8).numpy()
    
    aug_image = transforms(image=image)    # ERROR HERE
    print(aug_image)
    break

这是整个错误:

error                                     Traceback (most recent call last)
/tmp/ipykernel_33/2678995032.py in <module>
      7 
      8 
----> 9     aug_image = transforms(image=image)
     10     print(aug_image)
     11     break

/opt/conda/lib/python3.7/site-packages/albumentations/core/composition.py in __call__(self, force_apply, *args, **data)
    208 
    209         for idx, t in enumerate(transforms):
--> 210             data = t(force_apply=force_apply, **data)
    211 
    212             if check_each_transform:

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in __call__(self, force_apply, *args, **kwargs)
     95                     )
     96                 kwargs[self.save_key][id(self)] = deepcopy(params)
---> 97             return self.apply_with_params(params, **kwargs)
     98 
     99         return kwargs

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in apply_with_params(self, params, force_apply, **kwargs)
    110                 target_function = self._get_target_function(key)
    111                 target_dependencies = {k: kwargs[k] for k in self.target_dependence.get(key, [])}
--> 112                 res[key] = target_function(arg, **dict(params, **target_dependencies))
    113             else:
    114                 res[key] = None

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/rotate.py in apply(self, img, angle, interpolation, **params)
     86 
     87     def apply(self, img, angle=0, interpolation=cv2.INTER_LINEAR, **params):
---> 88         return F.rotate(img, angle, interpolation, self.border_mode, self.value)
     89 
     90     def apply_to_mask(self, img, angle=0, **params):

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in wrapped_function(img, *args, **kwargs)
     68     def wrapped_function(img, *args, **kwargs):
     69         shape = img.shape
---> 70         result = func(img, *args, **kwargs)
     71         if len(shape) == 3 and shape[-1] == 1 and len(result.shape) == 2:
     72             result = np.expand_dims(result, axis=-1)

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/functional.py in rotate(img, angle, interpolation, border_mode, value)
     77         cv2.warpAffine, M=matrix, dsize=(width, height), flags=interpolation, borderMode=border_mode, borderValue=value
     78     )
---> 79     return warp_fn(img)
     80 
     81 

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in __process_fn(img)
    187             img = np.dstack(chunks)
    188         else:
--> 189             img = process_fn(img, **kwargs)
    190         return img
    191 

error: OpenCV(4.5.4) /tmp/pip-req-build-21t5esfk/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'

I have been trying to do image augmentation using a library called Albumentations. But I got some error from OpenCV while transforming the images. I ran the code below on Kaggle's notebook. The dataset is called "Intel image classification" on kaggle. It has 6 classes. Each image is 150 * 150 * 3.

import numpy as np
import tensorflow as tf
import albumentations as a

x_train_path = "../input/intel-image-classification/seg_train/seg_train"

train_data =  tf.keras.utils.image_dataset_from_directory(
  x_train_path,
  seed=123,
  image_size=(150, 150),
  batch_size=128)

transforms = Compose([
            a.Rotate(limit=40),
            a.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5),
            a.GaussianBlur(p=0.5),
            a.RandomContrast(limit=0.2, p=0.5),
            a.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3),
            a.HorizontalFlip(),
        ])

#looping through the train_data generator
for image,label in train_data:

    #albumentations requires data to be numpy array and dtype uint8
    image = tf.cast(image,tf.uint8).numpy()
    
    aug_image = transforms(image=image)    # ERROR HERE
    print(aug_image)
    break

Here is the whole error:

error                                     Traceback (most recent call last)
/tmp/ipykernel_33/2678995032.py in <module>
      7 
      8 
----> 9     aug_image = transforms(image=image)
     10     print(aug_image)
     11     break

/opt/conda/lib/python3.7/site-packages/albumentations/core/composition.py in __call__(self, force_apply, *args, **data)
    208 
    209         for idx, t in enumerate(transforms):
--> 210             data = t(force_apply=force_apply, **data)
    211 
    212             if check_each_transform:

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in __call__(self, force_apply, *args, **kwargs)
     95                     )
     96                 kwargs[self.save_key][id(self)] = deepcopy(params)
---> 97             return self.apply_with_params(params, **kwargs)
     98 
     99         return kwargs

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in apply_with_params(self, params, force_apply, **kwargs)
    110                 target_function = self._get_target_function(key)
    111                 target_dependencies = {k: kwargs[k] for k in self.target_dependence.get(key, [])}
--> 112                 res[key] = target_function(arg, **dict(params, **target_dependencies))
    113             else:
    114                 res[key] = None

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/rotate.py in apply(self, img, angle, interpolation, **params)
     86 
     87     def apply(self, img, angle=0, interpolation=cv2.INTER_LINEAR, **params):
---> 88         return F.rotate(img, angle, interpolation, self.border_mode, self.value)
     89 
     90     def apply_to_mask(self, img, angle=0, **params):

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in wrapped_function(img, *args, **kwargs)
     68     def wrapped_function(img, *args, **kwargs):
     69         shape = img.shape
---> 70         result = func(img, *args, **kwargs)
     71         if len(shape) == 3 and shape[-1] == 1 and len(result.shape) == 2:
     72             result = np.expand_dims(result, axis=-1)

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/functional.py in rotate(img, angle, interpolation, border_mode, value)
     77         cv2.warpAffine, M=matrix, dsize=(width, height), flags=interpolation, borderMode=border_mode, borderValue=value
     78     )
---> 79     return warp_fn(img)
     80 
     81 

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in __process_fn(img)
    187             img = np.dstack(chunks)
    188         else:
--> 189             img = process_fn(img, **kwargs)
    190         return img
    191 

error: OpenCV(4.5.4) /tmp/pip-req-build-21t5esfk/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文