Today I am trying to get input from default camera, with OpenCV.

OpenCV hasa great library called “videoio” which can handle video input/output from filesystem or imaging devices. This library has a class cv::VideoCapture which can handle video capture from device as well as file system. OpenCV offers a clear documentation for its implementation and its pretty straight forward.

Following code utilizes default camera device of computer (laptop-webcam in my case) as input device for capturing image. Next, it implements

 #include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, COLOR_BGR2GRAY); // change from color to grayscale
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

The function cvtColor changes color space of the source image, like we have changed it from color to gray scale. This function has pretty elaborate documentation so better follow the hyperlink.

The function GaussianBlur convolves the image with gaussian distribution. This in effect preserves the boundary/edges of the source image while blurring or dissolving middle-pixels. More about gaussian blurring can be found here. In the GaussianBlur function documentation, its written that “the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F”. So what exactly is CV_8U etc. This question is well addressed in one such stack-overflow question. In short however, these are magnitude for each pixel in respective data types i.e int, float etc.

Lastly, we are using canny function for edge detection. This function implements canny algorithm. More about canny algorithm can be found in this paper.

Finally we display our image using imshow function. As, we are in an infinite for loop our program will keep on displaying transformed captured video as taken from our webcam.