当前位置:网站首页>Opencv maximum filtering (not limited to images)
Opencv maximum filtering (not limited to images)
2022-06-24 08:52:00 【Coding leaves】
opencv There are many filter functions in , Such as median filtering , But there is no maximum and minimum filtering . This article will introduce how to use python numpy Realize the maximum filtering function , It can be said that there is no need to resort to opencv That is to say .
1 Defined function maxBlur
The function that defines the maximum filter is maxBlur, contain 3 Parameters , They are as follows :
(1)image: The input image ,array type .
(2)kernel: Maximum range ,tuple type , The first element represents x The range in which the direction takes the maximum value , The second element represents y The range in which the direction takes the maximum value .
(3)limit: Elements that require maximum filtering ,tuple type , Such as (a,b) Indicates that the range of pixel values is [a, b] The maximum value filtering is performed only for pixels of .
This function achieves the following compatibility :
(1) Suitable for grayscale pictures or RGB picture .
(2) Not limited to arrays of images ,array.
(3) Suitable for any number of channels .
2 Reference procedure
# -*- coding: utf-8 -*-
"""
The official account of Lele perception school
@author: https://blog.csdn.net/suiyingy
"""
import numpy as np
def maxBlur(image, kernel=(3, 3), limit=(0, 255)):
"""
Parameters
----------
image : array, Input matrix or array .
kernel : tuple or list, optional
Respectively x、y The range of the maximum value in the direction . The default is (3, 3).
limit : tuple or list, optional
Specifies the pixel range for maximum filtering . The default is (0, 255).
Returns
-------
image_c : array, Processed matrix or 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 test result
More 3D 、 Please pay attention to two-dimensional perception algorithm and financial quantitative analysis algorithm “ Lele perception school ” WeChat official account , And will continue to update .
边栏推荐
猜你喜欢
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
[team management] 25 tips for testing team performance management
opencv最大值滤波(不局限于图像)
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
Liunx Mysql安装
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
数据中台:中台架构及概述
疫情、失业,2022,我们高喊着摆烂和躺平!
What is graph neural network? Figure what is the use of neural networks?
随机推荐
Using skills of xargs -- the way to build a dream
【牛客】把字符串转换成整数
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
双指针模拟
Increase insert speed
MySQL | store notes of Master Kong MySQL from introduction to advanced
GradScaler MaxClipGradScaler
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
4275. Dijkstra序列
第七章 操作位和位串(三)
A tip to read on Medium for free
工具类
不能改变虚拟机电源状态报错解决方案
用VNC Viewer的方式远程连接无需显示屏的树莓派
What is the future development trend of Business Intelligence BI
Send custom events in QT
2021-05-20computed和watch应用与区别
1704. 判断字符串的两半是否相似
SLAM14讲中Sophus包的安装问题
The pie chart with dimension lines can set various parameter options