OpenCV 从视频或摄像头中读取图像示例

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

#include <iostream>
using namespace std;

int main(int argc, const char *argv[]) 
{

	// Get filename to the source image:
	if (argc != 2) {
		cout << "usage: " << argv[0] << " <video_name>" << endl;
		return -1;
	}

	// Build video capturer
	VideoCapture cap(argv[1]); // read from file
	//VideoCapture cap(0); // read from camera

	// Check if video is open
	if(!cap.isOpened()){
		cout << "Cannot open video/camera!" << endl;
		return -1;
	}

	// Set up window
	const char* WINDOW = "frame";
	namedWindow(WINDOW, WINDOW_AUTOSIZE);

	// Read frames
	Mat frame;
	int frameNum = 0;
	for(;;){
		// read frame and check
		cap >> frame;
		if(frame.empty()){
			cout << "frame " << frameNum << " is empty!" << endl;
			break;
		}

		// process here...


		// update frame number
		frameNum++;

		// show image
		imshow(WINDOW, frame);
		int delay = 30; // ms
		int key = waitKey(delay);
		if(27 == key || 'Q' == key || 'q' == key)
			break;
	}


}


参考

1. http://docs.opencv.org/master/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite

2. http://docs.opencv.org/doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值