OpenCV学习笔记(2)-----开启摄像头

    本文记录了opencv 3.1.0 在Ubuntu 16.04系统下启动笔记本缺省摄像头的一些简要代码。

  

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
	VideoCapture cap(0); /*open the default camera device*/
	if(!cap.isOpened())  /*check open result*/
	  return false;

	cout<<"Original size:"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
	cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
	cap.set(CV_CAP_PROP_FRAME_HEIGHT,120);
	cap.set(CV_CAP_PROP_FPS, 10);
	cout<<"Modified size:"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;

        cvNamedWindow("camera show", CV_WINDOW_AUTOSIZE);

	Mat frame;

	while(true)
	{
		cap >> frame;
		imshow("camera show", frame);

		if(waitKey(10) == 27) //press key “ESC” to quit from loop
		  break;
	}


	return 0;
}

下面的code是将上面显示彩色图像进行‘转灰度“->"高斯滤波"->"边沿检测"后再输出:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;
using namespace cv;


int main(int argc, char** argv)
{
	VideoCapture cap(0); /*open the default camera device*/
	if(!cap.isOpened())  /*check open result*/
	  return false;

	cout<<"Original size:"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
	cap.set(CV_CAP_PROP_FRAME_WIDTH, 160);
	cap.set(CV_CAP_PROP_FRAME_HEIGHT,120);
	cap.set(CV_CAP_PROP_FPS, 10);
	cout<<"Modified size:"<<cap.get(CV_CAP_PROP_FRAME_WIDTH)<<" "<<cap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;

        cvNamedWindow("camera show", CV_WINDOW_AUTOSIZE);

	Mat frame;
	Mat gray;

	while(true)
	{
		cap >> frame;
                cvtColor(frame, gray, CV_BGR2GRAY); //change color to gray
                GaussianBlur(gray, gray, Size(5,5), 1.5, 1.5); //Gauss filter
                Canny(gray, gray, 0, 100, 5); //edage detection
                imshow("camera show", gray);

		if(waitKey(10) == 27) //press key “ESC” to quit from loop
		  break;
	}


	return 0;
}

你可能感兴趣的:(opencv,opencv打开摄像头)