def Sobel_Filter(img): sobel_x = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=5) sobel_y = cv2.Sobel(img, cv2.CV_16S, 0, 1, ksize=5) sobel_x = cv2.convertScaleAbs(sobel_x) sobel_y = cv2.convertScaleAbs(sobel_y) sobel_xy = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0) return sobel_xy
def sobel_extract(img): x = cv2.Sobel(img, cv2.CV_16S, 1, 0) # 1,0代表只计算x方向计算边缘 y = cv2.Sobel(img, cv2.CV_16S, 0, 1) # 0,1代表只在y方向计算边缘 absX = cv2.convertScaleAbs(x) absY = cv2.convertScaleAbs(y) dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) return dst
def apply_sobel(img, *params): ddepth = cv2.CV_16S scale = 1 delta = 0 kernel_size = 3 img = cv2.GaussianBlur(img, (5, 5), cv2.BORDER_DEFAULT) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) grad_x = cv2.Sobel(img, ddepth, 1, 0, ksize=kernel_size, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT) grad_y = cv2.Sobel(img, ddepth, 0, 1, ksize=kernel_size, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT) abs_grad_x = cv2.convertScaleAbs(grad_x) abs_grad_y = cv2.convertScaleAbs(grad_y) new_image = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0) #new_image = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=3) return new_image
def DeriveOG_filter(img, kernel_size, sigma): # img_x为x方向上的梯度 # img_y为y方向上的梯度 kernel_x = np.zeros((kernel_size, kernel_size)) kernel_y = np.zeros((kernel_size, kernel_size)) for i in range(kernel_size): for j in range(kernel_size): norm = (i - kernel_size // 2)**2 + (j - kernel_size // 2)**2 kernel_x[i, j] = (j - kernel_size // 2) * np.exp(-norm / (sigma**2 * 2)) kernel_y[i, j] = (i - kernel_size // 2) * np.exp(-norm / (sigma**2 * 2)) s_x = np.sum(abs(kernel_x)) / 2 s_y = np.sum(abs(kernel_y)) / 2 kernel_x = kernel_x / s_x kernel_y = kernel_y / s_y img_x = filter.conv(img, kernel_x, "copy") img_y = filter.conv(img, kernel_y, "copy") img_x = cv.convertScaleAbs(img_x) img_y = cv.convertScaleAbs(img_y) img_edge = 0.5 * img_x + 0.5 * img_y img_edge = (img_edge - np.min(img_edge)) / (np.max(img_edge) - np.min(img_edge)) * 255 return img_edge
def sobel_gradient(image): grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) # 对x求一阶导 grad_y = cv.Sobel(image, cv.CV_32F, 0, 1) # 对y求一阶导 gradx = cv.convertScaleAbs(grad_x) # 用convertScaleAbs()函数将其转回原来的uint8形式 grady = cv.convertScaleAbs(grad_y) # cv.imshow("sobel_x", gradx) # x方向上的梯度 # cv.imshow("sobel_y", grady) # y方向上的梯度 gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0) # 图片融合 cv.imshow("sobel", gradxy)
def edge_filter(img): kernel1 = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) kernel2 = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) result1 = conv(img, kernel1) result1 = cv.convertScaleAbs(result1) result2 = conv(img, kernel2) result2 = cv.convertScaleAbs(result2) result = 0.5 * result1 + 0.5 * result2 result = cv.convertScaleAbs(result) return result
def sobel_demo(image): #当边缘不明显时可用Scharr算子,但该算子受噪声影响较大 grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) grad_y = cv.Sobel(image, cv.CV_32F, 0, 1) gradx = cv.convertScaleAbs(grad_x) grady = cv.convertScaleAbs(grad_y) cv.imshow("x_dir", gradx) cv.imshow("y_dir", grady) gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0) cv.imshow("xy_dir", gradxy)
def get_cityscapes_rm_da(img, semantics): """Applies two adaptive filters to find road markings""" semantics_road = (128, 64, 128) mask_road = cv2.inRange(semantics, semantics_road, semantics_road) # cv2.imwrite('temp/cityscapes_road_mask.png', mask_road) height = img.shape[0] width = img.shape[1] vertices = np.array([[(0, height), (0, int(height * 3 / 4)), (width, int(height * 3 / 4)), (width, height)]]) mask_bottom = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8) cv2.fillPoly(mask_bottom, vertices, 255) mask_top = cv2.bitwise_not(mask_bottom) mask_road_top = cv2.bitwise_and(mask_road, mask_road, mask=mask_top) mask_road_bottom = cv2.bitwise_and(mask_road, mask_road, mask=mask_bottom) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # img = cv2.bitwise_and(img, img, mask=mask) thr_adaptive_top = masked_adaptive_threshold(img, mask_road_top, max_value=255, size=101, C=-15) thr_adaptive_top = cv2.convertScaleAbs(thr_adaptive_top) thr_adaptive_top = cv2.bitwise_and(thr_adaptive_top, thr_adaptive_top, mask=mask_road_top) thr_adaptive_bottom = masked_adaptive_threshold(img, mask_road_bottom, max_value=255, size=251, C=-15) thr_adaptive_bottom = cv2.convertScaleAbs(thr_adaptive_bottom) thr_adaptive_bottom = cv2.bitwise_and(thr_adaptive_bottom, thr_adaptive_bottom, mask=mask_road_bottom) thr_adaptive_combined = cv2.bitwise_or(thr_adaptive_top, thr_adaptive_bottom) plt.figure() plt.subplot(3, 1, 1) plt.imshow(img, cmap='gray') plt.subplot(3, 1, 2) plt.imshow(thr_adaptive_top, cmap='gray') plt.subplot(3, 1, 3) plt.imshow(thr_adaptive_bottom, cmap='gray') return thr_adaptive_combined
def sobelFilter(image): # Use Sobel filter on abscisse X. filterX = cv.Sobel(image, cv.CV_64F, 1, 0, ksize=3) absoluteX = cv.convertScaleAbs(filterX) # Use Sobel filter on abscisse Y. filterY = cv.Sobel(image, cv.CV_64F, 0, 1, ksize=3) absoluteY = cv.convertScaleAbs(filterY) # Converge the output filter. filteredImage = np.hypot(absoluteX, absoluteY) filteredImage = filteredImage / filteredImage.max() * 255 angle = np.rad2deg(np.arctan2(filterY, filterX)) return (filteredImage, angle)
def addGaussNoise(img, mu, sigma): noise = np.random.normal(mu, sigma, img.shape) # dst_img = np.clip(img, -1, 1) dst_img = img.copy() dst_img = dst_img + noise dst_img = cv.convertScaleAbs(dst_img) return dst_img
def median_filter(img, kernel_size): img_dim = len(img.shape) if img_dim == 3: img_row = img.shape[0] img_col = img.shape[1] img_channel = img.shape[2] result = np.zeros((img_row, img_col, img_channel)) ex_img = np.pad(img, ((kernel_size // 2, kernel_size // 2), (kernel_size // 2, kernel_size // 2), (0, 0)), 'constant', constant_values=(1, 1)) result_row = result.shape[0] result_col = result.shape[1] result_channel = result.shape[2] for i in range(result_row): for j in range(result_col): for k in range(result_channel): result[i, j, k] = np.median(ex_img[i:i + kernel_size, j:j + kernel_size, k]) elif img_dim == 2: img_row = img.shape[0] img_col = img.shape[1] result = np.zeros((img_row, img_col)) ex_img = np.pad(img, ((kernel_size // 2, kernel_size // 2), (kernel_size // 2, kernel_size // 2)), 'constant', constant_values=(1, 1)) result_row = result.shape[0] result_col = result.shape[1] for i in range(result_row): for j in range(result_col): result[i, j] = np.median(ex_img[i:i + kernel_size, j:j + kernel_size]) result = cv.convertScaleAbs(result) return result
def bilateral_filter(img, kernel_size, sigmas, sigmar): img_dim = len(img.shape) if img_dim == 3: img_row = img.shape[0] img_col = img.shape[1] img_channel = img.shape[2] kernels = gaussian_kernel(kernel_size, sigmas) kernelr = np.zeros((kernel_size, kernel_size)) kernel = np.zeros((kernel_size, kernel_size)) result = np.zeros((img_row, img_col, img_channel)) # ex_img = np.pad(img, ((kernel_size // 2, kernel_size // 2), (kernel_size // 2, kernel_size // 2), (0, 0)), 'constant', constant_values=(0, 0)) ex_img = padding(img, (kernel_size // 2, kernel_size // 2), (kernel_size // 2, kernel_size // 2), "reflect") result_row = result.shape[0] result_col = result.shape[1] result_channel = result.shape[2] for i in range(result_row): for j in range(result_col): for m in range(0, kernel_size, 1): for n in range(0, kernel_size, 1): kernelr[m, n] = (float(ex_img[i + m, j + n, 0]) - float(ex_img[i + kernel_size // 2, j + kernel_size // 2, 0]))**2 + (float(ex_img[i + m, j + n, 1]) - float( ex_img[i + kernel_size // 2, j + kernel_size // 2, 1]))**2 + (float(ex_img[i + m, j + n, 2]) - float(ex_img[i + kernel_size // 2, j + kernel_size // 2, 2]))**2 kernelr = np.exp(-kernelr / (2 * sigmar**2)) kernel = kernels * kernelr s = np.sum(kernel) kernel = kernel / s for k in range(result_channel): result[i, j, k] = np.sum(ex_img[i:i + kernel_size, j:j + kernel_size, k] * kernel) result = cv.convertScaleAbs(result) return result.astype(np.uint8)
def up_sample(img, kernel_size, sigma): img_dim = len(img.shape) if img_dim == 3: row = img.shape[0] col = img.shape[1] channel = img.shape[2] dst_img = np.zeros((2 * row, 2 * col, channel)) for i in range(2 * row): for j in range(2 * col): if i % 2 == 0 and j % 2 == 0: for k in range(channel): dst_img[i, j, k] = img[i // 2, j // 2, k] ex_img = filter.padding(dst_img, (kernel_size // 2, kernel_size // 2), (kernel_size // 2, kernel_size // 2), "reflect") gauss_k = filter.gaussian_kernel(kernel_size, sigma) for i in range(2 * row): for j in range(2 * col): for k in range(channel): if dst_img[i, j, k] == 0: s = np.sum( (ex_img[i:i + kernel_size, j:j + kernel_size, k] != 0) * gauss_k) dst_img[i, j, k] = np.sum( ex_img[i:i + kernel_size, j:j + kernel_size, k] * gauss_k) / s dst_img = cv.convertScaleAbs(dst_img) return dst_img.astype(np.uint8)
def convert_file(dcm_file_path, jpg_file_path): print("lalala") dicom_img = dicom.read_file(dcm_file_path) img = dicom_img.pixel_array scaled_img = cv2.convertScaleAbs( img-np.min(img), alpha=(255.0 / min(np.max(img)-np.min(img), 10000))) cv2.imwrite(jpg_file_path, scaled_img)
def gauss_filter1(img, kernel_size, sigma): kernel = gaussian_kernel1(kernel_size, sigma) result = conv1_col(img, kernel) result = conv1_row(result, kernel.transpose()) result = cv.convertScaleAbs(result) return result.astype(np.uint8)
def gray(img): row, col, _ = img.shape result = np.zeros((row, col)) for i in range(row): for j in range(col): result[i, j] = 0.114 * img[i, j, 0] + 0.299 * img[i, j, 1] + 0.587 * img[i, j, 2] result = cv.convertScaleAbs(result) return result
def custom_demo(image): kernel = np.array([[ 1, 1, 1, ], [1, -8, 1], [1, 1, 1]]) dst = cv.filter2D(image, cv.CV_32F, kernel=kernel) custom = cv.convertScaleAbs(dst) cv.imshow("result", custom)
def sharpen_filter(img): kernel1 = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]]) kernel2 = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) / 9 alpha = 0.8 kernel = (1 + alpha) * kernel1 - alpha * kernel2 result = conv(img, kernel) result = cv.convertScaleAbs(result) return result
def sharpen_filter(img, kernel_size, sigma, alpha): kernel1 = np.zeros((kernel_size, kernel_size)) kernel1[kernel_size // 2, kernel_size // 2] = 1 kernel2 = gaussian_kernel2(kernel_size, sigma) kernel = (1 + alpha) * kernel1 - alpha * kernel2 result = conv2(img, kernel) result = cv.convertScaleAbs(result) return result.astype(np.uint8)
def calculate_weight_map(tar_img, ref_img, mask): tar_img_YUV = cv2.GaussianBlur(cv2.cvtColor(tar_img, cv2.COLOR_BGR2YUV), (5, 5), 10) ref_img_YUV = cv2.GaussianBlur(cv2.cvtColor(ref_img, cv2.COLOR_BGR2YUV), (5, 5), 10) tar_img_Y = tar_img_YUV[:, :, 0] ref_img_Y = ref_img_YUV[:, :, 0] YUV_diff = np.abs(tar_img_YUV - ref_img_YUV) color_diff = cv2.convertScaleAbs(YUV_diff[:, :, 0] * 0.5 + YUV_diff[:, :, 1] * 0.25 + YUV_diff[:, :, 2] * 0.25) color_diff[mask.overlap == 0] = 0 print('finish YUV_diff') grad_diff_mag = getGradDiff(tar_img_Y, ref_img_Y) grad_diff_mag[mask.overlap == 0] = 0 print('finish grad_diff') color_grad_diff_sum = grad_diff_mag * 100 + color_diff filter_bank = getGarborFilterBank(tar_img_Y, ref_img_Y) # print('finish gabor filter bank') h, w = tar_img_Y.shape tar_result = np.zeros((h, w)) for i in range(len(filter_bank)): temp = cv2.filter2D(tar_img_Y, cv2.CV_64FC1, filter_bank[i]) tar_result += temp**2 tar_result = np.sqrt(tar_result) tar_result[mask.overlap == 0] = 0 print('finish gabor feature target') ref_result = np.zeros((h, w)) for i in range(len(filter_bank)): temp = cv2.filter2D(ref_img_Y, cv2.CV_64FC1, filter_bank[i]) ref_result += temp**2 ref_result = np.sqrt(ref_result) ref_result[mask.overlap == 0] = 0 print('finish gabor feature reference') gabor_result = ref_result + tar_result weight_map = np.multiply(gabor_result, color_grad_diff_sum) print('finish weight map') # cv2.imwrite('./YUV_diff.png',color_diff) # cv2.imwrite('./gradian_diff.png',(grad_diff_mag/np.max(grad_diff_mag)*255).astype(np.uint8)) # cv2.imwrite('./color_grad_diff.png',(color_grad_diff_sum/np.max(color_grad_diff_sum)*255).astype(np.uint8)) # cv2.imwrite(f'./gabor/ref_gobar_final.png',(ref_result/np.max(ref_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/gobar_final.png',(gabor_result/np.max(gabor_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/tar_gobar_final.png',(tar_result/np.max(tar_result)*255).astype(np.uint8) ) # cv2.imwrite(f'./gabor/W_final.png',(weight_map-np.mean(weight_map))/np.std(weight_map)*255) return weight_map
def mean_filter(img): kernel = np.zeros((3, 3), np.float32) for i in range(0, 3, 1): for j in range(0, 3, 1): kernel[i, j] = 1 kernel = kernel / 9 result = conv(img, kernel) result = cv.convertScaleAbs(result) return result
def calc_hog(gray): ''' 计算梯度 ''' dx = cv.Sobel(gray, cv.CV_16S, 1, 0) dy = cv.Sobel(gray, cv.CV_16S, 0, 1) sigma = 1e-3 # 计算角度 angle = np.int32(np.arctan(dy / (dx + sigma)) * 180 / np.pi) + 90 dx = cv.convertScaleAbs(dx) dy = cv.convertScaleAbs(dy) # 计算梯度大小 mag = cv.addWeighted(dx, 0.5, dy, 0.5, 0) print('angle\n', angle[:8, :8]) print('mag\n', mag[:8, :8]) ''' end of 计算梯度 ''' # 将图像切成多个cell cell_size = 8 bin_size = 9 img_h, img_w = gray.shape[:2] cell_h, cell_w = (img_h // cell_size, img_w // cell_size) cells = np.zeros((cell_h, cell_w, bin_size), dtype=np.int32) for i in range(cell_h): cell_row = cell_size * i for j in range(cell_w): cell_col = cell_size * j cells[i, j] = calc_hist( mag[cell_row:cell_row + cell_size, cell_col:cell_col + cell_size], angle[cell_row:cell_row + cell_size, cell_col:cell_col + cell_size], bin_size) # 多个cell融合成block block_size = 2 block_h, block_w = (cell_h - block_size + 1, cell_w - block_size + 1) blocks = np.zeros((block_h, block_w, block_size * block_size * bin_size), dtype=np.float32) for i in range(block_h): for j in range(block_w): blocks[i, j] = l2_norm(cells[i:i + block_size, j:j + block_size]) return blocks.flatten()
def apply_contrast_brightness(img, *params): alpha = params[0] beta = params[1] #alpha = 1.0 # Simple contrast control #beta = 0 # Simple brightness control new_image = cv2.convertScaleAbs(img, alpha=alpha, beta=beta) #new_image = (img * alpha) + beta #new_image = cv2.addWeighted(img, alpha, img, 0, beta) return new_image
def ssr_c(img, size): img_G = replaceZeroes(cv2.GaussianBlur(img, (size, size), 0)) img = replaceZeroes(img) # img_G = cv2.GaussianBlur(img, (size, size), 0) log_S = cv2.log(img / 255.0) g_L = cv2.log(img_G / 255.0) log_L = cv2.multiply(log_S, g_L) log_R = cv2.subtract(log_S, log_L) dst_R = cv2.normalize(log_R, None, 0, 255, cv2.NORM_MINMAX) R_c = cv2.convertScaleAbs(dst_R) return R_c
def get_oxford_rm(img, mask): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) thr_adaptive = masked_adaptive_threshold(img, mask, max_value=255, size=51, C=-20) thr_adaptive = cv2.convertScaleAbs(thr_adaptive) thr_adaptive = cv2.bitwise_and(thr_adaptive, thr_adaptive, mask=mask) kernel = np.ones((2, 2)) morph = cv2.morphologyEx(thr_adaptive, cv2.MORPH_OPEN, kernel) dil = cv2.dilate(morph, kernel) return dil
def edgeDetectionZeroCrossingLOG(I: np.ndarray) -> (np.ndarray, np.ndarray): # smooth with gaussian I = blurImage2(I, 9) # turn to GrayScale and activate laplace filter src_gray = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY) lap = cv2.Laplacian(src_gray, cv2.CV_16S, ksize=7) # show the img # activate abs only if you want to show the picture # make sure you find the edge points before using abs lap = cv2.convertScaleAbs(lap) return lap
def getContours(self, frame): #intermediate states where frame is being edited' self.frame = frame frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame2 = cv2.GaussianBlur(frame2, (25,25), 0) thresh = cv2.threshold(frame2, self.threshold, self.maxVal, cv2.THRESH_BINARY)[1] #incredibly important for using imageio to cv2 thresh = cv2.convertScaleAbs(thresh) thresh = cv2.dilate(thresh, None, iterations=1) contours, h = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) return contours
def conv1_row(img, kernel): img_dim = len(img.shape) if img_dim == 3: img_row = img.shape[0] img_col = img.shape[1] img_channel = img.shape[2] kernel_size = kernel.shape[1] result = np.zeros((img_row, img_col, img_channel)) # 横向卷积 ex_img = np.pad(img, ((0, 0), (kernel_size // 2, kernel_size // 2), (0, 0)), 'constant', constant_values=(0, 0)) result_row = result.shape[0] result_col = result.shape[1] result_channel = result.shape[2] for i in range(result_row): for j in range(result_col): for k in range(result_channel): result[i, j, k] = np.sum( ex_img[i:i + 1, j:j + kernel_size, k] * kernel) elif img_dim == 2: img_row = img.shape[0] img_col = img.shape[1] kernel_size = kernel.shape[1] result = np.zeros((img_row, img_col)) # 横向卷积 ex_img = np.pad(img, ((0, 0), (kernel_size // 2, kernel_size // 2)), 'constant', constant_values=(0, 0)) result_row = result.shape[0] result_col = result.shape[1] for i in range(result_row): for j in range(result_col): result[i, j] = np.sum(ex_img[i:i + 1, j:j + kernel_size // 2] * kernel) result = cv.convertScaleAbs(result) return result
def contrast_image(self, image, contrast_factor): """ Adjust contrast of the given image. Parameters ---------- img (numpy ndarray): numpy ndarray to be adjusted. contrast_factor (float): How much to adjust the contrast. Can be any non negative number. 0 gives a solid gray image, 1 gives the original image while 2 increases the contrast by a factor of 2. Returns ------- numpy ndarray: Contrast adjusted image. """ # alpha 1 beta 0 --> no change # 0 < alpha < 1 --> lower contrast # alpha > 1 --> higher contrast # -127 < beta < +127 --> good range for brightness values result = cv2.convertScaleAbs(image, alpha=contrast_factor, beta=0) return result
def padding(img, ud, lr, paddingtype): up = ud[0] down = ud[1] left = lr[0] right = lr[1] dst_img = np.zeros((img.shape[0] + up + down, img.shape[1] + left + right, 3)) dst_img[up:up + img.shape[0], left:left + img.shape[1], 0:3] = img if paddingtype == "black": pass if paddingtype == "wrap": dst_img[0:up, 0:left, 0:3] = dst_img[img.shape[0]:img.shape[0] + up, img.shape[1]:img.shape[1] + left, 0:3] dst_img[up:up + img.shape[0], 0:left, 0:3] = dst_img[up:up + img.shape[0], img.shape[1]:img.shape[1] + left, 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, 0:left, 0:3] = dst_img[up:up + down, img.shape[1]:img.shape[1] + left, 0:3] dst_img[0:up, left:left + img.shape[1], 0:3] = dst_img[img.shape[0]:img.shape[0] + up, left:left + img.shape[1], 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, left:left + img.shape[1], 0:3] = dst_img[up:up + down, left:left + img.shape[1], 0:3] dst_img[0:up, left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[img.shape[0]:img.shape[0] + up, left:left + right, 0:3] dst_img[up:up + img.shape[0], left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[up:up + img.shape[0], left:left + right, 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[up:up + down, left:left + right, 0:3] if paddingtype == "copy": dst_img[0:up, 0:left, 0:3] = dst_img[up:up + 1, left:left + 1, 0:3] dst_img[up:up + img.shape[0], 0:left, 0:3] = dst_img[up:up + img.shape[0], left:left + 1, 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, 0:left, 0:3] = dst_img[up + img.shape[0] - 1:up + img.shape[0], left:left + 1, 0:3] dst_img[0:up, left:left + img.shape[1], 0:3] = dst_img[up:up + 1, left:left + img.shape[1], 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, left:left + img.shape[1], 0:3] = dst_img[up + img.shape[0] - 1:up + img.shape[0], left:left + img.shape[1], 0:3] dst_img[0:up, left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[up:up + 1, left + img.shape[1] - 1:left + img.shape[1], 0:3] dst_img[up:up + img.shape[0], left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[up:up + img.shape[0], left + img.shape[1] - 1:left + img.shape[1], 0:3] dst_img[up + img.shape[0]:up + img.shape[0] + down, left + img.shape[1]:left + img.shape[1] + right, 0:3] = dst_img[up + img.shape[0] - 1:up + img.shape[0], left + img.shape[1] - 1:left + img.shape[1], 0:3] if paddingtype == "reflect": dst_img[0:up, 0:left, 0:3] = np.flip(dst_img[up:up + up, left:left + left, 0:3], (0, 1)) dst_img[up:up + img.shape[0], 0:left, 0:3] = np.flip(dst_img[up:up + img.shape[0], left:left + left, 0:3], 1) dst_img[up + img.shape[0]:up + img.shape[0] + down, 0:left, 0:3] = np.flip(dst_img[up + img.shape[0] - down:up + img.shape[0], left:left + left, 0:3], (0, 1)) dst_img[0:up, left:left + img.shape[1], 0:3] = np.flip(dst_img[up:up + up, left:left + img.shape[1], 0:3], 0) dst_img[up + img.shape[0]:up + img.shape[0] + down, left:left + img.shape[1], 0:3] = np.flip(dst_img[up + img.shape[0] - down:up + img.shape[0], left:left + img.shape[1], 0:3], 0) dst_img[0:up, left + img.shape[1]:left + img.shape[1] + right, 0:3] = np.flip(dst_img[up:up + up, left + img.shape[1] - right:left + img.shape[1], 0:3], (0, 1)) dst_img[up:up + img.shape[0], left + img.shape[1]:left + img.shape[1] + right, 0:3] = np.flip(dst_img[up:up + img.shape[0], left + img.shape[1] - right:left + img.shape[1]], 1) dst_img[up + img.shape[0]:up + img.shape[0] + down, left + img.shape[1]:left + img.shape[1] + right, 0:3] = np.flip( dst_img[up + img.shape[0] - down:up + img.shape[0], left + img.shape[1] - right:left + img.shape[1], 0:3], (0, 1)) dst_img = cv.convertScaleAbs(dst_img) return dst_img