Why do edge detection results look different for uint8 and float32?

Question:

I use OpenCV’s Sobel filter for edge detection. I was wondering why the output looks different when I run the 2 following lines.

# uint8 output
img_uint8 = cv2.Sobel(img_gray, cv2.CV_8U, dx=1, dy=0, ksize=5)

# float32 output
img_float32 = cv2.Sobel(img_gray, cv2.CV_32F, dx=1, dy=0, ksize=5)

This is how the 2 outputs look:

enter image description here

The float32 output appears mostly gray whereas the uint8 output appears mostly black. Are negative values of float32 output displayed as gray? Or how are negative values treated when we display the image?

Answers:

In the uint8 case, negative values get clipped and set to 0. What you see here is an incomplete result, information is missing. You should never do this.

In the float32 case, zero is shown as middle gray, with negative values darker and positive values brighter.

Answered By: Cris Luengo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.