How can I reduce-flicker & display thicker lines with Canny Edge detection on video with opencv?

Viewed 1821

I'm doing Canny edge detection on a video stream from my webcam and i've been trying to find parameters to reduce the flicker. I'm wonderng if thicker lines representing the edges might not do the trick.

The code I have working at the moment is

# inspired by hhttps://shahsparx.me/edge-detection-opencv-python-video-image/ttps://shahsparx.me/edge-detection-opencv-python-video-image/
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
 
while(1):
    ret, frame = cap.read()
    gray_vid = cv2.cvtColor(frame, cv2.IMREAD_GRAYSCALE)
    cv2.imshow('Original',frame)
    edged_frame = cv2.Canny(frame,100,200)
    cv2.imshow('Edges',edged_frame)
# Quit with 'Esc' key
    k= cv2.waitKey(5) & 0xFF
    if k==27:
        break
cap.release()
cv2.destroyAllWindows()

but it's very flickery, i.e. many of the detected edges appear and disappear, particular where the edges are faint like at wrinkles, hair, background elements etc. Are there opencv parameters to be used either at the videocapture or the transformations that can help out ?

Screencapture after edge detection

1 Answers

I don't have any sort of extensive knowledge on this subject, but I've been working on a project with opencv myself and researching a lot. My knowledge is probably incomplete, but here are some of the things I've found.

You can run edge detection on an image that has already had Gaussian Blur applied to it to reduce noise, as well as use an upper and lower threshold to help.

blur = cv2.GaussianBlur(cv_img, (3, 3), 0)
sigma = np.std(blur)
mean = np.mean(blur)
lower = int(max(0, (mean - sigma)))
upper = int(min(255, (mean + sigma)))

edge = cv2.Canny(blur, lower, upper)

After that, if there are still issues, you can use the opencv erosion or dilation filters in order to thicken or reduce the edges where necessary. More info on erosion and dilation here: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

Related