aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.4k stars 2.44k forks source link

broblem load and show images #119

Open ghost opened 6 years ago

ghost commented 6 years ago

I tried but failed. this is my code and error,thanks

import imgaug as ia from imgaug import augmenters as iaa import numpy as np import cv2 import scipy from scipy import misc

ia.seed(1)

Example batch of images.

The array has shape (32, 64, 64, 3) and dtype uint8.

scipy.ndimage.imread('cat.jpg', flatten=False, mode="RGB")

images = cv2.imread('C:\python3.6.4.64bit\images\cat.jpg',1)

images = np.array( [ia.quokka(size=(64, 64)) for _ in range(32)], dtype=np.uint8 )

seq = iaa.Sequential([ iaa.Fliplr(0.5), # horizontal flips iaa.Crop(percent=(0, 0.1)), # random crops

Small gaussian blur with random sigma between 0 and 0.5.

# But we only blur about 50% of all images.
iaa.Sometimes(0.5,
    iaa.GaussianBlur(sigma=(0, 0.5))
),
# Strengthen or weaken the contrast in each image.
iaa.ContrastNormalization((0.75, 1.5)),
# Add gaussian noise.
# For 50% of all images, we sample the noise once per pixel.
# For the other 50% of all images, we sample the noise per pixel AND
# channel. This can change the color (not only brightness) of the
# pixels.
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
# Make some images brighter and some darker.
# In 20% of all cases, we sample the multiplier once per channel,
# which can end up changing the color of the images.
iaa.Multiply((0.8, 1.2), per_channel=0.2),
# Apply affine transformations to each image.
# Scale/zoom them, translate/move them, rotate them and shear them.
iaa.Affine(
    scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
    translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
    rotate=(-25, 25),
    shear=(-8, 8)
)

], random_order=True) # apply augmenters in random order

images_aug = seq.augment_images(images)

cv2.imshow("Original", images_aug)

=============================== and show this error:

Traceback (most recent call last): File "C:/python3.6.4.64bit/augg.py", line 52, in cv2.imshow("Original", images_aug) cv2.error: C:\projects\opencv-python\opencv\modules\core\src\array.cpp:2493: error: (-206) Unrecognized or unsupported array type in function cvGetMat

aleju commented 6 years ago

You were trying to feed 32 images into imshow(), i.e. an array of shape (N,H,W,C), but the function expects (H,W,C). Also, when using cv2 you have to keep in mind that cv2 uses BGR internally while imgaug expects RGB. So change your last line to:

cv2.imshow("Original", images_aug[0, ..., ::-1])
cv2.waitKey(0)

where ::-1 changes from RGB to BGR.

ghost commented 6 years ago

Thanks aleju, I succeeded in displaying the quokka image but I failed to show and load my folder that containing images,please help me

aleju commented 6 years ago

You can load images from a folder using something like

import glob
from scipy import ndimage
fps = glob.glob("/path/to/directory/*.jpg")
images = [ndimage.imread(fp, mode="RGB") for fp in fps]
ghost commented 6 years ago

Hi aleju, Thank you very much for your help. When I work with one image to augmentation such as scaling or translation, it's okay, but when i work with two or more images, I get the wrong results. For example, images before augmentation : beforall

and images after translate and ascaling augmentations and bboxes : afterall results

and my code :

import imgaug as ia from imgaug import augmenters as iaa import glob import os import numpy as np import cv2 ia.seed(1) img = glob.glob("C:\Python36\img\*.jpg") pic_num=1 for image in img: images=cv2.imread(image,1) b,g,r = cv2.split(images)
rgb_img = cv2.merge([r,g,b])

bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=23, y1=56, x2=276, y2=287)],shape=rgb_img.shape)  #9.jpg
bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=7, y1=66, x2=295, y2=285)], shape=rgb_img.shape)  #3.jpg
bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=72, y1=102, x2=243, y2=239),ia.BoundingBox(x1=236, y1=79, x2=300, y2=137)]
                               ,shape=rgb_img.shape)  #10.jpg
bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=111, y1=109, x2=228, y2=208)], shape=rgb_img.shape)  #7.jpg

