我正在阅读Jan Erik Solem撰写的《用Python编程计算机视觉》一书,here提供了最终草案(CC许可证)。
在第34页上,显示了将Sobel滤镜应用于图像的结果,请参见下面的图1.10。当我运行书中的代码时,梯度幅值的图像(即面板(d))看起来是反转的,请参见下文。
我的问题是,这是仅因为作者颠倒了图像,还是其他原因?
下面列出了从本书改编而来的Python代码。
这些是书中代码生成的图像。
Python代码
from PIL import Image
from numpy import *
from pylab import *
from scipy.ndimage import filters
im = array(Image.open('empire.jpg').convert('L'))
# Sobel derivative filters
imx = zeros(im.shape)
filters.sobel(im,1,imx)
imy = zeros(im.shape)
filters.sobel(im,0,imy)
magnitude = sqrt(imx**2+imy**2)
figure(figsize=(12,4))
gray()
subplot(1,4,1)
title('Oiginal')
axis('off')
imshow(im)
subplot(1,4,2)
title('imx')
axis('off')
imshow(imx)
subplot(1,4,3)
title('imy')
axis('off')
imshow(imy)
subplot(1,4,4)
title('magnitude')
axis('off')
imshow(magnitude)
savefig('sobel.png')
show()
示例代码中使用的图片
答案 0 :(得分:1)
尝试imshow(magnitude, cmap='gray')
显式声明颜色图。如果仍然反转,请尝试cmap='gray_r'
使用反转的灰度色图。