当前位置:网站首页>opencv最大值滤波(不局限于图像)
opencv最大值滤波(不局限于图像)
2022-06-24 07:07:00 【Coding的叶子】
opencv中有较多滤波函数,如中值滤波等,但没有最大值和最小值滤波。本文将介绍用python numpy实现最大值滤波功能,可以说是不需要借助opencv即可实现。
1 定义函数maxBlur
定义最大值滤波的函数为maxBlur,包含3个参数,分别如下所示:
(1)image:输入图像,array类型。
(2)kernel:最大值范围,tuple类型,第一个元素表示x方向取最大值的范围,第二个元素表示y方向取最大值的范围。
(3)limit:需要最大值滤波的元素,tuple类型,如(a,b)表示像素值范围在[a, b]的像素才进行最大值滤波。
该函数做到了以下几个兼容性:
(1)适合灰度图片或者RGB图片。
(2)不局限于图像的数组,array。
(3)适合任意数量的通道数。
2 参考程序
# -*- coding: utf-8 -*-
"""
乐乐感知学堂公众号
@author: https://blog.csdn.net/suiyingy
"""
import numpy as np
def maxBlur(image, kernel=(3, 3), limit=(0, 255)):
"""
Parameters
----------
image : array, 输入矩阵或数组.
kernel : tuple or list, optional
分别为x、y方向上的最大值取值区间范围. The default is (3, 3).
limit : tuple or list, optional
指定进行最大值滤波的像素范围. The default is (0, 255).
Returns
-------
image_c : array,处理后矩阵或数组。
"""
image_c = image.copy()
if len(image_c.shape) == 2:
image_c = image_c[:, :, np.newaxis]
h, w, c = image_c.shape
image_c1 = image_c.copy()
for i in range(h):
for j in range(w):
x1 = max(j-kernel[0]//2, 0)
x2 = min(x1 + kernel[0], w)
y1 = max(i-kernel[1]//2, 0)
y2 = min(y1 + kernel[1], h)
for k in range(c):
if image_c[i, j, k] >= limit[0] and image_c[i, j, k] <= limit[1]:
sub_img = image_c1[y1:y2, x1:x2, k]
image_c[i, j, k] = np.max(sub_img)
if len(image.shape) == 2:
image_c = image_c.reshape(h, w)
return image_c
if __name__ == '__main__':
np.random.seed(1)
x = np.random.randint(0, 256, (10, 10))
x[x<150] = 0
y = maxBlur(x)
print('x:\n', x)
print('y:\n', y)
3 测试结果

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。
边栏推荐
- One development skill a day: how to establish P2P communication based on webrtc?
- [team management] 25 tips for testing team performance management
- K8S部署高可用postgresql集群 —— 筑梦之路
- QT source code analysis -- QObject (2)
- Use cpulimit to free up your CPU
- Common misconceptions in Tencent conference API - signature error_ code 200003
- Earthly container image construction tool -- the road to dream
- Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
- api平台通用签名机制
- xargs使用技巧 —— 筑梦之路
猜你喜欢

【关于运维和网工的差别,一文说透】

原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

Liunx Mysql安装

pymysql 向MySQL 插入数据无故报错

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!

一文详解|增长那些事儿

Xiaohei ai4code code baseline nibble 1
随机推荐
Building a static website with eleventy
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
Matlab求解线性方程组Ax=b
RuntimeError: Missing dependencies:XXX
K8S部署高可用postgresql集群 —— 筑梦之路
所说的Get post:请求的区别,你真的知道了吗??????
JUC personal simple notes
提高INSERT速度
WebRTC系列-网络传输之5选择最优connection切换
Rescue system -- the application of read-write separation
Using sonar for code checking
Shell basic operators -- relational operators
À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL
Xtrabackup for data backup
利用sonar做代码检查
rpiplay实现树莓派AirPlay投屏器
2022春招面试总结
關於ETL看這篇文章就够了,三分鐘讓你明白什麼是ETL
为什么ping不通,而traceroute却可以通
IIS build wordpress5.7 manually