counting gradient magnitude