Esempio n. 1
0
def CrossWindowMedianFilter(src_img, winSize):
    if winSize % 2 == 0 or winSize == 1:
        print('winSize Error! such as:3, 5, 7, 9....')
        return None
    elif winSize == 3:
        CrossWindows = np.array([[0, 1, 0],
                                 [1, 1, 1],
                                 [0, 1, 0]])
        print(CrossWindows)
    elif winSize == 5:
        CrossWindows = np.array([[0, 0, 1, 0, 0],
                                 [0, 0, 1, 0, 0],
                                 [1, 1, 1, 1, 1],
                                 [0, 0, 1, 0, 0],
                                 [0, 0, 1, 0, 0]])
        print(CrossWindows)
    elif winSize == 7:
        CrossWindows = np.array([[0, 0, 0, 1, 0, 0, 0],
                                 [0, 0, 0, 1, 0, 0, 0],
                                 [0, 0, 0, 1, 0, 0, 0],
                                 [1, 1, 1, 1, 1, 1, 1],
                                 [0, 0, 0, 1, 0, 0, 0],
                                 [0, 0, 0, 1, 0, 0, 0],
                                 [0, 0, 0, 1, 0, 0, 0]])
        print(CrossWindows)
    elif winSize == 9:
        CrossWindows = np.array([[0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [1, 1, 1, 1, 1, 1, 1, 1, 1],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0],
                                 [0, 0, 0, 0, 1, 0, 0, 0, 0]])
        print(CrossWindows)
    paddingSize = winSize // 2
    height, width = src_img.shape
    matBase = np.zeros((height + paddingSize * 2, width + paddingSize * 2), dtype=src_img.dtype)
    matBase[paddingSize:-paddingSize, paddingSize:-paddingSize] = src_img
    for r in range(paddingSize):
        matBase[r, paddingSize:-paddingSize] = src_img[0, :]
        matBase[-(1 + r), paddingSize:-paddingSize] = src_img[-1, :]
        matBase[paddingSize:-paddingSize, r] = src_img[:, 0]
        matBase[paddingSize:-paddingSize, -(1 + r)] = src_img[:, -1]
    matOut = np.zeros((height, width), dtype=src_img.dtype)
    for x in range(height):
        for y in range(width):
            line = matBase[x:x + winSize, y:y + winSize]
            ret = Sort.MatrixMultiplication(line, CrossWindows)
            lst = Sort.RemoveZero(ret)
            lst = sorted(lst)
            if lst is None:
                matOut[x, y] = src_img[x, y]
            elif len(lst) == 1:
                matOut[x, y] = lst[0]
            elif len(lst) == 2:
                matOut[x, y] = lst[1]
            elif len(lst) == 3:
                matOut[x, y] = lst[1]
            elif len(lst) == 4:
                matOut[x, y] = lst[1]
            elif len(lst) == 5:
                matOut[x, y] = lst[2]
            elif len(lst) == 6:
                matOut[x, y] = lst[2]
            elif len(lst) == 7:
                matOut[x, y] = lst[3]
            elif len(lst) == 8:
                matOut[x, y] = lst[3]
            elif len(lst) == 9:
                matOut[x, y] = lst[4]
    return matOut
Esempio n. 2
0
# _*_ coding: UTF-8 _*_
# 2020/5/6 9:10 
# PyCharm  
# Create by:LIUMINXUAN
# E-mail:[email protected]
import cv2 as cv
import numpy as np
import Sort
test = np.array([[6, 8, 7],
                 [3, 4, 15],
                 [1, 1, 1]])
CrossWindows = np.array([[0, 1, 0],
                        [1, 1, 1],
                        [0, 1, 0]])
ret = Sort.MatrixMultiplication(test, CrossWindows)
print(test)
print(CrossWindows)
print(ret)
ret_1 = Sort.RemoveZero(ret)
print(ret_1)