我正在嘗試為 GaussianBlur 制作內核,但我不知道該怎么做。我試過這個,但它會引發錯誤。
import cv2 as cv
import numpy as np
img = cv.imread('lena_gray.bmp')
x = np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]])
#x2 = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) I also need the ability to use other types of kernels like this one
img_rst = cv.GaussianBlur(img, kernel=x)
cv.imwrite('result.jpg', img_rst)
是否可以更改內核?我一直在尋找解決方案,但沒有結果。
uj5u.com熱心網友回復:
您已經混合了 Opencv 的內置高斯模糊方法和自定義內核過濾方法。讓我們看看下面的兩種方法:
先加載原圖
import cv2
from matplotlib import pyplot as plt
import numpy as np
img_path = 'Lena.png'
image = plt.imread(img_path)
plt.imshow(image)

如果你想使用內置方法,那么你不需要自己制作內核。只需定義其大小(以下大小為 11x11):
img_blur = cv2.blur(src=image, ksize=(11,11))
plt.imshow(img_blur)

如果你想使用自己的內核。使用filter2D方法:
blur_kernel = np.ones((11, 11), np.float32) / 121
kernel_blurred = cv2.filter2D(src=image, ddepth=-1, kernel=blur_kernel)
plt.imshow(kernel_blurred)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/344525.html
標籤:Python 图片 opencv 图像处理 计算机视觉
上一篇:如何反編譯微信小程式👻
