Home > Blockchain >  Albumentations - Read images after augmentation
Albumentations - Read images after augmentation

Time:09-06

I would like to know how to read (if it is possible) an image after augmentation by Albumentations.
I tried:

my_img = 'xyz.jpg'

image = cv2.imread(my_img)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
transformed = transform(image=image)
new_img = np.array(list(transformed.values())
Image.formarray(new_img)

but I get this error:

TypeError: Cannot handle this data type: (1, 1, 512, 3), |u1

CodePudding user response:

The variable transformed is a dictionary type. You have to display the value stored in the key image as an image array.

>>> type(transformed)
<class 'dict'>

# store the value in image key as an array & display it
transformed_image = transformed['image']
cv2.imshow(transformed_image)

CodePudding user response:

Your four-dimensional array is likely not interpretable as an image.

Try

new_img = np.squeeze(new_img)

to remove the first two dimensions and specify dtype=np.uint8 when calling np.array.

However, your dimensions still don't seem to match up correctly. If you expected a 512 x 512 image, your dimensions would look something like (1, 1, 512, 512, 3) prior to squeezing and (512, 512, 3) afterwards.

Consider the following example:

import numpy as np
import cv2 as cv

black_1d = np.zeros((1, 1, 512, 3), dtype=np.uint8)
# This doesn't work, it yields an error
# cv.imshow('Test', black)
# cv.waitKey()

# This works
new_img_1d = np.squeeze(black_1d)
cv.imshow('Test 1', new_img_1d)
cv.waitKey()

black_2d = np.zeros((1, 1, 512, 512, 3), dtype=np.uint8)
new_img_2d = np.squeeze(black_2d)
cv.imshow('Test 2', new_img_2d)
cv.waitKey()
  • Related