Python 图像膨胀导致亮度下降
我正在尝试使用以下算法实现图像扩展:Python 图像膨胀导致亮度下降,python,image-processing,Python,Image Processing,我正在尝试使用以下算法实现图像扩展: 1. Zero pad: Put a zero pixel between two pixels 2. Convolve with a column vector of a Gaussian, and multiply by 2 3. Convolve with a row vector of a Gaussian and multiply by 2 4. Return blurred image. 以下是我使用的代码(我使用3表示guassian尺寸):
1. Zero pad: Put a zero pixel between two pixels
2. Convolve with a column vector of a Gaussian, and multiply by 2
3. Convolve with a row vector of a Gaussian and multiply by 2
4. Return blurred image.
以下是我使用的代码(我使用3表示guassian尺寸):
以下分别是原始图像和展开图像:
原件:
扩大:
这是gaussian_内核的实现,但我已经验证过,对于内核_size=3,它给出[0.25,0.75,0.25]:
def gaussian_kernel(kernel_size):
''' Takes a kernel size that is odd and returns a 2D gaussian
kernel of this size. Results are undefined for even sizes.
Arguments:
kernel_size - The size of the kernel, an odd integer.
Returns:
gaussian - A normalized gaussian kernel of given size.
'''
if kernel_size == 1:
return np.array([0,1,0],dtype='float32')
# Create a gaussian kernel
gaussian = np.array([1,1],dtype='float32')
# Convolve it with itself kernel_size times
convolved = gaussian
num_of_convolutions = kernel_size-2
for i in range(0,num_of_convolutions):
convolved = np.convolve(convolved,gaussian)
# Normalize it
sum = convolved.sum()
nor_gaussian = np.divide(convolved,sum,dtype='float32')
return np.array([nor_gaussian,]).astype(np.float32)
如果在没有“mode”参数的情况下使用ndimage.filters.convalve,则默认情况下会使用“reflect”,这似乎意味着它会反映接近图片末尾的值,这似乎会导致问题。使用“mode=constant”解决了这个问题。您在哪一行使用“mode=constant”?我看不出有什么问题。将mode=“constant”添加到卷积中,问题就解决了。
def gaussian_kernel(kernel_size):
''' Takes a kernel size that is odd and returns a 2D gaussian
kernel of this size. Results are undefined for even sizes.
Arguments:
kernel_size - The size of the kernel, an odd integer.
Returns:
gaussian - A normalized gaussian kernel of given size.
'''
if kernel_size == 1:
return np.array([0,1,0],dtype='float32')
# Create a gaussian kernel
gaussian = np.array([1,1],dtype='float32')
# Convolve it with itself kernel_size times
convolved = gaussian
num_of_convolutions = kernel_size-2
for i in range(0,num_of_convolutions):
convolved = np.convolve(convolved,gaussian)
# Normalize it
sum = convolved.sum()
nor_gaussian = np.divide(convolved,sum,dtype='float32')
return np.array([nor_gaussian,]).astype(np.float32)