#coding=utf-8 import cv2 #cv2.__version__==3.2.0 import numpy as np #2D convolution: Blurring[1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 1/9 ] # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # rows, cols = img.shape[:2] # kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]]) # kernel_identity = np.array([[-1,0], [1,0]]) # kernel_3x3 = np.ones((3,3), np.float32) / 9.0 # kernel_5x5 = np.ones((5,5), np.float32) / 25.0 # cv2.imshow('Original', img) # output = cv2.filter2D(img, -1, kernel_identity) # cv2.imshow('Identity filter', output) # output = cv2.filter2D(img, -1, kernel_3x3) # cv2.imshow('3x3 filter', output) # output = cv2.filter2D(img, -1, kernel_5x5) # cv2.imshow('5x5 filter', output) # output = cv2.blur(img, (3,3)) # cv2.imshow('cv2.blur', output) # cv2.waitKey(0) #Edge detection horizontal edges[-1 0 1 -2 0 2 -1 0 1] vertical edges[-1 -2 -1 0 0 0 1 2 1] # img=cv2.imread('test1.jpg',cv2.IMREAD_GRAYSCALE) # rows, cols = img.shape # sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) # sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) # laplacian = cv2.Laplacian(img, cv2.CV_64F) # canny = cv2.Canny(img, 50, 240) # cv2.imshow('Original', img) # cv2.imshow('Sobel horizontal', sobel_horizontal) # cv2.imshow('Sobel vertical', sobel_vertical) # cv2.imshow('laplacian', laplacian) # cv2.imshow('canny', canny) # cv2.waitKey(0) #Motion blur # img=cv2.imread('test1.jpg',cv2.IMREAD_GRAYSCALE) # size = 15 # # generating the kernel # kernel_motion_blur = np.zeros((size, size)) # kernel_motion_blur[int((size-1)/2), :] = np.ones(size) # print(list(kernel_motion_blur)) # kernel_motion_blur = kernel_motion_blur / size # print() # print(list(kernel_motion_blur)) # # # applying the kernel to the input image # output = cv2.filter2D(img, -1, kernel_motion_blur) # cv2.imshow('Motion Blur', output) # cv2.waitKey(0) #Sharpening尖锐 # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # # generating the kernels # kernel_sharpen_1 = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) # kernel_sharpen_2 = np.array([[1,1,1], [1,-7,1], [1,1,1]]) # kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1], # [-1,2,2,2,-1], # [-1,2,8,2,-1], # [-1,2,2,2,-1], # [-1,-1,-1,-1,-1]]) / 8.0 # # applying different kernels to the input image # output_1 = cv2.filter2D(img, -1, kernel_sharpen_1) # output_2 = cv2.filter2D(img, -1, kernel_sharpen_2) # output_3 = cv2.filter2D(img, -1, kernel_sharpen_3) # cv2.imshow('Sharpening', output_1) # cv2.imshow('Excessive Sharpening', output_2) # cv2.imshow('Edge Enhancement', output_3) # cv2.waitKey(0) #Embossing浮雕 # img_emboss_input=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # # generating the kernels # kernel_emboss_1 = np.array([[0,-1,-1], # [1,0,-1], # [1,1,0]]) # kernel_emboss_2 = np.array([[-1,-1,0], # [-1,0,1], # [0,1,1]]) # kernel_emboss_3 = np.array([[1,0,0], # [0,0,0], # [0,0,-1]]) # # converting the image to grayscale # gray_img = cv2.cvtColor(img_emboss_input,cv2.COLOR_BGR2GRAY) # # applying the kernels to the grayscale image and adding the offset # output_1 = cv2.filter2D(gray_img, -1, kernel_emboss_1) + 128 # output_2 = cv2.filter2D(gray_img, -1, kernel_emboss_2) + 128 # output_3 = cv2.filter2D(gray_img, -1, kernel_emboss_3) + 128 # cv2.imshow('Input', img_emboss_input) # cv2.imshow('Embossing - South West', output_1) # cv2.imshow('Embossing - South East', output_2) # cv2.imshow('Embossing - North West', output_3) # cv2.waitKey(0) #Erosion腐蚀(变粗) and dilation膨胀 # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # kernel = np.ones((3,3), np.uint8) # img_erosion = cv2.erode(img, kernel, iterations=1) # img_dilation = cv2.dilate(img, kernel, iterations=1) # cv2.imshow('Input', img) # cv2.imshow('Erosion', img_erosion) # cv2.imshow('Dilation', img_dilation) # cv2.waitKey(0) #Enhancing the contrast加强对比度 # import matplotlib.pyplot as plt # #gray image # img=cv2.imread('test_color.tif',cv2.IMREAD_GRAYSCALE) # hist_cv = cv2.calcHist([img],[0],None,[256],[0,256]) # plt.subplot(221),plt.imshow(img,'gray') # plt.subplot(222),plt.plot(hist_cv) # # equalize the histogram of the input image # histeq = cv2.equalizeHist(img) # hist_cv1 = cv2.calcHist([histeq],[0],None,[256],[0,256]) # plt.subplot(223),plt.plot(hist_cv1) # plt.show() # cv2.imshow('Input', img) # cv2.imshow('Histogram equalized', histeq) # cv2.waitKey(0) #color image # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) # # equalize the histogram of the Y channel # img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) # # convert the YUV image back to RGB format # img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) # cv2.imshow('Color input image', img) # cv2.imshow('Histogram equalized', img_output) # cv2.waitKey(0)