Manoj
Manoj

Reputation: 971

Sobel operator - Open Cv Python

Img is my input image which is in RGB

import cv2
import numpy as np
img = cv2.imread("Lenna.png")
black = cv2.cvtColor(img , cv2.cv.CV_BGR2GRAY)

Now I would want to apply a sobel operator to it using filter2D

sobel = np.array([[-1 , 0 , 1] , [-2 , 0 , 2] , [-1 , 0 , 1] ])
dest = cv2.filter2D(black , -1 , sobel)    

The -1 parameter according to the docs is the depth of the image which is I believe is 'uint8' or cv2.cv.CV_8U , and when I do np.max(dest) it gives me 255 , which is expected.

However when I specify my depth as cv2.cv.CV_32F , i.e

dest = cv2.filter2D(black , cv2.cv.CV_32F , sobel)

and do np.max(dest) , it gives me a different value greater than 255. Could someone explain the reason?

Upvotes: 2

Views: 3671

Answers (1)

sgarizvi
sgarizvi

Reputation: 16796

The function filter2D performs image filtering in full precision and then saturates the resulting values to the destination data type. Actually, the output image contains a larger range of values. When you specify the type CV_32F the output values are kept as is. They may contain values larger than 255 and less than 0. But when the type is CV_8U, the results are clamped to the range of CV_8U data type. All the negative values become 0 and all the values larger than 255, will become 255.

Upvotes: 4

Related Questions