当前位置:网站首页>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 .

原网站

版权声明
本文为[Coding leaves]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240628078893.html