#dotnet – detecting edges on the 🎦 camera feed with Canny algorithm, #OpenCV and #net5

Buy Me A Coffee

Hi !

The Canny edge detection is one the most popular algorithms, and it’s also +30 years old.

The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images. It was developed by John F. Canny in 1986.

OpenCV includes the Canny algorithm as part of the base set of operations to perform on an image. You only need 2 lines of code to get this output.

canny demo effect using camera

The OpenCV canny tutorial (see references) explain the different steps to detect edges:

  • Noise Reduction
  • Finding Intensity Gradient of the Image
  • Non-maximum Suppression
  • Hysteresis Thresholding

I’ll keep this simple, just sharing the code !

using OpenCvSharp;
var capture = new VideoCapture(0);
var window = new Window("El Bruno – OpenCVSharp Effects demo");
var image = new Mat();
var imageNew = new Mat();
bool applyCanny = false;
bool run = true;
while (run)
if (image.Empty()) break;
if (applyCanny)
imageNew = applyCannyEffect(image);
imageNew = image.Clone();
switch ((char)Cv2.WaitKey(100))
case (char)27: // Esc – Exit
run = false;
case 'c':
applyCanny = !applyCanny;
Mat applyCannyEffect(Mat image)
var newImage = new Mat();
Cv2.Canny(image, newImage, 50, 200);
return newImage;

That’s all for today!

Happy coding!


El Bruno