Skip to content

Instantly share code, notes, and snippets.

@akihikoy
Created February 18, 2016 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akihikoy/38825f339dbf24abf77c to your computer and use it in GitHub Desktop.
Save akihikoy/38825f339dbf24abf77c to your computer and use it in GitHub Desktop.
A simple example of OpenCV... Just capture images from a USB camera, and display it.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <cstdio>
int main(int argc, char **argv)
{
cv::VideoCapture cap(0); // open the default camera
if(argc==2)
{
cap.release();
cap.open(atoi(argv[1]));
}
if(!cap.isOpened()) // check if we succeeded
{
std::cerr<<"no camera!"<<std::endl;
return -1;
}
std::cerr<<"camera opened"<<std::endl;
cv::namedWindow("camera",1);
cv::Mat frame;
for(;;)
{
cap >> frame; // get a new frame from camera
cv::imshow("camera", frame);
int c(cv::waitKey(10));
if(c=='\x1b'||c=='q') break;
// usleep(10000);
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment