[英]Masking a grayscale Image with a 2D Gaussian Mask fails
这似乎很简单,但是我没有得到期望的结果。 有人可以向我解释原因吗? 我有下面的代码来生成2D高斯蒙版,其均值在中心和sigma为32x32像素图像的图像高度的1/3,如下所示:
def gauss2D(image):
x,y = image.shape[:2]
shape = (x,y)
sigma = 1/3 * min(x,y)
m,n = [(ss-1.)/2. for ss in shape]
y,x = np.ogrid[-m:m+1,-n:n+1]
h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
h = h / h.max()
return h
以下是我要使用的蒙版图像:
我用来屏蔽图像的代码如下:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
msk = gauss2D(gray)
masked_data = gray * msk
结果图像是这样的:
一个空白的白色图像,有时在角落显示一些不同的图像。
我还尝试了一点与并应用蒙版,但我不断收到似乎无法修复的错误:
res = cv2.bitwise_and(gray, gray, mask = msk)
cv2.error:OpenCV(3.4.1)/Users/travis/build/skvark/opencv-python/opencv/modules/core/src/arithm.cpp:241:错误:(-215)(mtype == 0 || mtype == 1)&& _mask.sameSize(* psrc1)在binary_op函数中
试试这个,对我有用。
import numpy as np
import scipy.stats as st
def gkern(kernlen=21, nsig=3):
"""Returns a 2D Gaussian kernel array."""
interval = (2*nsig+1.)/(kernlen)
x = np.linspace(-nsig-interval/2., nsig+interval/2., kernlen+1)
kern1d = np.diff(st.norm.cdf(x))
kernel_raw = np.sqrt(np.outer(kern1d, kern1d))
kernel = kernel_raw/kernel_raw.sum()
return kernel
输入:
import matplotlib.pyplot as plt
plt.imshow(gkern(21), interpolation='none')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.