def get_bilinear_pixel_interpolation(self, img, posX, posY): out = [] # Get integer parts of positions modXi = int(posX) modYi = int(posY) # Get fractional parts of positions modXf = posX - modXi modYf = posY - modYi # To avoid going over image bounderies height, width = util.get_image_dimensions(img) modXiPlusOneLim = min(modXi + 1, width - 1) modYiPlusOneLim = min(modYi + 1, height - 1) # Get pixels in four corners for channel in range(img.shape[2]): bottom_left = img[modYi, modXi, channel] bottom_right = img[modYi, modXiPlusOneLim, channel] top_left = img[modYiPlusOneLim, modXi, channel] top_right = img[modYiPlusOneLim, modXiPlusOneLim, channel] # Calculate interpolation obtained_bottom = modXf * bottom_right + (1. - modXf) * bottom_left obtained_top = modXf * top_right + (1. - modXf) * top_left new_channel = modYf * obtained_top + (1. - modYf) * obtained_bottom out.append(int(new_channel + 0.5)) return out
def apply_bilinear_interpolation(self, img, scale): if scale <= 0: return img imHeight, imWidth = util.get_image_dimensions(img) enlargedShape = list( map(int, [imHeight * scale, imWidth * scale, img.shape[2]])) enlargedImg = np.empty(enlargedShape, dtype=np.uint8) enlargedHeight, enlargedWidth = util.get_image_dimensions(enlargedImg) rowScale = float(imHeight) / float(enlargedHeight) colScale = float(imWidth) / float(enlargedWidth) for row in range(enlargedHeight): for col in range(enlargedWidth): oriRow = row * rowScale # Find position in original image oriCol = col * colScale enlargedImg[row, col] = self.get_bilinear_pixel_interpolation( img, oriCol, oriRow) return enlargedImg
def get_nearest_neighbour_pixel_interpolation(self, img, posX, posY): out = [] # Get integer parts of positions modXi = int(posX) modYi = int(posY) # Get fractional parts of positions modXf = posX - modXi modYf = posY - modYi # To avoid going over image bounderies height, width = util.get_image_dimensions(img) modXiPlusOneLim = min(modXi + 1, width - 1) modYiPlusOneLim = min(modYi + 1, height - 1) for channel in range(img.shape[2]): target = img[modYi, modXi, channel] out.append(int(target + 0.5)) return out
def apply_sepia(img): height, width = util.get_image_dimensions(img) obtained, img = util.get_empty_image_with_same_dimensions(img) r_matrix, g_matrix, b_matrix = rgb.get_rgb_layers(img) for i in range(height): for j in range(width): r, g, b = r_matrix[i][j], g_matrix[i][j], b_matrix[i][j] tr = int(0.393 * r + 0.769 * g + 0.189 * b) tg = int(0.349 * r + 0.686 * g + 0.168 * b) tb = int(0.272 * r + 0.534 * g + 0.131 * b) r = ColorFilter.__normalize_max_value(tr, r) g = ColorFilter.__normalize_max_value(tg, g) b = ColorFilter.__normalize_max_value(tb, b) obtained[i][j] = [r, g, b] return obtained
def add_background(background, img, coord=(0, 0)): img = img_as_ubyte(img) x_size, y_size = util.get_image_dimensions(img) (y_begin, x_begin) = coord x_end = x_begin + x_size y_end = y_begin + y_size background_crop = background[ x_begin:x_end, y_begin:y_end, :] pixel_preserve = (img[:, :, 3] > 10) background_crop[pixel_preserve] = img[pixel_preserve] background[x_begin:x_end, y_begin:y_end, :] = background_crop return background