Rit*_*izh 6 python image-processing scipy
和我之前的许多人一样,我正在尝试实现冈萨雷斯和伍兹“数字图像处理”书中的图像锐化示例。
我创建一个负拉普拉斯核 (-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()
Run Code Online (Sandbox Code Playgroud)


问题是最终的锐化图像看起来褪色且呈灰色。我尝试进行直方图均衡化以使其更具对比度,但结果很奇怪。我考虑过应用伽玛校正,但我不喜欢自愿选择伽玛系数。
看来一定有一种简单方便的方法可以让图像恢复到原来的动态范围。我很感激对代码的想法和评论。谢谢你!
在我看来,部分问题与你如何重新调整规模有关Lap。我认为您不想首先减去最小值 - 锐化应该降低某些像素的强度并增加其他像素的强度。您可能还希望调整乘以的缩放因子Lap,以控制锐化程度(255 可能太极端)。
最终图像中背景看起来呈灰色的原因可能是因为在添加负拉普拉斯算子后,月亮中会有比背景更暗的像素(图像这部分的拉普拉斯算子的幅度会更大,因为它包含了更多的拉普拉斯算子)。局部结构)。这意味着您重新缩放背景像素将映射到某个值> 0。如果您不从中减去最小值,Lap那么这些较暗的像素将具有负值,因此您可以剪切结果图像中的像素值,这样它们都 > 0。这样你最终会得到纯黑色的背景。