高斯滤镜只会使图像变暗,而不会模糊

时间:2019-04-02 15:13:52

标签: python opencv image-processing filtering

我想了解高斯滤波器的实现。在这段代码中,我实现了一个高斯内核,并将其应用于带有filter2D的图像。但是,我得到的不是黑暗的图像,而是更暗的(对于高方差值)和非常明亮的(对于低方差值)。我为内核尝试了一个固定的np.ones(3,3)/2,它成功地使图像模糊。可能是什么问题?

import cv2
import math
import numpy as np

filename = "lenna.png"
winname = "G Team Lets Go"

original = cv2.imread(filename)
cv2.namedWindow(winname)


def gaussian_blurr(image, variance, kernel_size):

    def gaussian(x, y):
        return (1 / (2 * np.pi * variance) * np.exp(-1 * ((math.pow(x, 2) + math.pow(y, 2)) / (2 * variance))))

    kernel = np.zeros((kernel_size, kernel_size))

    for x in range(-int(kernel_size/2), kernel_size-int(kernel_size/2)):
        for y in range(-int(kernel_size/2), kernel_size-int(kernel_size/2)):
            kernel[x+int(kernel_size/2), y+int(kernel_size/2)] = gaussian(x, y)

    padding_size = int(kernel_size/2)
    withBorder = cv2.copyMakeBorder(
        image, padding_size, padding_size, padding_size, padding_size, cv2.BORDER_REPLICATE)

    return cv2.filter2D(image, -1, kernel)


variance = 1
size = 3


def handle_variance(value):
    if value == 0:
        value = 1
    variance = value/100
    cv2.imshow(winname, gaussian_blurr(original, variance, size))


def handle_size(value):
    if value == 0:
        value = 1
    size = value
    cv2.imshow(winname, gaussian_blurr(original, variance, size))


cv2.createTrackbar("Variance", winname, 0, 500, handle_variance)
cv2.createTrackbar("Size", winname, 0, 5, handle_size)

cv2.imshow(winname, gaussian_blurr(original, 0.2, 3))
cv2.waitKey(0)

0 个答案:

没有答案