ABCMOONMAN999
ABCMOONMAN999

Reputation: 111

Not getting expected output from opencv-python Laplacian operation

I am trying to use cv2.laplacian to get the edge of captured frame from camera, but the output seems not quite right.

The output looks like this: output img

Expected one like this:expected

here's the code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
while(1):
    _, frame = cap.read()
    laplacian = cv2.Laplacian(frame, cv2.CV_64F)
    # cv2.imshow('oringinal', frame)
    cv2.imshow('laplacian',laplacian)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

I think it show be some kinda edges and the overall color would not be that bright and white.

It's there something wrong with my code? I am using MAC.

Upvotes: 0

Views: 335

Answers (1)

deon cagadoes
deon cagadoes

Reputation: 602

The problem is that you don't convert the picture to uint8 image properly. It can be solved using cv.convertScaleAbs.

Try this:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
while(1):
    _, frame = cap.read()
    laplacian = cv2.Laplacian(frame, cv2.CV_64F)
    # cv2.imshow('oringinal', frame)
    abs_dst = cv.convertScaleAbs(laplacian)
    cv2.imshow('laplacian',abs_dst)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

Documentation: https://docs.opencv.org/3.4/d5/db5/tutorial_laplace_operator.html

Documentation about cv.convertScaleAbs: https://docs.opencv.org/3.4/d2/de8/group__core__array.html#ga3460e9c9f37b563ab9dd550c4d8c4e7d

Upvotes: 3

Related Questions