def modifiedLaplacian(img): ''''LAPM' algorithm (Nayar89)''' M = np.array([-1, 2, -1]) G = cv2.getGaussianKernel(ksize=3, sigma=-1) Lx = cv2.sepFilter2D(src=img, ddepth=cv2.cv.CV_64F, kernelX=M, kernelY=G) Ly = cv2.sepFilter2D(src=img, ddepth=cv2.cv.CV_64F, kernelX=G, kernelY=M) FM = np.abs(Lx) + np.abs(Ly) return cv2.mean(FM)[0]
def gaussderiv(img, sigma): x = np.array(list(range(math.floor(-3.0 * sigma + 0.5), math.floor(3.0 * sigma + 0.5) + 1))) G = np.exp(-x**2 / (2 * sigma**2)) G = G / np.sum(G) D = -2 * (x * np.exp(-x**2 / (2 * sigma**2))) / (np.sqrt(2 * math.pi) * sigma**3) D = D / (np.sum(np.abs(D)) / 2) Dx = cv2.sepFilter2D(img, -1, D, G) Dy = cv2.sepFilter2D(img, -1, G, D) return Dx, Dy
def compute_gradient(img, use_scharr=True): if use_scharr: norm_factor = 32 gradx = cv2.Scharr(img, cv2.CV_32F, 1, 0, scale=1.0 / norm_factor) grady = cv2.Scharr(img, cv2.CV_32F, 0, 1, scale=1.0 / norm_factor) else: kx = cv2.getDerivKernels(1, 0, ksize=1, normalize=True) ky = cv2.getDerivKernels(0, 1, ksize=1, normalize=True) gradx = cv2.sepFilter2D(img, cv2.CV_32F, kx[0], kx[1]) grady = cv2.sepFilter2D(img, cv2.CV_32F, ky[0], ky[1]) gradient = np.dstack([gradx, grady]) return gradient
def estimate_std(z, method='daub_reflect'): import cv2 # Estimates noise standard deviation assuming additive gaussian noise # Check method if (method not in NoiseEstMethod.values()) and (method in NoiseEstMethod.keys()): method = NoiseEstMethod[method] else: raise Exception("Invalid noise estimation method.") # Check shape if len(z.shape) == 2: z = z[..., np.newaxis] elif len(z.shape) != 3: raise Exception("Supports only up to 3D images.") # Run on multichannel image channels = z.shape[2] dev = np.zeros(channels) # Iterate over channels for ch in range(channels): # Daubechies denoising method if method == NoiseEstMethod[ 'daub_reflect'] or method == NoiseEstMethod['daub_replicate']: daub6kern = np.array([ 0.03522629188571, 0.08544127388203, -0.13501102001025, -0.45987750211849, 0.80689150931109, -0.33267055295008 ], dtype=np.float32, order='F') if method == NoiseEstMethod['daub_reflect']: wav_det = cv2.sepFilter2D(z, -1, daub6kern, daub6kern, borderType=cv2.BORDER_REFLECT_101) else: wav_det = cv2.sepFilter2D(z, -1, daub6kern, daub6kern, borderType=cv2.BORDER_REPLICATE) dev[ch] = np.median(np.absolute(wav_det)) / 0.6745 # Return standard deviation return dev
def EnergyRGB(src): # X derivative kx, ky = cv2.getDerivKernels(dx=1, dy=0, ksize=3) img_dx = cv2.sepFilter2D(src=src * 1.0, ddepth=-1, kernelX=kx, kernelY=ky) # Y derivative kx, ky = cv2.getDerivKernels(dx=0, dy=1, ksize=3) img_dy = cv2.sepFilter2D(src=src * 1.0, ddepth=-1, kernelX=kx, kernelY=ky) # Absolute sum for each channel img_e = abs(img_dx[:, :, 0]) + abs(img_dy[:, :, 0]) img_e = img_e + abs(img_dx[:, :, 1]) + abs(img_dy[:, :, 1]) img_e = img_e + abs(img_dx[:, :, 2]) + abs(img_dy[:, :, 2]) return img_e
def reduce_image(image): """Reduces an image to half its shape. The autograder will pass images with even width and height. When dealing with odd dimensions, the output image should be the result of rounding up the division by 2. For example (width, height): (13, 19) -> (7, 10) For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: output image with half the shape, same type as the input image. """ # 5-tap filter alpha = 3./8 # same as np.array([1., 4., 6., 4., 1.]) / 16. kernel = np.array([1. - (alpha * 2.), 1., (alpha * 4.), 1., 1. - (alpha * 2.)]) / 4. # sub-sample every other row/col reduced_image = cv2.sepFilter2D(image, -1, kernel, kernel)[::2, ::2] return reduced_image
def expand_image(image): """Expands an image doubling its width and height. For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: same type as 'image' with the doubled height and width. """ image_norm = image.copy() h, w = image.shape expand_ = np.zeros((2 * h, 2 * w)) expand_[::2, ::2] = image_norm five_tap = (1 / 8) * np.array([1, 4, 6, 4, 1]) expand = cv2.sepFilter2D(expand_, ddepth=cv2.CV_64F, kernelX=five_tap, kernelY=five_tap) return expand raise NotImplementedError
def reduce_image(image): """Reduces an image to half its shape. The autograder will pass images with even width and height. It is up to you to determine values with odd dimensions. For example the output image can be the result of rounding up the division by 2: (13, 19) -> (7, 10) For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: output image with half the shape, same type as the input image. """ image_norm = image.copy() five_tap = (1 / 16) * np.array([1, 4, 6, 4, 1]) reduce = cv2.sepFilter2D(src=image_norm, ddepth=cv2.CV_64F, kernelX=five_tap, kernelY=five_tap) reduce = reduce[::2, ::2] h, w = reduce.shape return reduce raise NotImplementedError
def separateFilter(srcKernel, srcImg): """ Helper function to separate filters based on separability condition arguments: srcKernel -- input Kernel srcImg -- input Image """ U, W, Vt = np.linalg.svd(srcKernel) rank = np.linalg.matrix_rank(srcKernel) if rank == 1: sepKernelX = math.sqrt(W[0]) * U[:, 0] sepKernelY = math.sqrt(W[0]) * Vt[:, 0] filteredSepImg = cv.sepFilter2D(grayImg, -1, sepKernelX, sepKernelY) else: sepKernel = W[0] * U[:, 0] * Vt[:, 0] filteredSepImg = cv.sepFilter2D(grayImg, -1, sepKernel, sepKernel) return filteredSepImg
def expand_image(image): """Expands an image doubling its width and height. For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: same type as 'image' with the doubled height and width. """ h, w = image.shape template = np.zeros((h * 2, w * 2)) template[::2, ::2] = image.copy() img_out = cv2.sepFilter2D(template, -1, kernel(), kernel()) * 4.0 return img_out
def reduce_image(image): """Reduces an image to half its shape. The autograder will pass images with even width and height. It is up to you to determine values with odd dimensions. For example the output image can be the result of rounding up the division by 2: (13, 19) -> (7, 10) For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: output image with half the shape, same type as the input image. """ # Apply the kernel and take every second pixel img_out = cv2.sepFilter2D(image.copy(), -1, kernel(), kernel())[::2, ::2] return img_out
def laplacian_pyramid(img, levels): img = zeroone(img) gaussian_after_downsize = [img] laplacian = [] for i in range(levels): orig = gaussian_after_downsize[-1] # blurred = cv2.GaussianBlur( # orig, # ksize=(0,0), # sigmaX=1.0, # ) blurred = cv2.sepFilter2D( orig, ddepth=cv2.CV_32F, kernelX=pyramid_kernel, kernelY=pyramid_kernel, borderType=cv2.BORDER_REPLICATE, ) laplacian.append(orig - blurred) downsized = vis.resize_nearest(blurred, blurred.shape[0] // 2, blurred.shape[1] // 2) gaussian_after_downsize.append(downsized) laplacian.append(blurred) return laplacian
def expand_image(image): """Expands an image doubling its width and height. For simplicity and efficiency, implement a convolution-based method using the 5-tap separable filter. Follow the process shown in the lecture 6B-L3. Also refer to: - Burt, P. J., and Adelson, E. H. (1983). The Laplacian Pyramid as a Compact Image Code You can find the link in the problem set instructions. Args: image (numpy.array): grayscale floating-point image, values in [0.0, 1.0]. Returns: numpy.array: same type as 'image' with the doubled height and width. """ h, w = image.shape H, W = 2 * h, 2 * w img_odd = np.zeros((H, W), dtype=np.float64) img_odd[0:H:2, 0:W:2] = image[:, :] kernel = np.array([[0.125, 0.5, 0.75, 0.5, 0.125]], dtype=np.float64) res = cv2.sepFilter2D(img_odd, -1, kernel, kernel) return res raise NotImplementedError
def Convolucion2D(img, kernels, txt, save=0): if np.linalg.matrix_rank(kernels[1].dot(kernels[1].transpose())) != 1: print( "Los vectores que ha incluido no son validos (la matriz formada por ellos tiene rango !=1" ) return -1 else: print("El rango de la matriz es 1, luego la mascara es separable") # Creo bordes imgBorder = cv.copyMakeBorder(img, 5, 5, 5, 5, cv.BORDER_REFLECT) # Emborrono la imagen para eliminar el ruido blurredImg = cv.GaussianBlur(src=imgBorder, ksize=(0, 0), sigmaX=1, sigmaY=1) # retorno la imagen con los filtros aplicados final = cv.sepFilter2D(src=blurredImg.astype(np.float) / 255., ddepth=-1, kernelX=kernels[0], kernelY=kernels[1]) final = normalize(final) if txt != 0: pintaI(final, txt) if save == 1: saveImage(final) return final
def ConvolucionMascaraDerivadas(imagen,dx,dy,ksize,titulo): # Obtenemos los kernels de derivadas kernel = cv2.getDerivKernels(dx,dy, ksize=ksize) # si ddepth=-1, la imagen destino tendrá igual profundidad que la fuente # Pasamos el kernel por la imagen i2 = cv2.sepFilter2D(imagen, -1, kernel[0], kernel[1]) showImagenWait( i2, titulo )
def ConvolucionPrimeraDerivada(nombre_imagen, tam, indice): i2b = leeimagen(nombre_imagen, 0) i2b = addBorders(i2b, cv2.BORDER_CONSTANT, 10, 0) kernel = cv2.getDerivKernels(1,1,tam) i2b = cv2.sepFilter2D(i2b, -1, kernel[0], kernel[1]) showImagenWait( i2b, 'Convolucion 2B'+indice )
def gausssmooth(img, sigma): x = np.array( list( range(math.floor(-3.0 * sigma + 0.5), math.floor(3.0 * sigma + 0.5) + 1))) G = np.exp(-x**2 / (2 * sigma**2)) G = G / np.sum(G) return cv2.sepFilter2D(img, -1, G, G)
def detect(self, image): floatimage = cv2.cvtColor(np.float32(image), cv2.COLOR_BGR2GRAY) / 255 if self.gaussian is None or self.gaussian.shape[ 0] != Configuration.log_kernel_size: self.gaussian = cv2.getGaussianKernel( Configuration.log_kernel_size, -1, cv2.CV_32F) gaussian_filtered = cv2.sepFilter2D(floatimage, cv2.CV_32F, self.gaussian, self.gaussian) # LoG filtered = cv2.Laplacian(gaussian_filtered, cv2.CV_32F, ksize=Configuration.log_block_size) # DoG #gaussian2 = cv2.getGaussianKernel(Configuration.log_block_size, -1, cv2.CV_32F) #gaussian_filtered2 = cv2.sepFilter2D(floatimage, cv2.CV_32F, gaussian2, gaussian2) #filtered = gaussian_filtered - gaussian_filtered2 mi = np.min(filtered) ma = np.max(filtered) if mi - ma != 0: filtered = 1 - (filtered - mi) / (ma - mi) _, thresholded = cv2.threshold(filtered, Configuration.log_threshold, 1.0, cv2.THRESH_BINARY) self.debug = thresholded thresholded = np.uint8(thresholded) contours = None if int(cv2.__version__.split('.')[0]) == 2: contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) else: _, contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) candidates = [] for i in range(len(contours)): rect = cv2.boundingRect(contours[i]) v1 = rect[0:2] v2 = np.add(rect[0:2], rect[2:4]) if rect[2] < Configuration.log_max_rect_size and rect[ 3] < Configuration.log_max_rect_size: roi = floatimage[v1[1]:v2[1], v1[0]:v2[0]] _, _, _, maxLoc = cv2.minMaxLoc(roi) maxLoc = np.add(maxLoc, v1) candidates.append(maxLoc) self.candidates = candidates return candidates
def hog(img, gamma=0.3, cell_size=6, block_size=3, normalization_tipe=2): # GAMMA/COLOR NORMALIZATION #Compute the normalize gamma and colour img = np.power(img, gamma, dtype=np.float32) # GRADIENT COMPUTATION # Compute centered horizontal and vertical gradients with no smoothing kernel = np.asarray([-1, 0, 1]) kernel_vacio = np.asarray([1]) gradientsx = cv2.sepFilter2D(img, -1, kernel, kernel_vacio) #showImgAndWait("Gradientes", gradientsx) gradientsy = cv2.sepFilter2D(img, -1, kernel_vacio, kernel) #showImgAndWait("Gradientes", gradientsy) # SPATIAL / ORIENTATION BINNING # Calculate the magnitudes and angles of the gradients magnitude, angle = cv2.cartToPolar(gradientsx, gradientsy, angleInDegrees=True) # If the image is in color, we pick the channel with the biggest value as the # intensity of that pixel. intensity = np.argmax(magnitude, axis=2) x, y = np.ogrid[:intensity.shape[0], :intensity.shape[1]] max_angle = angle[x, y, intensity] max_magnitude = magnitude[x, y, intensity] # 0-360 angle to 0-180 max_angle = (360 - max_angle) % 180 #generate the cells cells = genCells(max_angle, max_magnitude, cell_size) #generate the blocks with normalization and Gaussian blocks_normalized = genBlocks(block_size, cells, normalization_tipe=normalization_tipe) #return a vector return blocks_normalized.flatten()
def MascaraSeparable( nombre_imagen, tam1, indice ): i2a = leeimagen( nombre_imagen, 0) i2a = addBorders(i2a, cv2.BORDER_REFLECT, 10, 0) # Pasando sigma negativo, lo calcula a partir de ksize kernel1 = cv2.getGaussianKernel(tam1, -1, cv2.CV_32F) print(kernel1) i2a = cv2.sepFilter2D(i2a, -1, kernel1, kernel1) showImagenWait(i2a, 'MascaraSeparable 2A'+indice)
def downsample2_antialiased(X): kernel = np.array([1, 3, 3, 1]) / 8 dst = cv2.sepFilter2D(X, -1, kernel, kernel, anchor=(1, 1), borderType=cv2.BORDER_REPLICATE) return dst[::2, ::2]
def decompose_and_filter(img, kernel_name, kernel): sigmas, u, vt = cv.SVDecomp(kernel) vec_index = 0 if (sigmas[0][0] != 0): print("%s could not be decomposed ... approximating" % kernel_name) vec_index = np.where(sigmas == sigmas.max())[0][0] kernel1_V = np.sqrt(sigmas[vec_index, 0]) * u[:, vec_index] kernel1_H = np.sqrt(sigmas[vec_index, 0]) * vt[vec_index, :] return cv.sepFilter2D(img, -1, kernel1_H, kernel1_V)
def gausssmooth(img, sigma): # Create Gaussian kernel and filter image with it. x = np.array( list( range(math.floor(-3.0 * sigma + 0.5), math.floor(3.0 * sigma + 0.5) + 1))) G = np.exp(-x**2 / (2 * sigma**2)) G = G / np.sum(G) return cv2.sepFilter2D(img, -1, G, G)
def triangular_blur(src, radius): if radius == 0: return src elif radius <= 1: p = 12.0 / radius / (radius + 2) - 2 kernel = np.asarray([1, p, 1], dtype=np.float64) / (p + 2) return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel, borderType=cv2.BORDER_REFLECT) else: radius = int(radius) kernel = range(1, radius + 1) + [radius + 1] + range(radius, 0, -1) kernel = np.asarray(kernel, dtype=np.float64) / (radius + 1)**2 return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel, borderType=cv2.BORDER_REFLECT)
def laplace_of_gauss(self, img: np.ndarray) -> np.ndarray: # TODO: make it class method somehow ksize, sigma = self.extraction_opts.get('ksize', 3), self.extraction_opts.get('sigma', 0) kernel_x = cv2.getGaussianKernel(ksize=ksize, sigma=sigma) kernel_y = kernel_x.T gauss = -cv2.sepFilter2D(img, cv2.CV_64F, kernel_x, kernel_y) log_img = cv2.Laplacian(gauss, cv2.CV_64F) log_img = self.camera.apply_mask(log_img, self.camera.get_mask(self.camera.apply_blur(img))) log_img[log_img < 0] = 0 laser = self.fine_laser(log_img) return laser
def draw(self, img_in, extra_in): out = cv2.sepFilter2D( src=img_in, ddepth=-1, kernelX=self.kernel_X[0], kernelY=self.kernel_Y[0], anchor=(self._kernel_X.anchor, self._kernel_Y.anchor), delta=self.delta, borderType=self.border_type, ) return out
def create_attentuation_map(self, r_edges, edges, signed): edges_m = np.zeros(edges.shape) edges_m[edges == 0.] = 1. kernel = cv2.getGaussianKernel(13, -1, cv2.CV_64F) sep = cv2.sepFilter2D(edges_m * 255., cv2.CV_64F, kernel, kernel) # cv2.imwrite(os.path.join(self.test_folder, 'sep_cam.jpg'), sep) d = cv2.filter2D(edges_m * 255., -1, kernel) # cv2.imwrite(os.path.join(self.test_folder, 'd_cam.jpg'), d) gb = cv2.GaussianBlur(edges_m * 255., (5, 5), -1) # cv2.imwrite(os.path.join(self.test_folder, 'gb_cam.jpg'), gb) return d
def gauss_contrast(): kernal = cv2.getGaussianKernel(17, 5) img = cv2.imread(img_path_zhangyi) img_gauss1 = cv2.GaussianBlur(img, (17, 17), 5) img_gauss2 = cv2.sepFilter2D(img, -1, kernal, kernal) cv2.imshow('img_g1', img_gauss1) cv2.imshow('img_g2', img_gauss2) key = cv2.waitKey() if key == 17: cv2.destroyAllWindows()
def vhDeltaProductFilter(self, gray): """ at each pixel, take the difference of values between the pixel above and pixel below, and multiply it with the difference for the pixels to the left and right. """ edge_kernel = numpy.array([-1.0, 0, 1.0], numpy.float32) id_kernel = numpy.array([0, 1.0, 0], numpy.float32) numpy_gray = numpy.array(gray, numpy.float32) horiz = numpy.zeros((gray.height, gray.width), numpy.float32) vert = numpy.zeros((gray.height, gray.width), numpy.float32) filtered = cv.CreateMat(gray.height, gray.width, cv.CV_8UC1) horiz = cv2.sepFilter2D(numpy_gray, -1, id_kernel, edge_kernel) vert = cv2.sepFilter2D(numpy_gray, -1, edge_kernel, id_kernel) horiz = numpy.abs(horiz) vert = numpy.abs(vert) cv.Mul(cv.fromarray(horiz), cv.fromarray(vert), filtered) return filtered
def difference_of_Gaussians(img): kernel1 = cv2.getGaussianKernel(10, 1) kernel2 = cv2.getGaussianKernel(10, 3) dog_img = cv2.sepFilter2D(img, -1, kernel1 - kernel2, kernel1 - kernel2) dog_img = cv2.normalize(dog_img, dog_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) return dog_img
def filter_SVD(img, kernel): img_svd = img.copy() w, u, vt = cv.SVDecomp(kernel) # getting the highest singular value i_value = np.argmax(w) vt = vt[i_value, :].reshape((1, 3)) u = u[:, i_value].reshape((3, 1)) * w[i_value, 0:1] # filtering the image w/ the obtained kernel img_svd = cv.sepFilter2D(img_svd, -1, vt, u) return img_svd
def conv_tri(src, radius): """ Image convolution with a triangle filter. :param src: input image :param radius: gradient normalization radius :return: convolution result """ if radius == 0: return src elif radius <= 1: p = 12.0 / radius / (radius + 2) - 2 kernel = N.asarray([1, p, 1], dtype=N.float64) / (p + 2) return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel, borderType=cv2.BORDER_REFLECT) else: radius = int(radius) kernel = range(1, radius + 1) + [radius + 1] + range(radius, 0, -1) kernel = N.asarray(kernel, dtype=N.float64) / (radius + 1) ** 2 return cv2.sepFilter2D(src, ddepth=-1, kernelX=kernel, kernelY=kernel, borderType=cv2.BORDER_REFLECT)
def flow_feature(refImg, curImg, th): # compute difference diff = cv2.subtract(cv2.cvtColor(curImg, cv2.COLOR_RGB2GRAY), cv2.cvtColor(refImg, cv2.COLOR_RGB2GRAY)) ret, diffMask = cv2.threshold(diff, th, 255, cv2.THRESH_BINARY) diffMask = diffMask.astype(np.uint8) diff = cv2.bitwise_and(diff, diff, mask=diffMask) # compute sum add = cv2.add(cv2.cvtColor(curImg, cv2.COLOR_RGB2GRAY), cv2.cvtColor(refImg, cv2.COLOR_RGB2GRAY)) # create filter dg = np.array(([1, 0, -1]), dtype="float32") gd = np.array(([0.2163, 0.5674, 0.2163]), dtype="float32") # norm = 1 # compute component dx = cv2.sepFilter2D(add, cv2.CV_32F, gd, dg) dy = cv2.sepFilter2D(add, cv2.CV_32F, dg, gd) # compute mag ang direction mag, angle = cv2.cartToPolar(dy, dx) # compute mag mask velocity = cv2.bitwise_and(mag, mag, mask=diffMask) direction = cv2.bitwise_and(angle, angle, mask=diffMask) * 180 / np.pi / 2 return diff, velocity, direction
def estimate_std(z, method='daub_reflect'): # Estimates noise standard deviation assuming additive gaussian noise # Check method if (method not in NoiseEstMethod.values()) and (method in NoiseEstMethod.keys()): method = NoiseEstMethod[method] else: raise Exception("Invalid noise estimation method.") # Check shape if len(z.shape) == 2: z = z[..., np.newaxis] elif len(z.shape) != 3: raise Exception("Supports only up to 3D images.") # Run on multichannel image channels = z.shape[2] dev = np.zeros(channels) # Iterate over channels for ch in range(channels): # Daubechies denoising method if method == NoiseEstMethod['daub_reflect'] or method == NoiseEstMethod['daub_replicate']: daub6kern = np.array([0.03522629188571, 0.08544127388203, -0.13501102001025, -0.45987750211849, 0.80689150931109, -0.33267055295008], dtype=np.float32, order='F') if method == NoiseEstMethod['daub_reflect']: wav_det = cv2.sepFilter2D(z, -1, daub6kern, daub6kern, borderType=cv2.BORDER_REFLECT_101) else: wav_det = cv2.sepFilter2D(z, -1, daub6kern, daub6kern, borderType=cv2.BORDER_REPLICATE) dev[ch] = np.median(np.absolute(wav_det)) / 0.6745 # Return standard deviation return dev
def remove_slp(img, gstd1=GSTD1, gstd2=GSTD2, gstd3=GSTD3, ksize=KSIZE, w=W): """Remove the SLP from kinect IR image The input image should be a float32 numpy array, and should NOT be a square root image Parameters ------------------ img : (M, N) float ndarray Kinect NIR image with SLP pattern gstd1 : float Standard deviation of gaussian kernel 1 gstd2 : float Standard deviation of gaussian kernel 2 gstd3 : float Standard deviation of gaussian kernel 3 ksize : int Size of kernel (should be odd) w : float Weighting factor Returns ------------------ img_noslp : (M,N) float ndarray Input image with SLP removed """ gf1 = cv2.getGaussianKernel(ksize, gstd1) gf2 = cv2.getGaussianKernel(ksize, gstd2) gf3 = cv2.getGaussianKernel(ksize, gstd3) sqrtimg = cv2.sqrt(img) p1 = cv2.sepFilter2D(sqrtimg, -1, gf1, gf1) p2 = cv2.sepFilter2D(sqrtimg, -1, gf2, gf2) maxarr = np.maximum(0, (p1 - p2) / p2) minarr = np.minimum(w * maxarr, 1) p = 1 - minarr nc = cv2.sepFilter2D(p, -1, gf3, gf3) + EPS output = cv2.sepFilter2D(p*sqrtimg, -1, gf3, gf3) output = (output / nc) ** 2 # Since input is sqrted return output
def gaussian_smooth2(image, sigma): ''' Do gaussian smoothing with sigma. @param image 2D array representing 2D image with layer for each color (RGB) @param sigma float, defining the width of the filter @return the smoothed image. ''' # filter = np.zeros_like(image) # determine the length of the filter filter_length = math.ceil(sigma*5) # make the length odd filter_length = 2*(int(filter_length)/2) +1 # create filter filter = cv2.getGaussianKernel(filter_length, sigma) # apply the same filter on both the X and Y axis result = cv2.sepFilter2D(image, -1, filter, filter) return result
# Declaring Sobel Separable Filters sobel_sep_kernel_x_1 = np.array([[ 1], [ 2], [ 1]]) sobel_sep_kernel_x_2 = np.array([[-1, 0, 1]]) sobel_sep_kernel_y_1 = np.array([[-1], [ 0], [ 1]]) sobel_sep_kernel_y_2 = np.array([[ 1, 2, 1]]) # Computing Gradient Image Gx in X direction grad_sep_x = cv2.sepFilter2D(img, -1, sobel_sep_kernel_x_2, sobel_sep_kernel_x_1) # Computing Gradient Image Gy in Y direction grad_sep_y = cv2.sepFilter2D(img, -1, sobel_sep_kernel_y_2, sobel_sep_kernel_y_1) # Generating magnitude G = √G x2 + G y2 from Gradient Images Gx and Gy grad_sep = np.rint(np.sqrt(np.power(grad_sep_x, 2)+ np.power(grad_sep_y, 2))).astype(int) # Ending clock for 1D convolution with Separable Sobel Filters end_1d = time.clock() - start_1d ###################################################################### # Part c : Printing results print("Time taken for 2D convolution with Sobel Filters : ") print(end_2d)
image = cv2.imread('lena_gray.jpg') #Sobel Separable Filter in x direction sobelx1 = np.array([[1,2,1]], dtype = np.float) #column sobelx2 = np.array([[-1,0,1]], dtype = np.float) #row #Sobel Separable Filter in y direction sobely1 = np.array([[-1,0,1]], dtype = np.float) #column sobely2 = np.array([[1,2,1]], dtype = np.float) #row #Start time timestart = time.clock() #Calculate gx and gy using Sobel (horizontal and vertical gradients) gx = cv2.sepFilter2D(image, -1, sobelx2, sobelx1) gy = cv2.sepFilter2D(image, -1, sobely2, sobely1) #Calculate the gradient magnitude g = np.sqrt(gx * gx + gy * gy) #Normalize output to fit the range 0-255 g *= 255.0 / np.max(g) #End time timeend = time.clock() - timestart print("1D Convolution with Sobel Separable Filters: ", timeend) #Display result
import numpy as np from matplotlib import pyplot as plt import time img = cv2.imread('lena_gray.png') kx1 = np.array(([1, 2, 1]),np.float32) ky1 = np.array(([-1,0,1]),np.float32) kx2 = np.array(([-1,0,1]),np.float32) ky2 = np.array(([1,2,1]),np.float32) #g = cv2.addWeighted(gx,1,gy,1,0) past = time.time() gx = cv2.sepFilter2D(img,-1,kx1,ky1) gy = cv2.sepFilter2D(img,-1,kx2,ky2) g = np.sqrt(np.square(gx) + np.square(gy)) now = time.time() print 'Time Elapsed : {0:.2f} msec(s)\n'.format((float)(now - past)*(1000.0)) fig = plt.figure('1D Convolution Example') fig.add_subplot(221) #top left fig.add_subplot(222) #top right fig.add_subplot(223) #bottom left fig.add_subplot(224) #bottom right
KernelSize = (5, 5) BoxFilterImg = cv2.boxFilter(src=Img, ddepth=-1, ksize=KernelSize, normalize=True) cv2.imshow('cvbox', BoxFilterImg) print np.equal(BlurImg, BoxFilterImg).all() cv2.waitKey() # ---------------------------- Gaussian Blur ---------------------------- # KernelSize = (15, 15) GaussianBlurImg = cv2.GaussianBlur(src=Img, ksize=KernelSize, sigmaX=0, sigmaY=0) # KernelSize = (5, 15) GaussianKernelX = cv2.getGaussianKernel(ksize=KernelSize[0], sigma=0) GaussianKernelY = cv2.getGaussianKernel(ksize=KernelSize[1], sigma=0) print 'GaussianKernelX:\n', GaussianKernelX print 'GaussianKernelY:\n', GaussianKernelY FilterGaussian = cv2.sepFilter2D(src=Img, ddepth=-1, kernelX=GaussianKernelX, kernelY=GaussianKernelY) cv2.imshow('Gauss', GaussianBlurImg) cv2.imshow('FilterGaussian', FilterGaussian) print np.equal(GaussianBlurImg, FilterGaussian).all() cv2.waitKey() # ---------------------------- Median Blur ---------------------------- # KernelSize = 5 MeanBlurImg = cv2.medianBlur(src=Img, ksize=KernelSize) cv2.imshow('MeanBlurImg', MeanBlurImg) cv2.waitKey() # ---------------------------- Bilateral Blur ---------------------------- # BilateralBlur = cv2.bilateralFilter(src=Img, d=9, sigmaColor=75, sigmaSpace=75) cv2.imshow('BilateralBlur', BilateralBlur) cv2.waitKey()
def detect(self, image, mask = None): if self.width != image.shape[1] or self.height != image.shape[0] or self.channels != image.shape[2]: self.seeds = None if self.num_superpixels != Configuration.sp_num_superpixels or self.prior != Configuration.sp_prior or self.num_levels != Configuration.sp_num_levels or self.num_histogram_bins != Configuration.sp_num_histogram_bins: self.seeds = None self.reset() if self.seeds is None: self.width = image.shape[1] self.height = image.shape[0] self.channels = image.shape[2] self.seeds = cv2.ximgproc.createSuperpixelSEEDS(self.width, self.height, self.channels, Configuration.sp_num_superpixels, Configuration.sp_num_levels, Configuration.sp_prior, Configuration.sp_num_histogram_bins) self.threshold = np.ones((self.height, self.width), np.float32) converted_img = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) self.seeds.iterate(converted_img, Configuration.sp_num_iterations) labels = self.seeds.getLabels() if mask is None: mask = np.zeros((image.shape[0], image.shape[1]), np.uint8) floatimage = np.float32(image) fb, fg, fr = cv2.split(floatimage) rb = fr - fb #rb = fr + fb + fg mi = np.min(rb[mask == 0]) ma = np.max(rb[mask == 0]) rb = np.uint8((rb - mi) / (ma - mi) * 255) #mimaTg = np.uint8((np.array([-15, 15]) - mi) / (ma - mi) * 255) Tg, _ = cv2.threshold(rb[mask == 0], 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU) #if Tg < mimaTg[0]: # Tg = mimaTg[0] #elif Tg > mimaTg[1]: # Tg = mimaTg[1] Tl = np.zeros(self.seeds.getNumberOfSuperpixels()) for i in range(self.seeds.getNumberOfSuperpixels()): sp = rb[(labels == i) & (mask == 0)] if sp.size == 0: self.threshold[labels == i] = Tg continue Lmax = np.max(sp) Lmin = np.min(sp) if Lmax < Tg: Tl[i] = Tg#Lmax elif Lmin > Tg: Tl[i] = Tg#Lmin else: Sl, _ = cv2.threshold(sp, 0, 1, cv2.THRESH_BINARY | cv2.THRESH_OTSU) Tl[i] = 0.5 * (Sl + Tg) self.threshold[labels == i] = Tl[i] if self.gaussian is None or self.gaussian.shape[0] != Configuration.sp_kernel_size: self.gaussian = cv2.getGaussianKernel(Configuration.sp_kernel_size, -1, cv2.CV_32F) self.threshold = cv2.sepFilter2D(self.threshold, cv2.CV_32F, self.gaussian, self.gaussian) return np.uint8((self.threshold - rb) <= 0)# * mask
import cv2 img = cv2.imread('./test/img/test.png') kernel = cv2.getGaussianKernel(3, 1) # grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # print(grey) dst = cv2.sepFilter2D(img, -1, kernel, kernel, borderType=cv2.BORDER_REFLECT) # dst = cv2.GaussianBlur(grey, ksize=(3, 3), sigmaX=1, sigmaY=1, # borderType=cv2.BORDER_REFLECT) # print(dst) cv2.imwrite('./test/img/testGaussianBlur.png', dst)
def applyMMSI(I1, filters, togglePolarity=False): """ Applies the filters to a given input image to compute the modified multiscale singularity index response. Estimate the width and the dominant orientation angle for each spatial location. Inputs: I1 -- input image (e.g. Landsat NIR band or MNDWI) filters -- an instance of SingularityIndexFilters class that contains precomputed filters Keyword arguments: togglePolarity -- changes polarity, use if the rivers are darker than land in the input image (i.e. SAR images) Returns: psi -- the singularity index response widthMap -- estimated width at each spatial location (x,y) orient -- local orientation at each spatial location (x,y) """ if I1.dtype == 'uint8': I1 = I1.astype('float')/255 if I1.dtype == 'uint16': I1 = I1.astype('float')/65535 if len(I1.shape) > 2: raise ValueError('This function inputs only a singe channel image') R, C = I1.shape # Compute the multiscale singularity index for s in range(0, filters.nrScales): print "Processing scale: " + str(s) # Downscale the image to the current scale. We use a pyramid instead of # increasing the sigma and size of the kernels for efficiency if s > 0: I1 = cv2.resize(I1, (int(C/(np.sqrt(2)**s)), int(R/(np.sqrt(2)**s))), \ interpolation = cv2.INTER_CUBIC) # Debias the image. mu = cv2.sepFilter2D(I1, cv2.CV_64FC1, filters.Gdebias, filters.Gdebias.T, \ borderType=cv2.BORDER_REFLECT_101) I = I1 - mu # Apply the second order derivative filters J20 = fftconvolve(I, filters.G20, mode='same') J260 = fftconvolve(I, filters.G260, mode='same') J2120 = fftconvolve(I, filters.G2120, mode='same') # Compute the dominant local orientation Nr = np.sqrt(3) * ( (J260**2) - (J2120**2) + (J20*J260) - (J20*J2120) ) Dr = 2*(J20**2) - (J260**2) - (J2120**2) + (J20*J260) - 2*(J260*J2120) + (J20*J2120) angles = np.arctan2(Nr,Dr) / 2 # Apply the first order derivative filters J0u = cv2.sepFilter2D(I, cv2.CV_64FC1, filters.G1.T, filters.G0_a.T, \ borderType=cv2.BORDER_REFLECT_101) J90u = cv2.sepFilter2D(I, cv2.CV_64FC1, filters.G0_a, filters.G1, \ borderType=cv2.BORDER_REFLECT_101) # Compute 0th, 1st, and 2nd derivatives along the estimated direction J0 = cv2.sepFilter2D(I, cv2.CV_64FC1, filters.G01d, filters.G01d.T, \ borderType=cv2.BORDER_REFLECT_101) J1 = J0u * np.cos(angles) + J90u * np.sin(angles) J2 =((1+(2*np.cos(2*angles)))*J20 + \ (1-np.cos(2*angles)+(np.sqrt(3)*np.sin(2*angles)))*J260 + \ (1-np.cos(2*angles)-(np.sqrt(3)*np.sin(2*angles)))*J2120) / 3 # Compute the singularity index for the current scale psi_scale = np.abs(J0)*J2 / ( 1 + J1**2 ) # Resize scale responses to the same size for element-wise comparison if s > 0: psi_scale = cv2.resize(psi_scale, (C, R), interpolation = cv2.INTER_CUBIC) angles = cv2.resize(angles, (C, R), interpolation = cv2.INTER_NEAREST) # Toggle polarity if needed if togglePolarity: psi_scale = -psi_scale # Suppress island response (channels have negative response unless the polarity is changed) psi_scale[psi_scale>0] = 0 psi_scale = np.abs(psi_scale) # Gamma normalize response psi_scale = psi_scale * filters.minScale**2 # Find the dominant scale, orientation, and norm of the response across scales if s == 0: # response buffer (we need the neighbors of the dominant scale for width estimation) psi_prev = np.zeros(psi_scale.shape) psi_curr = np.zeros(psi_scale.shape) psi_next = psi_scale # response at the dominant scale and its neighbors psi_max_curr = np.zeros(psi_scale.shape) psi_max_prev = np.zeros(psi_scale.shape) psi_max_next = np.zeros(psi_scale.shape) dominant_scale_idx = np.zeros(psi_scale.shape) orient = angles psi_max = psi_scale psi = psi_scale**2 else: psi_prev = psi_curr psi_curr = psi_next psi_next = psi_scale idx_curr = psi_curr > psi_max_curr psi_max_curr[idx_curr] = psi_curr[idx_curr] psi_max_prev[idx_curr] = psi_prev[idx_curr] psi_max_next[idx_curr] = psi_next[idx_curr] idx = psi_scale > psi_max psi_max[idx] = psi_scale[idx] dominant_scale_idx[idx] = s orient[idx] = angles[idx] psi = psi + psi_scale**2 # Check if the coarsest scale has the maximum response psi_prev = psi_curr psi_curr = psi_next idx_curr = psi_curr > psi_max_curr psi_max_curr[idx_curr] = psi_curr[idx_curr] psi_max_prev[idx_curr] = psi_prev[idx_curr] psi_max_next[idx_curr] = 0 # Euclidean norm of the response across scales psi = np.sqrt(psi) # Estimate the width by fitting a quadratic spline to the response at the # dominant scale and its neighbors s_prev = filters.minScale * (np.sqrt(2)**(dominant_scale_idx-1)) s_max = filters.minScale * (np.sqrt(2)**(dominant_scale_idx)) s_next = filters.minScale * (np.sqrt(2)**(dominant_scale_idx+1)) A = s_next * (psi_max - psi_max_prev) + \ s_max * (psi_max_prev - psi_max_next) + \ s_prev * (psi_max_next - psi_max) B = s_next*s_next * (psi_max_prev - psi_max) + \ s_max*s_max * (psi_max_next - psi_max_prev) + \ s_prev*s_prev * (psi_max - psi_max_next) widthMap = np.zeros(psi.shape) widthMap[psi>0] = -B[psi>0] / (2*A[psi>0]) # Scaling factor for narrow rivers scalingFactor = 2/(1+np.exp(-8*psi))-1 widthMap = scalingFactor * widthMap + 0.5 return psi, widthMap, orient
import cv2 import numpy as np sigma = 1 if __name__ == '__main__': img = cv2.imread("5.jpg", 0) cv2.imshow('raw', img) cv2.waitKey(1) # 空域平滑 # 高斯滤波 kernel = cv2.getGaussianKernel(5, sigma) Gaussian_img = cv2.sepFilter2D(img, -1, kernel, kernel) cv2.imwrite('Guassian.jpg', Gaussian_img) # cv2.imshow('Gaussian', Gaussian_img) cv2.waitKey(1) # 均值滤波 Normalized_img = cv2.blur(img, (5, 5)) cv2.imwrite('Normalized.jpg', Normalized_img) # cv2.imshow('Normalized', Normalized_img) cv2.waitKey(1) # 中值滤波 median = cv2.medianBlur(img, 5) cv2.imwrite('Median.jpg', median) # cv2.imshow('Median', median) cv2.waitKey(1) # 空域锐化
def canny(img, low, high): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) g = cv2.getGaussianKernel(5, -1) sobel1 =np.matrix([[-1,0,1], [-2,0,2], [-1,0,1]]) sobel2 =np.matrix([[1,2,1], [0,0,0], [-1,-2,-1]]) new_img = cv2.sepFilter2D(gray, -1, g, g) mag_x = cv2.filter2D(new_img, cv2.cv.CV_32F,sobel1) mag_y = cv2.filter2D(new_img, cv2.cv.CV_32F, sobel2) #mag_x = cv2.Sobel(new_img,cv2.CV_64F,1,0,ksize=5) #mag_y = cv2.Sobel(new_img,cv2.CV_64F,0,1,ksize=5) new_img = np.hypot(mag_x, mag_y) theta = np.arctan2(mag_y, mag_x) # save edge direction theta = ((theta) * 4.0 / np.pi + 0.5).astype('int') % 4 # convert edge direction into 0-3 p1 = np.pad(new_img,((1, 0),(1,0)), mode='edge')[:-1,:-1] p2 = np.pad(new_img,((1, 0),(0,0)), mode='edge')[:-1,:] p3 = np.pad(new_img,((1, 0),(0,1)), mode='edge')[:-1,1:] p4 = np.pad(new_img,((0, 0),(1,0)), mode='edge')[:,:-1] p5 = np.pad(new_img,((0, 0),(0,1)), mode='edge')[:,1:] p6 = np.pad(new_img,((0, 1),(1,0)), mode='edge')[1:,:-1] p7 = np.pad(new_img,((0, 1),(0,0)), mode='edge')[1:,:] p8 = np.pad(new_img,((0, 1),(0,1)), mode='edge')[1:,1:] total = np.dstack((new_img,p1, p2, p3, p4, p5, p6, p7, p8, theta)) edges = np.zeros(new_img.shape) for (i,j), v in np.ndenumerate(edges): item = total[i,j,:] if item[9] == 0: edges.itemset((i,j), item[0] > item[2] and item[0] > item[7]) elif item[9] == 1: edges.itemset((i,j), item[0] > item[3] and item[0] > item[6]) elif item[9] == 2: edges.itemset((i,j), item[0] > item[5] and item[0] > item[4]) elif item[9] == 3: edges.itemset((i,j), item[0] > item[8] and item[0] > item[1]) new_img = new_img * edges #dual thresholding weak = np.zeros(new_img.shape) strong = np.zeros(new_img.shape) widx = new_img > low sidx = new_img > high weak[widx] = 50 strong[sidx] = 255 dual = cv2.add(weak, strong) indices = np.argwhere(dual>=255) #hysteresis q = Queue.Queue() for pair in indices: q.put(pair) while not q.empty(): current = q.get() dual.itemset((current[0],current[1]), 255) #neighbors for n in neighbors(current[0], current[1]): if n[0] < 0 or n[0] > dual.shape[0]-1 or \ n[1] < 0 or n[1] > dual.shape[1]-1: continue else: #valid if dual.item(n[0],n[1]) == 200: dual.itemset((n[0],n[1]), 255) print n q.put(n) final = np.zeros(dual.shape, np.float32) fidx = dual == 255 final[fidx] = 255 return final
import cv2 import numpy as np img = cv2.imread('./test/img/test.png') kernelX = np.zeros((1, 3)) kernelX[0, 0] = 0.1 kernelX[0, 1] = 0.2 kernelX[0, 2] = 0.3 kernelY = np.zeros((1, 5)) kernelY[0, 0] = 0.4 kernelY[0, 1] = 0.5 kernelY[0, 2] = 0.6 kernelY[0, 3] = -0.3 kernelY[0, 4] = -0.4 dst = cv2.sepFilter2D(img, kernelX=kernelX, kernelY=kernelY, borderType=cv2.BORDER_REFLECT) cv2.imwrite('./test/img/testConv.png', dst)