To compute the gradient magnitude, I used the humble finite difference Dx=[1 -1] and Dy = [1 -1]T to convolve with the image to get the gradient in x and y directons. Then I take the square root of their quadratic sum to get the magnitude of the gradient.
The smoothed image has less noises and clearer edges. I can convolve just once and get an equivalent result.
The train one kinda failed since the white train on the right is pretty visible both close and far.