当前位置:网站首页>图像处理一百题(11-20)
图像处理一百题(11-20)
2022-07-06 09:29:00 【狗蛋儿l】
问题十一:均值滤波器
使用3*3的均值滤波器来进行滤波吧!
平滑线性空间滤波器的输出是包含在滤波器模板邻域内的像素的简单平均值,也就是均值滤波器。均值滤波器也是低通滤波器,均值滤波器很容易理解,即把邻域内的平均值赋给中心元素。
均值滤波器用来降低噪声,均值滤波器的主要应用是去除图像中的不相关细节,不相关是指与滤波器的模板相比较小的像素区域。模糊图片以便得到感兴趣物体的粗略描述,因此那些较小的物体的灰度就会与背景混合在一起,较大的物体则变的像斑点而易于检测。模板的大小由那些即将融入背景中的物体尺寸决定。
import cv2
import numpy as np
# mean filter
def mean_filter(img, K_size=3):
H, W, C = img.shape
# zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
tmp = out.copy()
# filtering
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.mean(tmp[y: y + K_size, x: x + K_size, c])
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
return out
# Read image
img = cv2.imread("jiaoyan.jpg")
# Mean Filter
out = mean_filter(img, K_size=3)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
原图像
处理后图像
问题十二:Motion Filter
Motion Filter取对角线方向的像素的平均值,像下式这样定义:
import cv2
import numpy as np
# motion filter
def motion_filter(img, K_size=3):
H, W, C = img.shape
# Kernel
K = np.diag( [1] * K_size ).astype(np.float)
K /= K_size
# zero padding
pad = K_size // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)
tmp = out.copy()
# filtering
for y in range(H):
for x in range(W):
for c in range(C):
out[pad + y, pad + x, c] = np.sum(K * tmp[y: y + K_size, x: x + K_size, c])
out = out[pad: pad + H, pad: pad + W].astype(np.uint8)
return out
# Read image
img = cv2.imread("jiaoyan.jpg")
# motion filtering
out = motion_filter(img, K_size=3)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
边栏推荐
- Share an example of running dash application in raspberry pie.
- 日期加1天
- Codeforces Round #801 (Div. 2)A~C
- 969. Pancake sorting
- sublime text 代码格式化操作
- QT实现圆角窗口
- Research Report on market supply and demand and strategy of Chinese table lamp industry
- Acwing - game 55 of the week
- (POJ - 3186) treatments for the cows (interval DP)
- Maximum product (greedy)
猜你喜欢
随机推荐
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
AcWing:第56场周赛
Problem - 1646C. Factorials and Powers of Two - Codeforces
QWidget代码设置样式表探讨
Acwing: Game 58 of the week
Spark的RDD(弹性分布式数据集)返回大结果集
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
Generate random password / verification code
Calculate the time difference
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
Educational Codeforces Round 130 (Rated for Div. 2)A~C
875. Leetcode, a banana lover
Spark独立集群Worker和Executor的概念
1605. Sum the feasible matrix for a given row and column
Market trend report, technological innovation and market forecast of desktop electric tools in China
Research Report on market supply and demand and strategy of Chinese table lamp industry
860. Lemonade change
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Codeforces Round #801 (Div. 2)A~C
Codeforces - 1526C1&&C2 - Potions