seq = iaa.Sequential([
iaa.Multiply((1.2, 1.5)), 
iaa.Affine( translate_px={"x": 40, "y": 60}, scale=(0.3, 0.5))])

seq_det = seq.to_deterministic()
image_aug = seq_det.augment_images([rgb_img])[0]
bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

for i in range(len(bbs.bounding_boxes)):
    before = bbs.bounding_boxes[i]
    after = bbs_aug.bounding_boxes[i]
    print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (i,before.x1, before.y1, before.x2, before.y2,
    after.x1, after.y1, after.x2, after.y2))

    image_before = bbs.draw_on_image(rgb_img, thickness=2)
    image_after = bbs_aug.draw_on_image(image_aug, thickness=2, color=[0, 0, 255])
    cv2.imshow("Origl", image_after)        
    path = 'C:/newfolder/' 
    cv2.imwrite(os.path.join(path,str('merkavaafter-')+str(pic_num)+'.jpg'), image_after) 
    pic_num+=1
    cv2.waitKey(20)
aleju commented 6 years ago

Not sure what the problem in your case is. Did you really execute the code shown here? I ran it on my machine with minimal alterations (e.g. my own images) and it produced the expected results. Code:

import imgaug as ia
from imgaug import augmenters as iaa
import glob
import os
import numpy as np
import cv2
from scipy import misc
from skimage import data
ia.seed(1)
img = [data.astronaut(), data.astronaut(), data.astronaut(), data.astronaut()]
pic_num=1
for j, image in enumerate(img):
    #images=cv2.imread(image,1)
    #images = image
    #b,g,r = cv2.split(images)   
    #rgb_img = cv2.merge([r,g,b])
    rgb_img = image

    if j == 0:
        bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=23, y1=56, x2=276, y2=287)],shape=rgb_img.shape)  #9.jpg
    elif j == 1:
        bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=7, y1=66, x2=295, y2=285)], shape=rgb_img.shape)  #3.jpg
    elif j == 2:
        bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=72, y1=102, x2=243, y2=239),ia.BoundingBox(x1=236, y1=79, x2=300, y2=137)], shape=rgb_img.shape)  #10.jpg
    elif j == 3:
        bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=111, y1=109, x2=228, y2=208)], shape=rgb_img.shape)  #7.jpg

    seq = iaa.Sequential([
    iaa.Multiply((1.2, 1.5)), 
    iaa.Affine( translate_px={"x": 40, "y": 60}, scale=(0.3, 0.5))])

    seq_det = seq.to_deterministic()
    image_aug = seq_det.augment_images([rgb_img])[0]
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]

    for i in range(len(bbs.bounding_boxes)):
        before = bbs.bounding_boxes[i]
        after = bbs_aug.bounding_boxes[i]
        print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (i,before.x1, before.y1, before.x2, before.y2,
        after.x1, after.y1, after.x2, after.y2))

    image_before = bbs.draw_on_image(rgb_img, thickness=2)
    image_after = bbs_aug.draw_on_image(image_aug, thickness=2, color=[0, 0, 255])
    both = np.hstack((image_before, image_after))
    #cv2.imshow("Origl", image_after)        
    misc.imshow(both)
    #path = 'C:/newfolder/' 
    #cv2.imwrite(os.path.join(path,str('merkavaafter-')+str(pic_num)+'.jpg'), image_after)
    misc.imsave("bbstest-%d.jpg" % (j,), both) 
    pic_num+=1
    cv2.waitKey(20)

Results: bbstest-0 bbstest-1 bbstest-2 bbstest-3

ghost commented 6 years ago

you test on one type of image, If you test on Several different types of images within a folder simultaneously, you will see that the result is not reasonable; like my images

aleju commented 6 years ago

Resized the images by replacing img = [...] with

img = [
    misc.imresize(data.astronaut(), (400, 400)),
    misc.imresize(data.astronaut(), (300, 400)),
    misc.imresize(data.astronaut(), (400, 300)),
    misc.imresize(data.astronaut(), (500, 300))
]

in the above example. Still works fine for me.

bbstest-0 bbstest-1 bbstest-2 bbstest-3