I'm reading the book "Programming Computer Vision with Python" by Jan Erik Solem, final draft (CC licence) is available here.
On page 34 the result of applying the Sobel filters to an image is shown, see Fig 1.10, also shown below. When I run the code in the book, the image of the gradient magnitude, i.e. panel (d), looks reversed, see below.
My question is, is this just because the author inverted the image, or is there some other reason?
The Python code, adapted from the book with additions for plotting, is listed below.
These are the image generated with the code in the book.
Python Code
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()
The image used in the example code
Try imshow(magnitude, cmap='gray')
to explicitly declare the colormap. If it is still inverted, try cmap='gray_r'
to use the inverted grayscale colormap.