2020-12-08 10:11:20

# Wuhan University proposed ARGAN： Attention cycle generated confrontation model is used to detect 、 Remove image shadows | ICCV 2019

https://blog.csdn.net/weixin_42137700/article/details/103211491

Here's the traditional maximum filter , Minimum filtering method ：

python Relatively slow , A picture takes a few seconds , Output graph ,opencv You can't show, The effect is a little bit

2. When deleting shadows , There are two things to pay attention to . Because the image is a grayscale image , If the background of the image is light and the object is dark , Then the maximum filtering must be performed first , Then the minimum filtering is performed . If the background of the image is dark and the object is bright , We can do the minimum filtering first , And then we do the maximum filtering .

that , What are the maximum and minimum filters ？

3. Maximum filtering ： Let's assume we have a certain size image I. The algorithm we write should traverse one by one I The pixel , And for each pixel （x,y）, It has to find the neighborhood around the pixel （ The size is N x N The window of ） The maximum gray value in , And write A The corresponding pixel position in the （x,y） Maximum gray value of . The resulting image A It's called an input image I Maximum filtered image of . Now let's implement this concept in code .

• max_filtering（） Function accepts input image and window size N.

• It initially creates a... Around the input array “ wall ”（ with -1 Fill of ）, It helps when we traverse the edge pixels .

• then , We create a “ temp” Variable , Copy the calculated maximum value into it .

• then , We iterate through the array and surround it with a size of N x N Create a window with the current pixel of .

• then , We use “ amax（）” Function to calculate the maximum value in this window , And write the value to temp Array .

• We copy the temporary array to the main array A in , And return it as output .

• A It's input I Maximum filtered image of .

``def max_filtering(N, I_temp):``    wall = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1)``    wall[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)] = I_temp.copy()``    temp = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1)``    for y in range(0,wall.shape[0]):``        for x in range(0,wall.shape[1]):``            if wall[y,x]!=-1:``                window = wall[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1]``                num = np.amax(window)``                temp[y,x] = num``    A = temp[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)].copy()``    return A``

4. Minimum filtering ： This algorithm is exactly the same as the maximum filter , But we didn't find the maximum gray value nearby , It's around the pixel N x N Minimum value found in neighborhood , And write the minimum gray value to B Medium （x,y）. The resulting image B It's called an image I The minimum filtered image of , The code is as follows .

``def min_filtering(N, A):``    wall_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300)``    wall_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)] = A.copy()``    temp_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300)``    for y in range(0,wall_min.shape[0]):``        for x in range(0,wall_min.shape[1]):``            if wall_min[y,x]!=300:``                window_min = wall_min[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1]``                num_min = np.amin(window_min)``                temp_min[y,x] = num_min``    B = temp_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)].copy()``    return B``

5. therefore , If the background of the image is shallow , We're going to do maximum filtering first , This will provide us with an enhanced background , And pass the maximum filtered image to the minimum filtering function , This feature will be responsible for the actual content enhancement .

6. therefore , Minimum execution - After maximum filtering , The value we get is not in 0-255 Within the scope of . therefore , We have to normalize the final array obtained using background subtraction , The method is to reduce the original image to the minimum - Maximum filtered image , To get the final image of the shadow removed .

``#B is the filtered image and I is the original image``def background_subtraction(I, B):``    O = I - B``    norm_img = cv2.normalize(O, None, 0,255, norm_type=cv2.NORM_MINMAX)``    return norm_img``

7. Variable N（ Window size for filtering ） Changes will be made based on the size of the particles or content in the image . For test images , Choose the size N = 20. The final output image after enhancement is as follows ：

``````# -*- coding: UTF-8 -*-
import cv2
import numpy as np
import os

#%%

import cv2
import numpy as np
import matplotlib.pyplot as plt

#%%

def max_filtering(N, I_temp):
wall = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1)
wall[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)] = I_temp.copy()
temp = np.full((I_temp.shape[0]+(N//2)*2, I_temp.shape[1]+(N//2)*2), -1)
for y in range(0,wall.shape[0]):
for x in range(0,wall.shape[1]):
if wall[y,x]!=-1:
window = wall[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1]
num = np.amax(window)
temp[y,x] = num
A = temp[(N//2):wall.shape[0]-(N//2), (N//2):wall.shape[1]-(N//2)].copy()
return A

def min_filtering(N, A):
wall_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300)
wall_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)] = A.copy()
temp_min = np.full((A.shape[0]+(N//2)*2, A.shape[1]+(N//2)*2), 300)
for y in range(0,wall_min.shape[0]):
for x in range(0,wall_min.shape[1]):
if wall_min[y,x]!=300:
window_min = wall_min[y-(N//2):y+(N//2)+1,x-(N//2):x+(N//2)+1]
num_min = np.amin(window_min)
temp_min[y,x] = num_min
B = temp_min[(N//2):wall_min.shape[0]-(N//2), (N//2):wall_min.shape[1]-(N//2)].copy()
return B

def background_subtraction(I, B):
O = I - B
norm_img = cv2.normalize(O, None, 0,255, norm_type=cv2.NORM_MINMAX)
return norm_img

def min_max_filtering(M, N, I):
if M == 0:
#max_filtering
A = max_filtering(N, I)
#min_filtering
B = min_filtering(N, A)
#subtraction
normalised_img = background_subtraction(I, B)
elif M == 1:
#min_filtering
A = min_filtering(N, I)
#max_filtering
B = max_filtering(N, A)
#subtraction
normalised_img = background_subtraction(I, B)
return normalised_img

cv2.imshow("asdf",P)

O_P = min_max_filtering(M = 0, N = 20, I = P)

cv2.imshow("O_P",O_P)
cv2.waitKey()

``````

https://chowdera.com/2020/12/202012081011025129.html