OpenCV摄像头读取视频、存取处理后视频的代码流程

  • 例子:摄像头实时检测圆形物体的中心(用的霍夫圆变换)
#include   
using namespace std;
using namespace cv;

int main()
{
     
	//【1】从摄像头读入视频
	VideoCapture capture(1);
	//=============存取视频=====================
	//int w = capture.get(CV_CAP_PROP_FRAME_WIDTH);
	//int h = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
	//int count = capture.get(CV_CAP_PROP_FRAME_COUNT);
	//int fps = capture.get(CV_CAP_PROP_FPS);
	
	//VideoWriter wri;
	//wri.open("1.avi", CV_FOURCC('D', 'I', 'V', 'X'), fps, Size(w, h));
	//=============================================


	//【2】循环显示每一帧
	while (1)
	{
     
		Mat frame;  //定义一个Mat变量,用于存储每一帧的图像
		Mat midImage, dstImage;//临时变量和目标图的定义
		capture >> frame;  //读取当前帧
		cvtColor(frame, midImage, COLOR_BGR2GRAY);
		GaussianBlur(midImage, midImage, Size(9, 9), 2, 2);

		//【4】进行霍夫圆变换
		vector<Vec3f> circles;
		HoughCircles(midImage, circles, HOUGH_GRADIENT, 1.5, 10, 200, 100, 0, 0);
		for (size_t i = 0; i < circles.size(); i++)
		{
     
			//参数定义
			Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
			int radius = cvRound(circles[i][2]);
			//绘制圆心
			circle(frame, center, 3, Scalar(0, 255, 0), -1, 8, 0);
			//绘制圆轮廓
			circle(frame, center, radius, Scalar(155, 50, 255), 3, 8, 0);
			//wri << frame;//保存视屏
		}
		imshow("读取视频", frame);  //显示当前帧
		waitKey(10);  //延时30ms
	}
	return 0;
}

你可能感兴趣的:(采坑及软件安装)