Lê Huy Hùng
Lê Huy Hùng

Reputation: 265

opencv Laplacian function not work effectively like in document

here is my code

img_original=cv2.imread("sudoku-original.jpg",0)
cv2.imshow("original",img_original)
laplacian = cv2.Laplacian(img_original,cv2.CV_64F)
cv2.imshow("laplace",laplacian)

I want result like in the document but It don't.

here is the link of document:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_gradients/py_gradients.html#gradients

enter image description here

Upvotes: 2

Views: 2131

Answers (2)

Pavel Lebedev
Pavel Lebedev

Reputation: 1

Аfter changing the ksize and ddepth params, the resulting image matched the example from OpenCV python tutorial:

img = cv.imread(cv.samples.findFile('sudoku.png'), cv.IMREAD_GRAYSCALE)

laplacian = cv.Laplacian(img,cv.CV_8U, ksize=3)
 
plt.subplot(1,2,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(1,2,2),plt.imshow(laplacian,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
 
plt.show()

The resulting image

Upvotes: 0

janu777
janu777

Reputation: 1978

laplacian = cv2.Laplacian(img_original,cv2.CV_64F)

The above line implies that the format of the image is CV_64F which is an array of float values. So when you use cv2.imshow() function, it works in a way like: values greater than 1.0 will be white pixels and values lesser than 0.0 will be black.

So you will need to convert it to CV_8U. There are many ways to do it, I generally use this:

laplacian = cv2.Laplacian(img,cv2.CV_64F)
ret,thresh = cv2.threshold(laplacian,0,255.0,cv2.THRESH_TOZERO)
laplacian8 = np.uint8(thresh)
cv2.imshow('sud',laplacian8)

This gave me the result:

Result

check this link to learn more about the problem.

Upvotes: 3

Related Questions