import numpy as np

#反谐波均值滤波
def contraharmonic_mean_filter(img, kernel_size=3, Q=1.5):
    img = img.astype(np.float64)
    filtered_img = np.zeros_like(img)

    k = kernel_size // 2
    height, width = img.shape

    for i in range(k, height - k):
        for j in range(k, width - k):
            window = img[i - k:i + k + 1, j - k:j + k + 1]
            numerator = np.power(window, Q + 1).sum()
            denominator = np.power(window, Q).sum()

            if denominator != 0:
                filtered_img[i, j] = numerator / denominator
            else:
                filtered_img[i, j] = img[i, j]

    return np.clip(filtered_img, 0, 255).astype(np.uint8)