【问题标题】:How to demonstrate the Gaussian Kernal used in opencv GausssianBlurr, using the instruction `GaussianBlur()` from OpenCV?如何使用 OpenCV 的指令 `GaussianBlur()` 演示 opencv GaussianBlur 中使用的高斯内核?
【发布时间】:2020-11-04 19:11:26
【问题描述】:
  • 我想演示 openCV 中使用的 Gaussian Kernel。 cv2.GaussianBlurr(img, kernel_size, sigma) 用于解释目的。
  • 我知道如何演示应用模糊后生成的图像,而这不是我的目标。
  • 我的目标是针对任何使用的 sigma 和任何使用的内核大小自动演示内核!
  • 我看过一段代码(下面提到了),但我更喜欢使用与 OpenCV 中使用的指令更相关的东西,而不仅仅是一般的数学相关方法。
  • 预期的输出内核是这样的:

import cv2
import numpy as np

# Read Image
img_path = 'image.jpg'
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Gaussian Blurr
Kernel = np.ones((15,15))
sigma = 2

Blurred_Image = cv2.GaussianBlur(img, (Kernel.shape[0], Kernel.shape[1]), sigma)

高斯内核手册代码:

def dnorm(x, mu, sd):
    return 1 / (np.sqrt(2 * np.pi) * sd) * np.e ** (-np.power((x - mu) / sd, 2) / 2)

def gaussian_kernel(size, sigma=1, verbose=False):
 
    kernel_1D = np.linspace(-(size // 2), size // 2, size)
    for i in range(size):
        kernel_1D[i] = dnorm(kernel_1D[i], 0, sigma)
    kernel_2D = np.outer(kernel_1D.T, kernel_1D.T)
 
    kernel_2D *= 1.0 / kernel_2D.max()
 
    if verbose:
        plt.imshow(kernel_2D, interpolation='none',cmap='gray')
        plt.title("Image")
        plt.show()
 
    return kernel_2D

【问题讨论】:

  • 你似乎知道你必须做的一切。还有什么?
  • @YvesDaoust 我不喜欢这个手动演示内核的过程,我更喜欢直接从 OpenCV 的模糊定义中导出内核。
  • 什么叫手册?
  • @YvesDaoust 我用了manual这个词来描述在没有任何连接到OpenCV库和指令cv2.GaussianBlur()的结果图像的情况下使用的数学方法@

标签: python opencv image-processing gaussianblur


【解决方案1】:

这是 Python/OpenCV 中的一种方式。

 - Read the input
 - Create a delta image (one white pixel in the center of a black background)
 - Blur the image
 - List item
 - Resize the image to enlarge it
 - Stretch the image to full dynamic range
 - Save the result

import cv2
import numpy as np
import skimage.exposure as exposure

# create delta image
dims = 30
dims2 = 30 // 2
delta = np.zeros((dims,dims,3), dtype=np.float32)
delta[dims2:dims2+1, dims2:dims2+1] = (255,255,255)

# blur image
blur = cv2.GaussianBlur(delta, (0,0), sigmaX=5, sigmaY=5)

# resize 16x
dims4x = dims * 16
resized = cv2.resize(blur, (dims4x,dims4x), interpolation = cv2.INTER_AREA)

# stretch to full dynamic range
result = exposure.rescale_intensity(resized, in_range='image', out_range=(0,255)).astype(np.uint8)

# save image
cv2.imwrite('delta.png',delta)
cv2.imwrite('gaussian_blur_view.png',result)

# show the images
cv2.imshow("delta", delta)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

增量图像:

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 2012-06-30
    • 2013-02-18
    相关资源
    最近更新 更多