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

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。
边栏推荐
- Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
- 【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
- C# 读取web上的xml
- Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
- useMemo模拟useCallback
- Pit encountered by pytorch: why can't l1loss decrease during model training?
- 【QT】Qt 5 的程序:打印文档
- C get the version number of exe - file version and assembly version
- [batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
- (tool class) use SecureCRT as the communication medium
猜你喜欢

Tempest HDMI leak receive 1

VectorDraw Web Library 10.10

Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver

China Mobile MCU product information

Genuine photoshop2022 purchase experience sharing

数据可视化没有重点怎么办?
![[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)](/img/2b/4495a6cd41a2dd4e7a20ee60b398c9.png)
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)

Chuantu microelectronics high speed and high performance rs-485/422 transceiver series

搞清信息化是什么,让企业转型升级走上正确的道路

smartBugs安装小问题总结
随机推荐
双三次差值bicubic
国外LEAD域名邮箱获取途径
What is the difference between norflash and nandflash
[batch dos-cmd command - summary and summary] - CMD extended command and function (CMD /e:on, CMD /e:off)
OpenMP入门
Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
个人域名和企业域名的区别
(tool class) quickly add time to code in source insight
MySQL face Scripture eight part essay
Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field
Four software 2021-10-14 suitable for beginners to draw PCB
SQL solve select basic statement
[Batch dos - cmd Command - Summary and Summary] - External Command - cmd Download Command, wget Command
C Getting Started tutorial
Cglib dynamic proxy
Ca-is1200u current detection isolation amplifier has been delivered in batch
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
Sichuan earth microelectronics 8-channel isolated digital input receiver
Genuine photoshop2022 purchase experience sharing
STL tutorial 4- input / output stream and object serialization