def smooth_resize(img, n_rows, n_cols, smooth_type): ##----CAUTION--- ## THIS FUNCTION DOES NOT UPSCALE ON ONE AXIS AND DOWNSCALE IN THE OTHER # smooth_type= gaussian, bilinear interpolation, median or whatever # flag: 0 down else up if n_rows==img.shape[0]: return img if n_cols==img.shape[1]: return img flag = n_rows > img.shape[0] ratio_rows = (n_rows)*1.0 / img.shape[0] ratio_cols = (n_cols)*1.0 / img.shape[1] if flag: kernel_size = (math.ceil((ratio_rows + ratio_cols) / 2))*2-1 else: kernel_size = (math.ceil(math.log(math.ceil((math.pow(ratio_rows,-1) + math.pow(ratio_cols,-1)) / 2),2)))* 2 + 1 resized_img = Sampling.simple_resize(img, n_rows, n_cols) if flag else Sampling.simple_resize(img, n_rows, n_cols) if smooth_type == Sampling.FILTER_MEDIAN: return conv.median_filter_nd(resized_img, kernel_size) elif smooth_type == Sampling.FILTER_BILINEAR_INTERPOLATION: return conv.convolution_nd(resized_img, ker.bilinear_interpolation(kernel_size)) elif smooth_type == Sampling.FILTER_GAUSSIAN: return conv.convolution_nd(resized_img, ker.gaussian(kernel_size))
names = ['i-3-a', 'i-3-b', 'i-3-c'] char = 97 for name in names: path = '../input/convolution/' + name + '.jpg' img = cv2.imread(path) print(path) i = 0 for n in [3, 7, 15]: kernel = np.ones((n, n), dtype=np.float32) / (n * n) size = str(n) + 'x' + str(n) print(size) start_time = time.time() img_out_1 = conv.convolution_nd(img, kernel, conv_type=conv.CONV_ITERATIVE) print("--- classical algorithm: %s seconds ---" % (time.time() - start_time)) name = '../output/convolution/o-3-' + chr(char) + '-' + str( i) + '-' + size + 'ca.png' cv2.imwrite(name, img_out_1) i += 1 start_time = time.time() img_out_2 = conv.convolution_nd(img, kernel, conv_type=conv.CONV_KERNEL_SIZE) print("--- vectorized element-wise multiplication %s seconds ---" % (time.time() - start_time)) name = '../output/convolution/o-3-' + chr(char) + '-' + str(