Ejemplo n.º 1
0
def test_resize_image():
    '''
    Call resize_image on the test image then pad to 256 x 256 and save the result to file
    '''
    test_img = cv2.imread('test_image.jpeg')
    resized_image = pb.resize_image(test_img, 256)
    cv2.imwrite('resized_image' + '.jpg', resized_image)
    resized_image = pb.pad_image(resized_image, 256, 256)
    cv2.imwrite('resized_padded_image' + '.jpg', resized_image)
Ejemplo n.º 2
0
def test_gauss_noise():
    '''
    Call add_gauss_noise on the test image with a large amount of noise and save the result, then do the same with a small amount of noise.
    '''
    test_img = cv2.imread('test_image.jpeg')
    test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
    if test_img is None:
        print('Image is None')
    high_noise_img = pb.add_gauss_noise(test_img,10)
    cv2.imwrite('test_high_gauss_noise.jpg',high_noise_img)
    low_noise_img = pb.add_gauss_noise(test_img, 1)
    cv2.imwrite('test_low_gauss_noise.jpg',low_noise_img)
Ejemplo n.º 3
0
def test_mirror_image():
    '''
    Call mirror_image on the test image and save the result to file
    '''
    test_img = cv2.imread('test_image.jpeg')
    mirrored_image = pb.mirror_image(test_img)
    cv2.imwrite('mirrored_image' + '.jpg', mirrored_image)
Ejemplo n.º 4
0
def test_pad_image():
    '''
    Call pad_image on the test image to pad an image and save the results.
    '''
    test_img = cv2.imread('test_image.jpeg')
    test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
    if test_img is None:
        print('Image is None')
    test_img = pb.pad_image(test_img, 50, 50)
    cv2.imwrite('padded_img.jpeg', test_img)
Ejemplo n.º 5
0
def test_adjust_brightness():
    '''
    Call adjust_brightness on the test image and save the result to file
    '''
    test_img = cv2.imread('test_image.jpeg')
    if test_img is None:
        print('Image is None')
    img_file_index = 0    
    for i in range(-10, 10, 3):
        brightned_img = pb.adjust_brightness(test_img, i)
        img_file_index = img_file_index + 1
        cv2.imwrite('test_brightned_img' + str(img_file_index) + '.jpg', brightned_img)
Ejemplo n.º 6
0
def test_rotate_image():
    '''
    Call rotate_image on the test image and rotate the image 5 times and save the results.
    '''
    test_img = cv2.imread('test_image.jpeg')
    test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
    if test_img is None:
        print('Image is None')
    degrees = [np.random.uniform(-5, 5) for i in range(5)]
    for i in degrees:
        test_img = pb.rotate_image(test_img, i)
        cv2.imwrite('rotated_img_' + str(i) +'.jpeg', test_img)
def augment_data_set(input_dir, output_dir):
    '''
    Augments the data set using the perturbations in pdetect_perturbations.

    Parameters
    ----------
    input_dir: string
    The input directory containing only image files to be perturbed

    output_dir: string
    The output directory

    Returns
    -------
    No return value
    '''

    # initialize constants
    max_dim = 256

    # list the perturbations you would like to apply
    perturbations = ['resize', 'noise', 'resize', 'brightness', 'flip']

    file_num = 0  # initialize current file number
    file_count = len([
        name for name in os.listdir(input_dir)
        if (os.path.isfile(input_dir + '/' +
                           name) and ('.jpeg' in name or '.jpg' in name))
    ])
    for file in os.listdir(input_dir):

        if (('.jpeg' not in file) or ('.jpg' not in file) and ('._' in file)):
            continue

        current_percent = (file_num / file_count) * 100

        # load the file
        image = cv2.imread(input_dir + '/' + file)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # noise - must go first because it will have a much larger
        # impact after resizing
        if 'noise' in perturbations:
            image = pb.add_sp_noise(image, 0.50, 0.00001)
            image = pb.add_gauss_noise(image, 0.5)

        # apply perturbations
        if 'resize' in perturbations:
            image = pb.resize_image(image, 256)
            image = pb.pad_image(image, 256, 256)
            cv2.imwrite(output_dir + '/' + 'resized_' + file, image)

        # apply brightness perturbation
        if 'brightness' in perturbations:
            bness = np.random.uniform(-10, 10)
            bright_image = pb.adjust_brightness(image, 10)
            cv2.imwrite(output_dir + '/' + 'bright_image_' + file,
                        bright_image)

        # save the non-flipped image and then save the flipped copy
        # if enabled
        cv2.imwrite(file + '.jpg', image)
        if 'flip' in perturbations:
            image = pb.mirror_image(image)
            cv2.imwrite(output_dir + '/' + 'flipped_' + file, image)

        # save the non-rotated image and then save the rotated copy
        # if enabled
        cv2.imwrite(file, image)

        if 'rotate' in perturbations:
            angle = np.random.uniform(-5, 5)
            image = pb.rotate_image(image, angle)
            cv2.imwrite(output_dir + '/' + 'rotated_' + file, image)

        image = None  # clear out the image
        print('Finished file, currently ' + str(current_percent) + ' done.')
        file_num += 1