【笔记】Opencv打开内置摄像头

        Opencv中VideoCapture是专门用来处理视频文件或者摄像头视频流的类,详细的说明和用法可以参考Opencv2.4.13的说明文档:点击打开链接

        使用VideoCapture打开内置摄像头的例子:

#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>  
 
using namespace cv; 
 
int main(int argc,char *argv[])  
{  
	VideoCapture cap(0);//打开默认的摄像头
	if(!cap.isOpened())  
	{  
		return -1;  
	}  
	Mat frame;  	
	bool stop = false;  
	while(!stop)  
	{  		
		cap.read(frame); //  或cap>>frame;			
		imshow("Video",frame);
		if(waitKey(30)==27) //Esc键退出
		{
			stop = true;  
		}  
	}
	return 0;  
}  

#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>  
 
using namespace cv; 
 
int main(int argc,char *argv[])  
{  
	VideoCapture cap(0);//打开默认的摄像头
	if(!cap.isOpened())  
	{  
		return -1;  
	}  
	Mat frame; //接收视频输入流 
	Mat embedFrame;
	cap.read(frame); //  或cap>>frame;
	int hight=frame.rows;
	int width=frame.cols;
	embedFrame=Mat::ones(Size(width/3,hight/3),CV_8UC3);
	bool stop = false;  
	while(!stop)  
	{  		
		cap.read(frame); //  或cap>>frame;
		for(int i=0;i<embedFrame.rows;i++)
		{
			for(int j=0;j<embedFrame.cols;j++)
			{
				embedFrame.at<Vec3b>(i,j)[0]=frame.at<Vec3b>(i*3,j*3)[0];
				embedFrame.at<Vec3b>(i,j)[1]=frame.at<Vec3b>(i*3,j*3)[1];
				embedFrame.at<Vec3b>(i,j)[2]=frame.at<Vec3b>(i*3,j*3)[2];
			}
		}
		Mat roi=frame(Rect(0,0,embedFrame.cols,embedFrame.rows));
		addWeighted(roi,0,embedFrame,1,0,roi,-1);		
		imshow("Video",frame);
		if(waitKey(30)==27) //Esc键退出
		{
			stop = true;  
		}  
	}
	return 0;  
} 

bool VideoCapture::set(int propId, double value) 和 double VideoCapture::get(int propId)

猜你喜欢

转载自blog.csdn.net/nyist_yangguang/article/details/121875839