1.运动模糊
import cv2
import numpy as np
img = cv2.imread('try.jpg',cv2.IMREAD_COLOR)
cv2.imshow('original',img)
size =15
#generating the kernel
kernel_motion_blur = np.zeros((size,size))
#水平运动模糊
kernel_motion_blur[ int((size-1)/2),: ] = np.ones(size)
#垂直运动模糊
# kernel_motion_blur[ int((size-1)/2),: ] = np.ones(size)
kernel_motion_blur =kernel_motion_blur /size
#applying the kernel to the input image
output = cv2.filter2D(img,-1,kernel_motion_blur)
cv2.namedWindow('Motion Blur',cv2.WINDOW_NORMAL)
cv2.imshow('Motion Blur',output)
cv2.waitKey()
2.锐化
import cv2
import numpy as np
img = cv2.imread('try.jpg')
cv2.imshow('Original', img)
# generating the kernels
#kernel总值为1
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)
3.浮雕
import cv2
import numpy as np
img_emboss_input = cv2.imread('try.jpg')
# 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)
4.腐蚀和膨胀
import cv2
import numpy as np
img = cv2.imread('try.jpg')
kernel = np.ones((5,5),np.uint8)
#参数3是迭代次数
img_erosion = cv2.erode(img,kernel,iterations=1)
img_dilation = cv2.dilate(img,kernel,iterations=1)
cv2.namedWindow('input',cv2.WINDOW_NORMAL)
cv2.imshow('input',img)
cv2.namedWindow('erosion',cv2.WINDOW_NORMAL)
cv2.imshow('erosion',img_erosion)
cv2.namedWindow('dilae',cv2.WINDOW_NORMAL)
cv2.imshow('dilae',img_dilation)
cv2.waitKey()
5.晕影
#晕影过滤器
import cv2
import numpy as np
img = cv2.imread('try.jpg',cv2.IMREAD_COLOR)
# print(img.shape[:2])
rows,cols = img.shape[:2]
#generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(cols,200)
kernel_y = cv2.getGaussianKernel(rows,200)
# .T转置矩阵
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
output = np.copy(img)
#applying the mask to each channel in the input image
for i in range(3):
output[:,:,i] = output[:,:,i] * mask
cv2.imshow('Original',img)
cv2.imshow('Vignette',output)
cv2.waitKey()
we want to focus on a different region in the image, as shown in the following figure.
import cv2
import numpy as np
img = cv2.imread('try.jpg')
rows, cols = img.shape[:2]
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(int(1.5*cols),200)
kernel_y = cv2.getGaussianKernel(int(1.5*rows),200)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)
mask = mask[int(0.5*rows):, int(0.5*cols):]
output = np.copy(img)
# applying the mask to each channel in the input image
for i in range(3):
output[:,:,i] = output[:,:,i] * mask
cv2.imshow('Input', img)
cv2.imshow('Vignette with shifted focus', output)
cv2.waitKey(0)
6.直方图均衡--增加对比度(灰度图)
import cv2
import numpy as np
img = cv2.imread('try.jpg', 0)
# equalize the histogram of the input image
histeq = cv2.equalizeHist(img)
cv2.imshow('Input', img)
cv2.imshow('Histogram equalized', histeq)
cv2.waitKey(0)
7.直方图均衡--增强对比度(彩色图)
import cv2
import numpy as np
img = cv2.imread('try.jpg')
img_yuv = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
#equalize the histogram of the Y channle 直方图均衡Y通道
img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0])
#conver the yuv image back to RGM format 将YUV颜色模型转换为RGB
img_output = cv2.cvtColor(img_yuv,cv2.COLOR_YUV2BGR)
cv2.imshow('input',img)
cv2.imshow('Historgram equalized',img_output)
cv2.waitKey()