# Revision history [back]

### Most accurate visual representation of Gradient Magnitude

I know of a couple of ways to visually show the gradient magnitude of an image. However which is the most accurate visual representation? I know that when I calculate the magnitude its in 32bit floats and outside the grayscale visible range (0-255) thus I need to scale it and in doing so I loose accuracy. But which of the below techniques scales it most accurately? Also if there are better ways I haven't done below please let me know.

# Calculate the Gradient magnitude and direction
dX = cv2.Sobel(gray, cv2.CV_32F, 1, 0, (3,3))
dY = cv2.Sobel(gray, cv2.CV_32F, 0, 1, (3,3))
mag, direction = cv2.cartToPolar(dX, dY, angleInDegrees=True)

# Technique 1
abs_dx = cv2.convertScaleAbs(dX)
abs_dy = cv2.convertScaleAbs(dY)
alt_abs_mag = cv2.addWeighted(abs_dx, 0.5, abs_dy, 0.5, 0)

# Technique 2
abs_dx = cv2.convertScaleAbs(dX)
abs_dy = cv2.convertScaleAbs(dY)
alt_abs_mag = abs_dx + abs_dy

# Technique 3
abs_mag = cv2.convertScaleAbs(mag)

# Technique 4
abs_mag = cv2.normalize(mag, 0, 255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)