Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit faedc37

Browse files
committed
update ch16
1 parent 051b551 commit faedc37

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

‎ch16-图像平衡/16.filter2D.py‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
# -*- coding: utf-8 -*-
22

3+
'''
4+
2D 卷积
5+
OpenCV 提供的函数 cv.filter2D() 可以 我们对一幅图像 卷积操
6+
作。
7+
操作如下 将核放在图像的一个像素 A 上 求与核对应的图像上 25 5x5 个像素的和 在取平均数 用 个平均数替代像素 A 的值。 复以上操作直到 将图像的每一个像素值 更新一 。
8+
'''
9+
310
import cv2
411
import numpy as np
512
from matplotlib import pyplot as plt
6-
img = cv2.imread('opencv_logo.png')
7-
kernel = np.ones((5,5),np.float32)/25
8-
#cv.Filter2D(src, dst, kernel, anchor=(-1, -1))
9-
#ddepth –desired depth of the destination image;
10-
#if it is negative, it will be the same as src.depth();
11-
#the following combinations of src.depth() and ddepth are supported:
12-
#src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
13-
#src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
14-
#src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
15-
#src.depth() = CV_64F, ddepth = -1/CV_64F
16-
#when ddepth=-1, the output image will have the same depth as the source.
1713

18-
dst = cv2.filter2D(img,-1,kernel)
19-
plt.subplot(121),plt.imshow(img),plt.title('Original')
14+
img = cv2.imread('../data/opencv_logo.png')
15+
kernel = np.ones((5, 5), np.float32) / 25
16+
# cv.Filter2D(src, dst, kernel, anchor=(-1, -1))
17+
# ddepth –desired depth of the destination image;
18+
# if it is negative, it will be the same as src.depth();
19+
# the following combinations of src.depth() and ddepth are supported:
20+
# src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
21+
# src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
22+
# src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
23+
# src.depth() = CV_64F, ddepth = -1/CV_64F
24+
# when ddepth=-1, the output image will have the same depth as the source.
25+
26+
dst = cv2.filter2D(img, -1, kernel)
27+
28+
plt.subplot(121), plt.imshow(img), plt.title('Original')
2029
plt.xticks([]), plt.yticks([])
21-
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
30+
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
2231
plt.xticks([]), plt.yticks([])
2332

24-
plt.show()
33+
plt.show()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月12日 下午1:35
3+
# @Author : play4fun
4+
# @File : 图像模糊-平均.py
5+
# @Software: PyCharm
6+
7+
"""
8+
图像模糊-平均.py:
9+
是由一个归一化卷积框完成的。他只是用卷积框 盖区域所有像素的平 均值来代替中心元素。可以使用函数 cv2.blur() 和 cv2.boxFilter() 来完 个任务。
10+
"""
11+
12+
import cv2
13+
import numpy as np
14+
from matplotlib import pyplot as plt
15+
16+
img = cv2.imread('../data/opencv_logo.png')
17+
# blur = cv2.blur(img, (5, 5))
18+
19+
'''
20+
现在把卷积核换成 斯核 简单来 方框不变 将原来每个方框的值是 相等的 现在 的值是符合 斯分布的 方框中心的值最大 其余方框根据 离中心元素的 离 减 构成一个 斯小山包。原来的求平均数现在变成求 加权平均数 全就是方框 的值 。
21+
'''
22+
# 0 是指根据窗口大小 5,5 来计算高斯函数标准差
23+
blur = cv2.GaussianBlur(img, (5, 5), 0) # 高斯模糊
24+
25+
'''
26+
名思义就是用与卷积框对应像素的中值来替代中心像素的值。 个滤波 器经常用来去 椒盐噪声。前 的滤波器 是用 算得到的一个新值来取代中 心像素的值 而中值滤波是用中心像素周围 也可以使他本 的值来取代他。 他能有效的去 噪声。卷积核的大小也应 是一个奇数。
27+
'''
28+
median = cv2.medianBlur(img, 5) # 中值模糊
29+
30+
'''
31+
函数 cv2.bilateralFilter() 能在保持边界清晰的情况下有效的去 噪 。
32+
但是 种操作与其他滤波器相比会比 慢。
33+
我们已经知 高斯滤波器是求 中心点 邻近区域像素的高斯加权平均值。
34+
种 斯滤波器只考虑像素之间的空间关系
35+
而不会考虑像素值之间的关系 ,像素的相似度 。
36+
所以 种方法不会考 虑 一个像素是否位于边界。
37+
因此边界也会被模糊掉 而 这正不是我们想要。
38+
39+
双边滤波在同时使用空 高斯权重和灰度值相似性 斯权 。
40+
空 高斯函数确保只有邻近区域的像素对中心点有影响
41+
灰度值相似性高斯函数确保只有与中心像素灰度值相近的才会被用来做模糊运算。
42+
所以 种方法会确保边界不会被模糊掉
43+
因为边界处的灰度值变化比较大。
44+
'''
45+
46+
# cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace)
47+
# d – Diameter of each pixel neighborhood that is used during filtering. # If it is non-positive, it is computed from sigmaSpace
48+
# 9 域直径 两个 75 分别是空 斯函数标准差 灰度值相似性 斯函数标准差
49+
blur = cv2.bilateralFilter(img, 9, 75, 75)
50+
51+
plt.subplot(121), plt.imshow(img), plt.title('Original')
52+
plt.xticks([]), plt.yticks([])
53+
plt.subplot(122), plt.imshow(blur), plt.title('Blurred')
54+
plt.xticks([]), plt.yticks([])
55+
plt.show()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /