Canny Edge Detection on Webcam

Steps to Canny Edge Detection on Webcam | OpenCV with Visual Studio in Windows 10. In this Tutorial, we are going to implement Canny Edge Detection on Webcam using OpenCV.
  1. First of all, Follow this tutorial to Install & Configure OpenCV with Visual Studio 2015
  2. #include<opencv2/core/core.hpp>
    #include<opencv2/highgui/highgui.hpp>
    #include<opencv2/imgproc/imgproc.hp>
    
    #include<iostream>
    #include<conio.h>         
    
    int main() {
     cv::VideoCapture capWebcam(0);   // declare a VideoCapture object to associate webcam, 0 means use 1st (default) webcam
    
     if (capWebcam.isOpened() == false)  //  To check if object was associated to webcam successfully
     {
      std::cout << "error: Webcam connect unsuccessful\n"; // if not then print error message
      return(0);            // and exit program
     }
    
     cv::Mat imgOriginal;        // input image
     cv::Mat imgGrayscale;       // grayscale image
     cv::Mat imgBlurred;         // blured image
     cv::Mat imgCanny;           // Canny edge image
    
     char charCheckForEscKey = 0;
     int lowTh = 45;
     int highTh = 90;
    
     while (charCheckForEscKey != 27 && capWebcam.isOpened()) {            // until the Esc key is pressed or webcam connection is lost
      bool blnFrameReadSuccessfully = capWebcam.read(imgOriginal);   // get next frame
    
      if (!blnFrameReadSuccessfully || imgOriginal.empty()) {    // if frame read unsuccessfully
       std::cout << "error: frame can't read \n";      // print error message
       break;               
      }
    
      cv::cvtColor(imgOriginal, imgGrayscale, CV_BGR2GRAY);                   // convert to grayscale
    
      cv::GaussianBlur(imgGrayscale,imgBlurred,cv::Size(5, 5),1.8);           // Blur Effect             
    
      cv::Canny(imgBlurred,imgCanny,lowTh,highTh);       // Canny Edge Image
    
      //declare windows
      cv::namedWindow("imgOriginal", CV_WINDOW_NORMAL);      
      cv::namedWindow("imgCanny", CV_WINDOW_NORMAL);    
      //declare trackbar
      cv::createTrackbar("LowTh", "imgCanny", &lowTh, 100);
      cv::createTrackbar("HighTh", "imgCanny", &highTh, 100);
      // show windows
      cv::imshow("imgOriginal", imgOriginal);                 
      cv::imshow("imgCanny", imgCanny);                       
    
      charCheckForEscKey = cv::waitKey(1);        // delay and get key press
     }
    
     return(0);
    }
    
    OR
    Get CannyCam.cpp from Github:
    Download CannyCam.cpp

  3. Paste full source code and Run it (Ctrl+F5 or F5).

OUTPUT
Input Image OpenCV
Original Webcam Image OpenCV
Canny Edge Detection Webcam OpenCV
Canny Edge Image Webcam OpenCV


Explaination : Function used: cv::Canny(imgBlurred,imgCanny,lowTh,highTh) - This function can process images and implement the Canny Edge Detector Algorithm.
  • 1st parameter is the source image.
  • 2nd parameter is the destination or resultant image.
  • 3rd parameter is the low threshold value.
  • 4th parameter is the high threshold value.
  •  

Like, Share and Comment Below. You may also like this


  • Apply Canny Edge Effect on an Image


  • 2 comments:

    1. Wow useful for newbie like me..
      keep share!!

      ReplyDelete
    2. Thanks, works perfect! But you need to add a "p" in line 3 "imgproc.hpp"

      ReplyDelete