Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Median filter

The median filter is easy to say, opencV comes with and functions can be used directly. In OpenCV, the function that implements median filtering is cv2.medianBlur(SRC, ksize).

  • SRC: image position
  • Kszie: convolution kernel size
import cv2 as cv

img = cv.imread('d:/Desktop/1.jpg')  # I have a 1.jpg on my desktop
res = cv.medianBlur(img, 5)
cv.imshow("original", img)
cv.imshow("result", res)
cv.waitKey(0)
Copy the code

The median filter will take the pixel values of the current pixel and its neighboring pixels (a total of an odd number of pixels), sort these pixel values, and then take the pixel value in the middle as the pixel value of the current pixel.

Its neighborhood set to 3 x 3 size, the 3 x 3 sort of pixels neighborhood pixels within the ascending and descending order (can), according to the ascending order after get the sequence value is:,78,90,91,93,94,95,97,101 [66]. In this sequence, the value at the center (also known as the center point or median point) is “93”, so the original pixel value 78 is replaced by this value as the new pixel value for the current point, resulting in the following processing.

Max/min filter

The principle of the median filter, as we saw earlier, is that a max-min filter is a filter that changes the median to a maximum or a minimum.

Opencv does not have its own Max/min filter, so you need to write their own, define the function code as follows:

def original(i, j, k, ksize, img) :
    # Find the matrix coordinates
    x1 = y1 = -ksize // 2
    x2 = y2 = ksize + x1
    temp = np.zeros(ksize * ksize)
    count = 0
    # Process images
    for m in range(x1, x2):
        for n in range(y1, y2):
            if i + m < 0 or i + m > img.shape[0] - 1 or j + n < 0 or j + n > img.shape[1] - 1:
                temp[count] = img[i, j, k]
            else:
                temp[count] = img[i + m, j + n, k]
            count += 1
    return temp

# Custom maximum filter minimum filter
def max_min_functin(ksize, img, flag) :
    img0 = copy.copy(img)
    for i in range(0, img.shape[0) :for j in range(2, img.shape[1) :for k in range(img.shape[2]):
                temp = original(i, j, k, ksize, img0)
                if flag == 0:   # set the flag parameter to detect the maximum value if 0 and the minimum value if 1.
                    img[i, j, k] = np.max(temp)
                elif flag == 1:
                    img[i, j, k] = np.min(temp)
    return img
Copy the code

Note that the code above is fairly simple, and only accepts the convolution kernel as a square matrix.

In practice:

import cv2 as cv
import numpy as np
import copy

def original(i, j, k, ksize, img) :
    # Find the matrix coordinates
    x1 = y1 = -ksize // 2
    x2 = y2 = ksize + x1
    temp = np.zeros(ksize * ksize)
    count = 0
    # Process images
    for m in range(x1, x2):
        for n in range(y1, y2):
            if i + m < 0 or i + m > img.shape[0] - 1 or j + n < 0 or j + n > img.shape[1] - 1:
                temp[count] = img[i, j, k]
            else:
                temp[count] = img[i + m, j + n, k]
            count += 1
    return temp

# Custom maximum filter minimum filter
def max_min_functin(ksize, img, flag) :
    img0 = copy.copy(img)
    for i in range(0, img.shape[0) :for j in range(2, img.shape[1) :for k in range(img.shape[2]):
                temp = original(i, j, k, ksize, img0)
                if flag == 0:
                    img[i, j, k] = np.max(temp)
                elif flag == 1:
                    img[i, j, k] = np.min(temp)
    return img

img = cv.imread('d:/Desktop/1.png')
min_img = max_min_functin(3, copy.copy(img),1)
max_img = max_min_functin(3, copy.copy(img),0)
cv.imshow("original", img)
cv.imshow("min_img", min_img)
cv.imshow("max_img", max_img)
cv.waitKey(0)
Copy the code

It can be seen that the maximum filter can remove the dark spots in the image, but also make the bright spots increase (swell). Minimum filtering can remove bright spots in an image and also increase dark spots (corrosion).