当前位置:网站首页>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 测试结果

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。
边栏推荐
- QT source code analysis -- QObject (2)
- Win11 blank when using VIM to view content in cmder
- api平台通用签名机制
- Application of tidb in Netease games
- Easydss anonymous live channel data volume instability optimization scheme sharing
- 原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
- 图片工具
- QT writing security video monitoring system 36 onvif continuous movement
- ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
- Distributed | how to make "secret calls" with dble
猜你喜欢

Pymysql inserts data into MySQL and reports an error for no reason

ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis

Distributed | how to make "secret calls" with dble

中国芯片独角兽公司

Why can ping fail while traceroute can

表单图片上传在Chorme中无法查看请求体的二进制图片信息

JUC personal simple notes

Mysql数据(Liunx环境)定时备份

Qt 中发送自定义事件

K8S部署高可用postgresql集群 —— 筑梦之路
随机推荐
所说的Get post:请求的区别,你真的知道了吗??????
There was an error checking the latest version of pip
中国芯片独角兽公司
Earthly 容器镜像构建工具 —— 筑梦之路
获取屏幕宽高工具类
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
js中通过key查找和更新对象中指定值的方法
Xtrabackup for data backup
dataX使用指南
小程序wx.show
小黑ai4code代码baseline啃食1
提高INSERT速度
Shell array
Qt源码分析--QObject(2)
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
MySQL 因字符集问题插入中文数据时提示代码 :1366
Background management of uniapp hot update
Picture tools
Get screen width and height tool class
Redis的Cluster集群数据倾斜