当前位置:网站首页>中值滤波器
中值滤波器
2022-07-26 14:28:00 【为饭带盐】
中值滤波器属于非线性滤波器,中值滤波是对整幅图像求解中位数的过程。
import cv2 as cv
import matplotlib.pyplot as plt
import math
import numpy as np
plt.rcParams['font.sans-serif'] =['SimHei'] #显示中文标签
def get_median(data):
data.sort()
half = len(data) // 2
return data[half]
#计算灰度图像的中值滤波
def my_median_blur_gray(image, size):
data = []
sizepart = int(size/2)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
for ii in range(size):
for jj in range(size):
#首先判断所以是否超出范围,也可以事先对图像进行零填充
if (i+ii-sizepart)<0 or (i+ii-sizepart)>=image.shape[0]:
pass
elif (j+jj-sizepart)<0 or (j+jj-sizepart)>=image.shape[1]:
pass
else:
data.append(image[i+ii-sizepart][j+jj-sizepart])
#取每个区域内的中位数
image[i][j] = int(get_median(data))
data=[]
return image
def my_median_blur_RGB(image, size):
(b ,r, g) = cv.split(image)
blur_b = my_median_blur_gray(b, size)
blur_r = my_median_blur_gray(r, size)
blur_g = my_median_blur_gray(g, size)
result = cv.merge((blur_b, blur_r, blur_g))
return result
if __name__ == '__main__':
image_test1 = cv.imread('apic27858.jpg')
#调用自定义函数
my_image_blur_median = my_median_blur_RGB(image_test1, 5)
#调用库函数
computer_image_blur_median = cv.medianBlur(image_test1, 5)
fig = plt.figure()
fig.add_subplot(131)
plt.title('原图')
plt.imshow(image_test1)
fig.add_subplot(132)
plt.title('自定义函数滤波')
plt.imshow(my_image_blur_median)
fig.add_subplot(133)
plt.title('库函数滤波')
plt.imshow(computer_image_blur_median)
plt.show()

边栏推荐
- How to evaluate the test quality?
- Unity学习笔记–无限地图
- Sqldeveloper tools quick start
- C# NanUI 相关功能整合
- 基于用户画像的在线健康社区用户流失预测研究
- What is the problem of the time series database that has been developed for 5 years?
- Jzoffer51- reverse pairs in the array (merge sort solution)
- GOM登录器配置免费版生成图文教程
- TransC知识表示模型
- C language_ Combination of structure and array
猜你喜欢

关于存储芯片的入门基础知识

研发了 5 年的时序数据库,到底要解决什么问题?

MySQL-03 数据库操作

基于用户画像的在线健康社区用户流失预测研究

Some lightweight network models in detection and segmentation (share your own learning notes)

MLX90640 红外热成像仪测温传感器模块开发笔记(六)

Use cpolar to build a commercial website (apply for website security certificate)

Basic syntax of MySQL DDL and DML and DQL

Embedded development: skills of debugging embedded software
Network pictures are transferred locally, causing the kernel to exit
随机推荐
[paper reading] raw+:a two view graph propagation method with word coupling for readability assessment
What is the problem of the time series database that has been developed for 5 years?
Seata的部署与微服务集成
初识Opencv4.X----图像透视变换
How to evaluate the test quality?
Tips for unity transparent channel
Leetcode1170- compare the occurrence frequency of the minimum letter of the string (the corresponding occurrence frequency of each string minimum element in the map set storage array)
智能家居行业发展,密切关注边缘计算和小程序容器技术
如何做 APP 升级测试 ?
填问卷,领奖品 | 诚邀您填写 Google Play Academy 活动调研问卷
Meeting seating and submission for approval of OA project
@A thousand lines of work, ride the cloud together!
Use cpolar to build a commercial website (apply for website security certificate)
【干货】MySQL索引背后的数据结构及算法原理
How to do app upgrade test?
【1.2.投资的收益和风险】
TransC知识表示模型
MySQL-03 数据库操作
Disease knowledge discovery based on spo semantic triples
Seata deployment and microservice integration