【发布时间】:2017-01-29 20:40:40
【问题描述】:
与我之前的许多人一样,我正在尝试实现 Gonzalez 和 Woods “数字图像处理”一书中的图像锐化示例。
我创建一个负拉普拉斯核 (-1, -1, -1; -1, 8, -1; -1, -1,-1) 并将其与图像进行卷积,然后从原始结果中减去结果图片。 (我还尝试采用正拉普拉斯算子 (1, 1, 1; 1, -8, 1; 1, 1, 1) 并将其添加到图像中)。在每个阶段,我都会将结果拟合到 (0, 255) 范围内,归一化的拉普拉斯算子看起来不错,而且像预期的那样是灰色的。
import matplotlib.cm as cm
import scipy.misc
import scipy.ndimage.filters
#Function for plotting abs:
pic_n = 1
def show_abs(I, plot_title):
plt.title(plot_title)
plt.tight_layout()
plt.axis('off')
plt.imshow(abs(I), cm.gray)
#Reading the image into numpy array:
A = scipy.misc.imread('moon1.jpg', flatten=True)
plt.figure(pic_n)
pic_n += 1
show_abs(A, 'Original image')
A -= np.amin(A) #map values to the (0, 255) range
A *= 255.0/np.amax(A)
#Kernel for negative Laplacian
kernel = np.ones((3,3))*(-1)
kernel[1,1] = 8
#Convolution of the image with the kernel:
Lap = scipy.ndimage.filters.convolve(A, kernel)
#Laplacian now has negative values in range (-255, 255):
print('L', np.amax(Lap), np.amin(Lap))
plt.figure(pic_n)
pic_n += 1
show_abs(Lap, 'Laplacian')
#Map Laplacian to the (0, 255) range:
Lap -= np.amin(Lap)
Lap *= 255.0/np.amax(Lap)
print('L', np.amax(Lap), np.amin(Lap))
plt.figure(pic_n)
pic_n += 1
show_abs(Lap, 'Normalized Laplacian')
A += Lap #Add negative Laplacian to the original image
print('A', np.amax(A), np.amin(A))
A -= np.amin(A)
A *= 255.0/np.amax(A)
print('A', np.amax(A), np.amin(A))
plt.figure(pic_n)
pic_n += 1
show_abs(A, 'Laplacian filtered img')
plt.show()
原图:
结果:
问题是最终锐化的图像看起来褪色和灰色。我尝试进行直方图均衡以使其更具对比度,但结果很奇怪。我考虑过应用 gamma 校正,但我不喜欢自愿选择 gamma 系数。
似乎必须有一种简单方便的方法将图像恢复到原始动态范围。我将不胜感激有关代码的想法和 cmets。谢谢!
【问题讨论】:
标签: python image-processing scipy