'Image Mask to apply image filters

I want to calculate variance, gabor and entropy filters to some images, but the images have blank areas that I don´t want to apply the filters. I try to use a np.ma.array option but return this error: "'MaskedArray' object is not callable"

this is the code:

def bandas_img (image, array1, array2):
    imagenRGB = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    return cv2.inRange(imagenRGB, array1, array2)

def rescale_by_width(image, target_width, method=cv2.INTER_LANCZOS4):
    h = int(round(target_width * image.shape[0] / image.shape[1]))
    return cv2.resize(image, (target_width, h), interpolation=method)

#Resized image by width
target_width = 400

#To mask null values
mask_image = True

hue = 20
sat = 57
value = 116
toleranciaH = 150
toleranciaS = 150
toleranciaV = 150

lower = np.array ([hue - toleranciaH, sat - toleranciaS, value - toleranciaV])
upper = np.array ([hue + toleranciaH, sat + toleranciaS, value + toleranciaV])

#working directory where the csv files are
os.chdir("C:/Users/Mariano/Documents/3 - Visual studio code/Prueba filtrar mascara/filtrada")        ##ojoooo las barras van /// y no  D:/OMAN/BHI TEXTURES/U-2

file_extension = '.png'  #Check Extension  
all_filenames = [i for i in glob.glob(f"*{file_extension}")]

for f in all_filenames:
    image = cv2.imread(f,1)
    #resized Image
    resized1 = rescale_by_width(image, target_width)
    #Set f value (image name)
    f = f.replace(".png", "")
    #Save Image
    plt.imsave(f+"_resized.png", resized1)
    #Create mask for null values
    if mask_image == True:
        mask = bandas_img(resized1, lower, upper)
        cv2.imwrite(f+"_mask.png", mask)
        resized2 = io.imread(f+"_resized.png", as_gray=True)
        resized3 = resized2.copy()
        #First Try
        resized3[mask == 0] = np.nan        
        resized3[mask != 0] = resized2[mask != 0]
        #Second Try
        mask1 = (resized3 == np.nan)      
        resized_Mask = np.ma.array(resized3, mask = mask1)
    #Varianza
    k=6
    img_mean = ndimage.uniform_filter(resized_Mask, (k, k))
    img_sqr_mean = ndimage.uniform_filter(resized_Mask**2, (k, k))
    img_var = img_sqr_mean - img_mean**2
    img_var[mask == 0] = 1
    plt.imsave(f+"_varianza.png", img_var)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source