def standardize(self, image, mean=0.48462227599918, std=0.22889466674951): """ Standardize a given image, by subtracting mean and dividing by standar deviation. Parameters ---------- image : ndarray input data mean : float given mean, will be subtracted from each pixel std : float given standar deviation, will be subtracted from each pixel. Returns --------- img: int (2d array of ints) 2d array of pixel values """ img = Data._read_image(self, image) img = img.astype(np.float32) / 255.0 img = np.divide(np.subtract(img, mean), std) return img
def image_crop(self, image, crop=None, random_crop=False): """ Crop the given image. - If crop is None crop size is generated with a random size range from [0.5*height,height] - If random_crop == True image croped from a random position Parameters: ------------- image: ndarray [H, W, C] input data crop: list, int [target_height, target_width], height and width of crop Returns: ---------- croped image with shape[crop[0], crop[1], C] """ image = Data._read_image(self, image) hei, wid, _ = image.shape if crop is None: crop = (np.random.randint(int(hei / 2), hei), np.random.randint(int(wid / 2), wid)) th, tw = [int(round(x / 2)) for x in crop] if random_crop: th, tw = np.random.randint(0, hei - crop[0] - 1), np.random.randint( 0, wid - crop[1] - 1) return image[th:th + crop[0], tw:tw + crop[1]]
def contrast_adjust(self, image, alpha=1.3, beta=20): """ Increase/ Decrease -- adjust the contrast of the given image. Parameters ---------- image : ndarray, ints, floats input data Returns ------ denoised_image : ndarray, ints, floats Denoised image """ img = Data._read_image(self, image) newimage = img.astype(np.float32) * alpha + beta if type(img[0, 0, 0]) == np.uint8: newimage[newimage < 0] = 0 newimage[newimage > 255] = 255 return np.uint8(newimage) else: newimage[newimage < 0] = 0 newimage[newimage > 1] = 1. return newimage
def translation(self, image, shifting, width, height): """ Translation Parameters ---------- image : ndarray, ints, floats input data Returns ------ denoised_image : ndarray, ints, floats Denoised image """ img = Data._read_image(self, image) HEIGHT = height WIDTH = width if shifting == 'left': for i in range(HEIGHT, 1, -1): for j in range(WIDTH): if (i < HEIGHT - 20): img[j][i] = img[j][i - 20] elif (i < HEIGHT - 1): img[j][i] = 0 elif shifting == 'right': # Shifting target_height for j in range(WIDTH): for i in range(HEIGHT): if (i < HEIGHT - 20): img[j][i] = img[j][i + 20] elif shifting == 'up': # Shifting Up for j in range(WIDTH): for i in range(HEIGHT): if (j < WIDTH - 20 and j > 20): img[j][i] = img[j + 20][i] else: img[j][i] = 0 else: for j in range(WIDTH, 1, -1): for i in range(278): if (j < 144 and j > 20): img[j][i] = img[j - 20][i] return img
def wavelet_denoised(self, image): """ Perform wavelet denoising on an image. Parameters ---------- image : ndarray input data Returns ------ denoised_image : ndarray Denoised image """ img = Data._read_image(self, image) denoised_image = denoise_wavelet(img, sigma=0.1) return denoised_image
def bilateral_denoised(self, image): """ Denoise image using bilateral filter. Parameters ---------- image : ndarray input data Returns ------ denoised_image : ndarray Denoised image """ img = Data._read_image(self, image) denoised_image = denoise_bilateral(img) return denoised_image
def total_variation_denoised(self, image): """ Perform total-variation denoising. Parameters ---------- image : ndarray input data Returns ------ denoised_image : ndarray Denoised image """ img = Data._read_image(self, image) denoised_image = denoise_tv_chambolle(img, weight=100) return denoised_image
def med_denoised(self, image): """ Denoise image using median filter. Parameters ---------- image : ndarray input data Returns ------ denoised_image : ndarray Denoised image """ img = Data._read_image(self, image) denoised_image = nd.median_filter(img, 3) return denoised_image
def gauss_denoised(self, image): """ Denoise image using gauss filter. Parameters ---------- image : ndarray input data Returns ------ denoised_image : ndarray Denoised image """ img = Data._read_image(self, image) denoised_image = nd.gaussian_filter(img, 2) return denoised_image
def flip_image(self, image): """ Flips a given image. Parameters ---------- image : ndarray, ints, floats input data Returns ------ denoised_image : ndarray, ints, floats Denoised image """ img = Data._read_image(self, image) flipped_img = np.fliplr(img) return flipped_img
def remove_mean(self, image, mean): """ Subtract give mean from each pixel of the image. Parameters ---------- image : ndarray input data Returns: ---------- img : int (2d array of ints) normalized 2d array of pixel values """ img = Data._read_image(self, image) img = img.astype(np.float32) img = np.subtract(np.divide(img, 255.0), mean) return img
def image_pad(self, image, pad_width=None, axis=0, mode='symmetric'): """ Adds padding to the given image. Parameters ---------- image : ndarray, ints, floats input data pad_width : int Returns ------ denoised_image : ndarray, ints, floats Denoised image """ image = Data._read_image(self, image) hei, wid = image.shape[0], image.shape[1] if pad_width is None: th = hei // 10 tw = wid // 10 pad_width = ((th, th), (tw, tw), (0, 0)) if axis == 0: if type(pad_width[0]) == tuple: pad_width = (pad_width[0], (0, 0), (0, 0)) else: pad_width = (pad_width, (0, 0), (0, 0)) if axis == 1: if type(pad_width[0]) == tuple: pad_width = ((0, 0), pad_width[1], (0, 0)) else: pad_width = ((0, 0), pad_width, (0, 0)) if len(image.shape) == 3: newimage = np.pad(image, pad_width, mode) elif len(image.shape) == 2: newimage = np.squeeze( np.pad(image[:, :, np.newaxis], pad_width, mode)) return cv2.resize(newimage, (wid, hei), interpolation=cv2.INTER_NEAREST)
def sample_wise_normalization(self, image): """ Sample wise normalization. Parameters ---------- image : ndarray input data Returns ------ image : ndarray normalized image """ img = Data._read_image(self, image) data = img data.astype(np.float32) if np.max(data) == np.min(data): return np.ones_like(data, dtype=np.float32) * 1e-6 else: return 1.0 * (data - np.min(data)) / (np.max(data) - np.min(data))
def random_flip(self, image, lr, ud): """ Flips the given image randomly. Parameters ---------- image : ndarray, ints, floats input data Returns ------- denoised_image : ndarray, ints, floats Denoised image """ img = Data._read_image(self, image) if lr: if np.random.random() > 0.5: img = cv2.flip(img, flipCode=1) if ud: if np.random.random() > 0.5: img = cv2.flip(img, flipCode=0) return img
def add_noise(self, img, width, height, depth): """ Adds random noise to the image. Parameters ---------- image : ndarray, ints, floats input image height : int height of the image width : int width of the image depth : int depth of the image Returns ------ denoised_image : ndarray, ints, floats Denoised image """ img = Data._read_image(self, img) HEIGHT = height WIDTH = width DEPTH = depth noise = np.random.randint(5, size=(200, 300, 2), dtype='uint8') for i in range(WIDTH): for j in range(HEIGHT): for k in range(DEPTH): if (img[i][j][k] != 255): img[i][j][k] += noise[i][j][k] return img