使用 scipy convolve2d 锐化图像 - 奇怪的结果
Sharpen image with scipy convolve2d - odd results
我正在尝试使用 scipy 中的 convolve2d 函数来锐化 RGB 图像。代码如下所示。对于卷积,我使用维基百科的锐化内核:https://en.wikipedia.org/wiki/Kernel_(image_processing)
输出看起来很奇怪。我不确定这里有什么不正确的地方。我已经尝试改变数组的数据类型,它给了我一些奇怪的图像。
'''
import numpy as np
from PIL import Image
image = Image.open("1.jpg")
img_array = np.array(image)
def RGB_convolve(image,kern):
image2 = np.empty_like(image)
for dim in range(image.shape[-1]):
image2[:,:,dim]=convolve2d(image[:,:,dim],kern, 'same')
return image2
KERNEL_sharpen = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
im_filtered = RGB_convolve(img_array,KERNEL_sharpen)
output_image = Image.fromarray(im_filtered)
display(output_image)
'''
In/Out Image example
更新:Image output after using Adrien Mau's suggestion
我很确定您看到了一些价值观的颠覆。
我不明白为什么会这样,但是将内核除以 255 并将初始图像设置为浮点数会产生正确的结果:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import scipy.signal
from scipy.signal import convolve2d
f = "C:/Users/Ordi Calcul/Desktop/Capture.PNG"
image = Image.open( f )
img_array = np.array(image).astype('float')
def RGB_convolve(image,kern):
image2 = np.empty_like(image)
for dim in range(image.shape[-1]):
image2[:,:,dim]=convolve2d(image[:,:,dim],kern, 'same')
return image2
KERNEL_sharpen = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]]) / 255
im_filtered = RGB_convolve(img_array,KERNEL_sharpen)
plt.figure( figsize=(10,4))
plt.subplot(1,2,1)
plt.imshow( image )
plt.subplot(1,2,2)
plt.imshow( im_filtered )
使用图像你会做:
output_image = Image.fromarray(im_filtered.astype('uint8'))
display(output_image)
我正在尝试使用 scipy 中的 convolve2d 函数来锐化 RGB 图像。代码如下所示。对于卷积,我使用维基百科的锐化内核:https://en.wikipedia.org/wiki/Kernel_(image_processing)
输出看起来很奇怪。我不确定这里有什么不正确的地方。我已经尝试改变数组的数据类型,它给了我一些奇怪的图像。 '''
import numpy as np
from PIL import Image
image = Image.open("1.jpg")
img_array = np.array(image)
def RGB_convolve(image,kern):
image2 = np.empty_like(image)
for dim in range(image.shape[-1]):
image2[:,:,dim]=convolve2d(image[:,:,dim],kern, 'same')
return image2
KERNEL_sharpen = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
im_filtered = RGB_convolve(img_array,KERNEL_sharpen)
output_image = Image.fromarray(im_filtered)
display(output_image)
''' In/Out Image example
更新:Image output after using Adrien Mau's suggestion
我很确定您看到了一些价值观的颠覆。 我不明白为什么会这样,但是将内核除以 255 并将初始图像设置为浮点数会产生正确的结果:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import scipy.signal
from scipy.signal import convolve2d
f = "C:/Users/Ordi Calcul/Desktop/Capture.PNG"
image = Image.open( f )
img_array = np.array(image).astype('float')
def RGB_convolve(image,kern):
image2 = np.empty_like(image)
for dim in range(image.shape[-1]):
image2[:,:,dim]=convolve2d(image[:,:,dim],kern, 'same')
return image2
KERNEL_sharpen = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]]) / 255
im_filtered = RGB_convolve(img_array,KERNEL_sharpen)
plt.figure( figsize=(10,4))
plt.subplot(1,2,1)
plt.imshow( image )
plt.subplot(1,2,2)
plt.imshow( im_filtered )
使用图像你会做:
output_image = Image.fromarray(im_filtered.astype('uint8'))
display(output_image)