opencv从摄像头获取图片帧并做边缘检测

// opencv6_videocapturefromcamera.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

using namespace cv;
using namespace std;

//debug下在lib文件的名称后加d,release下不加d。


int main()
{
	VideoCapture capture(0);	// 0为电脑自带摄像头
	/*capture.open(0);*/
	if (capture.isOpened())
	{
		cout << "已打开摄像头" << endl;
	}
	else
	{
		cout << "摄像头打开失败" << endl;
	}

	while (1)
	{
		Mat frame;
		capture >> frame;	//读取当前帧
		if (frame.data == NULL)
			break;
		imshow("读取视频的当前帧", frame);

		Mat edges;
		cvtColor(frame, edges, COLOR_BGR2GRAY);	//转为灰图
		blur(edges, edges, Size(8, 8));		//降噪
		Canny(edges, edges, 3, 9, 3);	//边缘检测

		imshow("边缘检测效果", edges);

		if (waitKey(30) >= 0)
			break;
	}
	capture.release();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章