This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_mod_functions.py
54 lines (44 loc) · 1.88 KB
/
image_mod_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from scipy import ndimage
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import cv2
'''
A suite of functions to manipulate numpy image matrices. Manipulations include rotation, additive gaussian noise, speckle,
up and down sampling, size standardization, to gray scale.
'''
def rotate_image(image, degree):
return ndimage.rotate(image, degree, reshape=False)
def add_gaussian_noise(image, mean, var):
sigma = var**0.5
gauss = np.random.normal(mean,sigma,image.shape)
gauss = gauss.reshape(image.shape)
noisy_image = image + gauss
return noisy_image
def rgb2gray(image, channels=1):
grayscale2D = np.dot(image[...,:3], [0.2989, 0.5870, 0.1140])
if channels == 1:
return np.expand_dims(grayscale2D, axis=2)
if channels == 3:
return np.repeat(grayscale2D[:, :, np.newaxis], 3, axis=2)
def add_speckle(image, var):
row,col,ch = image.shape
gauss = np.random.randn(row,col,ch) * var #rescales variance of distribution
gauss = gauss.reshape(row,col,ch)
noisy_image = image + image * gauss
return noisy_image
def down_sample_image(image):
im = tf.image.resize(image, size=[240,340], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, antialias=True)
image_downscaled = tf.dtypes.cast(im, dtype=tf.uint8)
return image_downscaled
def standardize_image(image, height, width):
image = cv2.resize(image, dsize=(height, width), interpolation=cv2.INTER_NEAREST)
image_resized = image.astype(np.float32)
# if single sample, reshape to include the channel again
if (len(image_resized.shape) == 2):
image_resized = image_resized.reshape(*image_resized.shape, 1)
return image_resized
def up_sample_image(image):
im = tf.image.resize(image, size=[900,1200], method=tf.image.ResizeMethod.BICUBIC)
image_upscaled = tf.dtypes.cast(im, dtype=tf.uint8)
return image_upscaled