当前位置:网站首页>中值滤波器
中值滤波器
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()

边栏推荐
- First knowledge of opencv4.x --- image perspective transformation
- [paper reading] raw+:a two view graph propagation method with word coupling for readability assessment
- GOM登录器配置免费版生成图文教程
- UE4 smart pointer and weak pointer
- 中部“第一城”,长沙“人才引力”从争先到领先
- Use of URL download resources
- Learning basic knowledge of Android security
- C# Winfrom 常用功能整合
- 1对1直播源码——1对1语音聊天源码
- Mysql-04 storage engine and data type
猜你喜欢

Seata deployment and microservice integration

当AI邂逅生命健康,华为云为他们搭建三座桥

31. Opinion-based Relational Pivoting forCross-domain Aspect Term Extraction 阅读笔记

C language_ Structure pointer variable introduction

C# Winfrom 常用功能整合

填问卷,领奖品 | 诚邀您填写 Google Play Academy 活动调研问卷

键盘快捷键操作电脑(自己遇到不会的)

C# 常用功能整合

14. Bridge-Based Active Domain Adaptation for Aspect Term Extraction 阅读笔记

Embedded development: skills of debugging embedded software
随机推荐
Flink SQL (III) connects to the external system system and JDBC
GOM登录器配置免费版生成图文教程
基于标签嵌入注意力机制的多任务文本分类模型
14. Bridge-Based Active Domain Adaptation for Aspect Term Extraction 阅读笔记
Low power multi-channel wfas1431 wireless data acquisition and transmission instrument operation process description
Arithmetic operation and logic operation of image in opencv
SP export map to Maya
Leetcode question type priority queue (TOPK question)
First knowledge of opencv4.x --- image perspective transformation
My meeting of OA project
Plato Farm有望通过Elephant Swap,进一步向外拓展生态
嵌入式开发:调试嵌入式软件的技巧
Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
How to evaluate the test quality?
请问下大家,flink sql有没有办法不输出update_before?
Basic syntax of MySQL DDL and DML and DQL
基于多任务深度学习的实体和事件联合抽取模型
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)
Seata deployment and microservice integration
MySQL-04 存储引擎和数据类型