Ask Your Question

Operating a web camera- how?

asked 2012-07-26 13:11:11 -0600

or gravatar image

updated 2012-07-28 03:35:16 -0600

Kirill Kornyakov gravatar image

Hi! I'm new to opencv, and not an experienced programmer. Could someone please tell me how to use opencv in my visual studio to implement code that starts my web cam and shows me the video? Thanks, Or.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-07-26 20:39:35 -0600

Hi or,

You can find tutorials on how to install OpenCV on Windows and get it running with visual studio here.

About the code to start the webcam and show the video it is really simple :

#include <opencv2/opencv.hpp>
#include <iostream>
int main (int, char**)
  //Open the first webcam plugged in the computer
  cv::VideoCapture camera(0); 
    std::cerr << "ERROR: Could not open camera" << std::endl;
    return 1;

  //Create a window to display the images from the webcam
  cv::namedWindow("Webcam", CV_WINDOW_AUTOSIZE);

  //Pick up a new frame and display it until you press a key
    //This will contain the image from the webcam
    cv::Mat frame;

    //Capture the next frame from the webcam
    camera >> frame;

    //Show the image on the window
    cv::imshow("Webcam", frame);

    //Wait for a key to be pressed
    if (cv::waitKey(30) >= 0) break;

  //Success. The program accomplished its mission and now it can go
  // to the heaven of programs.
  return 0;

Welcome to the world of OpenCV

edit flag offensive delete link more

Question Tools


Asked: 2012-07-26 13:11:11 -0600

Seen: 5,021 times

Last updated: Jul 26 '12