Created
February 18, 2016 22:34
-
-
Save akihikoy/313a6a5b4a80171b3e31 to your computer and use it in GitHub Desktop.
OpenCV2 example to convert the color to gray scale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <iostream> | |
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::namedWindow("gray",1); | |
cv::Mat frame,gray; | |
cap >> frame; | |
for(;;) | |
{ | |
cap >> frame; // get a new frame from camera | |
cv::cvtColor(frame,gray,CV_BGR2GRAY); | |
cv::imshow("camera", frame); | |
cv::imshow("gray", gray); | |
if(cv::waitKey(10) >= 0) break; | |
} | |
// 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