当前位置:网站首页>opencv最小值滤波(不局限于图像)
opencv最小值滤波(不局限于图像)
2022-06-25 06:40:00 【Coding的叶子】
opencv中有较多滤波函数,如中值滤波等,但没有最大值和最小值滤波。本文将介绍用python numpy实现最小值滤波功能,可以说是不需要借助opencv即可实现。
1 定义函数minBlur
定义最小值滤波的函数为minBlur,包含3个参数,分别如下所示:
(1)image:输入图像,array类型。
(2)kernel:最小值范围,tuple类型,第一个元素表示x方向取最小值的范围,第二个元素表示y方向取最小值的范围。
(3)limit:需要最小值滤波的元素,tuple类型,如(a,b)表示像素值范围在[a, b]的像素才进行最小值滤波。
该函数做到了以下几个兼容性:
(1)适合灰度图片或者RGB图片。
(2)不局限于图像的数组,array。
(3)适合任意数量的通道数。
2 参考程序
import numpy as np
def minBlur(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.min(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] = 255
y = minBlur(x)
print('x:\n', x)
print('y:\n', y)
3 测试结果
更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。
边栏推荐
- 一次弄清楚 Handler 可能导致的内存泄漏和解决办法
- 对链表进行插入排序[dummy统一操作+断链核心--被动节点]
- C#入门教程
- ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
- Manufacturing process of PCB 2021-10-11
- 【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
- SQL solve select basic statement
- [distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
- 国外LEAD域名邮箱获取途径
- CPDA | how to start the growth path of data analysts?
猜你喜欢
VectorDraw Web Library 10.10
realsense d455 semantic_ Slam implements semantic octree mapping
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
三年营收连续下滑,天地壹号困在醋饮料里
PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
搞清信息化是什么,让企业转型升级走上正确的道路
Home environment monitoring system design (PC version) (mobile app version to be determined)
随机推荐
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
Summary of small problems in smartbugs installation
VectorDraw Web Library 10.10
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
差点被这波Handler 面试连环炮带走~
Four software 2021-10-14 suitable for beginners to draw PCB
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
el-input实现尾部加字
Tempest HDMI leak receive 1
Distributed quorum NWR of the alchemy furnace of the Supreme Master
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
C reads XML on the web
El input to add words to the tail
Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
个人域名和企业域名的区别
RTKLIB-b33版本中GALILEO广播星历存储问题
LeetCode 每日一题——515. 在每个树行中找最大值