文章目录
- 图像滤波
- 卷积相关概念
- 锚点
- 实战图像卷积
- Blur an image with a 2d convolution matrix
- 方盒滤波与均值滤波
- 高斯滤波
- 中值滤波
- 双边滤波
- 高通滤波—索贝尔算子
- 高通滤波—沙尔算子
- 高通滤波—拉普拉斯算子
- 边缘检测Canny
图像滤波
卷积核=滤波器
卷积相关概念
锚点
锚点就是卷积核所对应的图像中间的点,比方说是3x3的卷积核,那对应的锚点可以是16
实战图像卷积
低通滤波:低于某个阀值滤波可以通过
高通滤波:高于某个阀值滤波可以通过
Blur an image with a 2d convolution matrix
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//10.jpg')kernel = np.ones((5, 5), np.float32) / 25
dst = cv2.filter2D(img, -1, kernel)cv2.imshow('dst', dst)
cv2.imshow('img', img)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
方盒滤波与均值滤波
这两种滤波API功能基本一样,一般我们用blur这个
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('E://pic//10.jpg')# kernel = np.ones((5, 5), np.float32) / 25
# dst = cv2.filter2D(img, -1, kernel)
dst = cv2.blur(img, (5, 5))cv2.imshow('dst', dst)
cv2.imshow('img', img)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
高斯滤波
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./gaussian.png')# kernel = np.ones((5, 5), np.float32) / 25
# dst = cv2.filter2D(img, -1, kernel)
dst = cv2.GaussianBlur(img, (5, 5), sigmaX=1)cv2.imshow('dst', dst)
cv2.imshow('img', img)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
中值滤波
中值滤波的优点是对胡椒噪音效果明显
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./papper.png')# kernel = np.ones((5, 5), np.float32) / 25
# dst = cv2.filter2D(img, -1, kernel)
dst = cv2.medianBlur(img, 5)cv2.imshow('dst', dst)
cv2.imshow('img', img)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
双边滤波
可以进行美颜
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./bieber.jpeg')# kernel = np.ones((5, 5), np.float32) / 25
# dst = cv2.filter2D(img, -1, kernel)
# dst = cv2.medianBlur(img, 5)
dst = cv2.bilateralFilter(img, 7, 20, 50)cv2.imshow('dst', dst)
cv2.imshow('img', img)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
高通滤波—索贝尔算子
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./chess.png')# 索贝尔算子y方向边缘
d1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
# 索贝尔算子x方向边缘
d2 = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)# dst = d1 + d2
dst = cv2.add(d1, d2)cv2.imshow('img', img)
cv2.imshow('d1', d1)
cv2.imshow('d2', d2)
cv2.imshow('dst', dst)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
高通滤波—沙尔算子
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./chess.png')# 索贝尔算子y方向边缘
# d1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)d1 = cv2.Scharr(img, cv2.CV_64F, 1, 0)
# 索贝尔算子x方向边缘
# d2 = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)d2 = cv2.Scharr(img, cv2.CV_64F, 0, 1)# dst = d1 + d2
dst = cv2.add(d1, d2)cv2.imshow('img', img)
cv2.imshow('d1', d1)
cv2.imshow('d2', d2)
cv2.imshow('dst', dst)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
高通滤波—拉普拉斯算子
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./chess.png')# 索贝尔算子y方向边缘
# d1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
# 沙尔算子
# d1 = cv2.Scharr(img, cv2.CV_64F, 1, 0)
# 索贝尔算子x方向边缘
# d2 = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
# 沙尔算子
# d2 = cv2.Scharr(img, cv2.CV_64F, 0, 1)# dst = d1 + d2
# dst = cv2.add(d1, d2)# 拉普拉斯
ldst = cv2.Laplacian(img, cv2.CV_64F, ksize=5)cv2.imshow('img', img)
# cv2.imshow('d1', d1)
# cv2.imshow('d2', d2)
cv2.imshow('dst', ldst)key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
边缘检测Canny
超过最大值肯定是边缘,低于最小值肯定不是边缘,而介于最大值和最小值之间,如果和超出最大值A是连续的,则C也是边缘,而B就不是边缘
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = cv2.imread('./lena.png')
dst = cv2.Canny(img, 100, 200)cv2.imshow("img", img)
cv2.imshow("dst", dst)
key = cv2.waitKey(0) & 0xff
if key == ord('q'):cv2.destroyAllWindows()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!