我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用cv2.fastNlMeansDenoising()。
def apply_filters(self, image, denoise=False): """ This method is used to apply required filters to the to extracted regions of interest. Every square in a sudoku square is considered to be a region of interest, since it can potentially contain a value. """ # Convert to grayscale source_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Denoise the grayscale image if requested in the params if denoise: denoised_gray = cv2.fastNlMeansDenoising(source_gray, None, 9, 13) source_blur = cv2.GaussianBlur(denoised_gray, BLUR_KERNEL_SIZE, 3) # source_blur = denoised_gray else: source_blur = cv2.GaussianBlur(source_gray, (3, 3), 3) source_thresh = cv2.adaptiveThreshold(source_blur, 255, 0, 1, 5, 2) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) source_eroded = cv2.erode(source_thresh, kernel, iterations=1) source_dilated = cv2.dilate(source_eroded, kernel, iterations=1) if ENABLE_PREVIEW_ALL: image_preview(source_dilated) return source_dilated
def process_image(img = list()): """ Extracts faces from the image using haar cascade, resizes and applies filters. :param img: image matrix. Must be grayscale ::returns faces:: list contatining the cropped face images """ face_cascade = cv2.CascadeClassifier('/Users/mehul/opencv-3.0.0/build/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml') faces_location = face_cascade.detectMultiScale(img, 1.3, 5) faces = [] for (x,y,w,h) in faces_location: img = img[y:(y+h), x:(x+w)] try: img = cv2.resize(img, (256, 256)) except: exit(1) img = cv2.bilateralFilter(img,15,10,10) img = cv2.fastNlMeansDenoising(img,None,4,7,21) faces.append(img) return faces
def EdgeDetection(img): img = cv2.fastNlMeansDenoising(img,None,3,7,21) _,img = cv2.threshold(img,30,255,cv2.THRESH_TOZERO) denoise_img = img laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) # y canny = cv2.Canny(img,100,200) contour_image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) return {"denoise":denoise_img,"laplacian":laplacian,"canny":canny,"sobely":sobely,"sobelx":sobelx,"contour":contour_image} # GrayScale Image Convertor # https://extr3metech.wordpress.com
def MyDenoiseSobely(path): img_gray = ToGrayImage(path) img_mydenoise = MyDenoise(img_gray,5) img_denoise = cv2.fastNlMeansDenoising(img_mydenoise,None,3,7,21) _,img_thre = cv2.threshold(img_denoise,100,255,cv2.THRESH_TOZERO) sobely = cv2.Sobel(img_thre,cv2.CV_64F,0,1,ksize=3) return sobely
def create_dataset(file_paths, label_set, with_denoising=False): data_x = [] data_y = [] for path in file_paths: single_x = np.asarray(PIL.Image.open(path)).flatten() # Denoise image with help of OpenCV (increase time of computing). if with_denoising: single_x = cv2.fastNlMeansDenoising(single_x).flatten() data_x.append(single_x) for l in label_set: l_to_num = char_to_num(l) data_y.append(l_to_num) np_data_x = np.array(data_x) np_data_y = np.array(data_y) return np_data_x, np_data_y # Use the Keras data generator to augment data.
def non_local_means_bw_py(imgs, search_window, block_size, photo_render): import cv2 ret_imgs = opencv_wrapper(imgs, cv2.fastNlMeansDenoising, [None,photo_render,block_size,search_window]) return ret_imgs
def EdgeDetection(img): # img = cv2.medianBlur(img,5) img = cv2.fastNlMeansDenoising(img,None,3,7,21) _,img = cv2.threshold(img,30,255,cv2.THRESH_TOZERO) denoise_img = img # print(img) # cv2.imwrite("Denoise.jpg",img) # cv2.waitKey(0) # cv2.destroyAllWindows() # convolute with proper kernels laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # x sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) # y # sobel2y = cv2.Sobel(sobely,cv2.CV_64F,0,1,ksize=3) # sobelxy = cv2.Sobel(img,cv2.CV_64F,1,1,ksize=5) # y canny = cv2.Canny(img,100,200) contour_image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # print(canny) # cv2.imwrite('laplacian.jpg',laplacian) # cv2.imwrite('sobelx.jpg',sobelx) # cv2.imwrite('sobely.jpg',sobely) # cv2.imwrite('sobelxy.jpg',sobelxy) # cv2.imwrite('canny.jpg',canny) # plt.subplot(3,2,1),plt.imshow(img,cmap = 'gray') # plt.title('Original'), plt.xticks([]), plt.yticks([]) # plt.subplot(3,2,2),plt.imshow(laplacian,cmap = 'gray') # plt.title('Laplacian'), plt.xticks([]), plt.yticks([]) # plt.subplot(3,2,3),plt.imshow(sobelx,cmap = 'gray') # plt.title('Sobel X'), plt.xticks([]), plt.yticks([]) # plt.subplot(3,2,4),plt.imshow(sobely,cmap = 'gray') # plt.title('Sobel Y'), plt.xticks([]), plt.yticks([]) # plt.subplot(3,2,4),plt.imshow(sobelxy,cmap = 'gray') # plt.title('Sobel XY'), plt.xticks([]), plt.yticks([]) # plt.subplot(3,2,5),plt.imshow(canny,cmap = 'gray') # plt.title('Canny'), plt.xticks([]), plt.yticks([]) # plt.show() # return {"denoise":img} return {"denoise":denoise_img,"laplacian":laplacian,"canny":canny,"sobely":sobely,"sobelx":sobelx,"contour":contour_image}