【发布时间】:2020-04-02 08:19:31
【问题描述】:
我刚刚从这个博客学习了如何从头开始将高斯滤镜应用于 Python 中的灰度图像:
http://www.adeveloperdiary.com/data-science/computer-vision/applying-gaussian-smoothing-to-an-image-using-python-from-scratch/
现在我想将高斯滤波器应用于 3 通道 (RGB) 图像。
为此,我实现了代码,但我得到的输出是一个模糊的沉闷图像,具有非常低亮度。 另外,图像的边缘没有正确模糊。
这是我的代码:
# import libraries
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
%matplotlib inline
import cv2
# loading image
img_orig = cv2.imread('home.jpg')
# convert GBR image to RGB image
img_orig = cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB)
# Gaussian function
def dnorm(x, mu, sd):
return 1 / (np.sqrt(2 * np.pi) * sd) * np.exp(-((x-mu)/sd)** 2 / 2)
# function for making gaussian kernel
def gaussian_kernel(kernel_size, mu = 0):
# initializing mu and SD
sd = np.sqrt(kernel_size)
# creating 1D kernel
kernel_1D = np.linspace(-(kernel_size // 2), kernel_size // 2, kernel_size)
# normalizing 1D kernel
for i in range(kernel_size):
kernel_1D[i] = dnorm(kernel_1D[i], mu, sd)
# creating 2D kernel
kernel_2D = np.outer(kernel_1D, kernel_1D)
kernel_2D /= kernel_2D.max()
return kernel_2D
# Covolution function with zero padding
def convolution(image, kernel):
# find row and column of 3 channel (RGB) image
img_row, img_col, img_channel = image.shape
kernel_size = kernel.shape[0]
padding_width = (kernel_size - 1) // 2
#initialize output image
output = np.zeros(image.shape, dtype = np.uint8)
# initialize padded image with zeros
padded_img = np.zeros((img_row + 2*padding_width, img_col + 2*padding_width, img_channel), dtype = np.uint8)
# copy orignal image inside padded image
padded_img[padding_width : padding_width + img_row, padding_width : padding_width + img_col] = image
# average pixel values using gaussian kernel
for i in range(img_row):
for j in range(img_col):
# average each pixel's R channel value
output[i, j, 0] = np.sum(padded_img[i : i+kernel_size, j : j+kernel_size, 0] * kernel) // (kernel_size * kernel_size)
# average each pixel's G channel value
output[i, j, 1] = np.sum(padded_img[i : i+kernel_size, j : j+kernel_size, 1] * kernel) // (kernel_size * kernel_size)
# average each pixel's B channel value
output[i, j, 2] = np.sum(padded_img[i : i+kernel_size, j : j+kernel_size, 2] * kernel) // (kernel_size * kernel_size)
return output
def gaussian_filter(image, kernel_size = 3):
# initialize mu
mu = 0
# create gaussian kernel
kernel = gaussian_kernel(kernel_size, mu)
# apply convolution to image
conv_img = convolution(image, kernel)
# return blurred image
return conv_img
高斯滤波器测试代码:
plt.figure(figsize = (7, 5))
print('orignal image')
plt.imshow(img_orig)
plt.show()
plt.figure(figsize = (7, 5))
print('blurred image')
plt.imshow(gaussian_filter(img_orig, 11))
plt.show()
与openCV GaussianBlur对比:
print('openCV blurred image')
plt.imshow(cv2.GaussianBlur(img_orig, (11,11), 0))
plt.show()
我的问题是:
1)为什么我得到一个沉闷的图像作为输出。
2) RGB 图像的高斯滤波器的上述实现是否错误?如果写错了怎么改?
3)为什么边缘没有正确模糊(看到边缘的黑色阴影)?
4) 与 OpenCV GaussianBlur 相比,高斯滤波器的上述实现需要很长时间才能执行。我怎样才能让它变得高效?
【问题讨论】:
标签: python opencv image-processing computer-vision gaussianblur