pythonopencvpycharmimage-augmentation

Augmented images saved as grey (imgaug,imageio, opencv)


I´m starting my first Computer Vision Project and wanted to tryout image augmentation to improve my dataset. I tried it with imgaug and everything works fine, as long as I only display them. But as as soon as I save them to a file they turn out greyisch/blue.

Here´s the code I´m using:

import cv2
import imgaug.augmenters as iaa
import imageio as iio
import numpy as np


path = ('C:\\Users\\kathr\\Desktop\\JPG\\cards.jpg')
img = cv2.imread(path)

augmentation= iaa.Sequential([
    iaa.Crop(px=(0,2)),
    iaa.Flipud(0.5),
    iaa.Fliplr(0.5),
    iaa.Sometimes(
        0.5,
        iaa.GaussianBlur(sigma=(0,0.5)),
    )] ,
    random_order = True)

image = cv2.imread(path)
cv2.imshow('orign', img)
cv2.waitKey(100)


images = np.array([image for _ in range(32)], dtype=np.uint8)
images_aug = augmentation.augment_images(images)


for i in range(32):
    iio.imwrite('C:\\Users\\kathr\\Desktop\\Aug\\' + str(i) + 'aug.jpg', images_aug[i], format= '.jpg')

Solution

  • Opencv reads in images in B,G,R order. Converting them to R,G,B before saving will fix your problem using imageio. Check this post for more information.

    for i in range(32):
        images_aug[i] = cv2.cvtColor(images_aug[i], cv2.COLOR_BGR2RGB)
        iio.imwrite('C:\\Users\\kathr\\Desktop\\Aug\\' + str(i) + 'aug.jpg', images_aug[i], format= '.jpg')
    

    Since you are already using opencv you can also save them directly with opencv avoiding the conversion.

    for i in range(32):
        cv2.imwrite('C:\\Users\\kathr\\Desktop\\Aug\\' + str(i) + 'aug.jpg', images_aug[i])