Using opencv to process the video stream from camera

9 篇文章 0 订阅
6 篇文章 0 订阅

Here, we just talk about how to obtain the video stream from opencv and then to process the video stream to get new video stream.


1st, obtain the video stream from the camera.

2nd, process each frame, draw one circle, one rectangle on the frame.

Here is the code.

#include<iostream>
#include<stdio.h>
#include<opencv/highgui.h>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
const int X = 640,Y = 480;             // the size of the frame
void MyEllipse(Mat img ,double angle);   // draw ellipse
void MyRectangle(Mat img, Point a, Point b);  // draw rectangle
void MyCircle(Mat img, Point center);         // draw circle
int main(int argc,char* argv[]){
	VideoCapture cap(0);                  // open the camera
	char name[]="camera";
	if(!cap.isOpened()){
		cout << "error 1" << endl;
		return -1;
	}
	namedWindow(name, WINDOW_AUTOSIZE);    // generate a window to show image
	moveWindow(name,200,200);              // move from top left cornet to position of (200,200)
	while(1){                              // process the image in a loop
		Mat frame;
		bool bSuccess = cap.read(frame);  // obtain each frame from camera
		if(!bSuccess){
			cout << "can not read a frame from video stream" << endl;
			break;
		}
		MyCircle(frame,Point(X/2,Y/2));   // draw a circle
		MyRectangle(frame, Point(225,450), Point(415,480));   // draw a rectangle

		imshow(name, frame);              // show the frame
		if(waitKey(30)==27){
		   cout << "finished" << endl;
		   break;
		}
	}
	return 0;
}
void MyRectangle(Mat img, Point a, Point b){   // the function to draw a rectangle
    int thickness = CV_FILLED;
	int lineType = 8;
	rectangle(img, a, b, Scalar(255,0,0), thickness, lineType);
}
void MyCircle(Mat img, Point center){         // the function to drwa a circle
     int radius = 20;
	 int lineType = 8;
	 circle(img, center, radius, Scalar(0,0,255),CV_FILLED,lineType);
}

/*void MyEllipse(Mat img,double angle){    // the function to draw an ellipse
	int thickness = 2;
	int lineType = 8;
	ellipse(img, 
			Point(X/2.0,Y/2.0), 
			Size(X/4.0, Y/16.0),
			angle, 
			0, 
			360, 
			Scalar(255,0, 0), 
			thickness, 
			lineType);
}*/

	    //cout << frame.rows << " * " << frame.cols << endl;
	    /*MyEllipse(frame, 0);
		MyEllipse(frame, 90);
		MyEllipse(frame, 45);
		MyEllipse(frame, -45);
	    */



在C#中使用OpenCV库时,如果遇到"Failed to create VideoWriter"的错误,这通常意味着在尝试创建视频文件写入器时出现了问题。视频写入器通常用于将视频帧保存到文件中。这个错误可能由多种原因引起,以下是一些可能的原因和相应的解决方法: 1. 无法找到正确的编码器:确保你使用的编码器名称是系统支持的,可以通过OpenCV库中提供的方法查询支持的编码器列表。 2. 指定的输出文件已经存在:确保你尝试写入的文件名不存在,如果文件已存在,尝试删除它或者更换一个文件名。 3. 权限问题:检查你的程序是否有权限写入指定的文件路径,尤其是当路径位于受保护的系统目录时。 4. 视频写入器初始化参数错误:确保你提供的视频写入器构造函数中的参数是正确的,包括视频编码格式、帧尺寸、帧率以及输出文件名等。 5. OpenCV库与你的应用程序版本不兼容:确保你使用的OpenCV库版本与你的应用程序兼容,可能需要更新或降级OpenCV库。 6. 系统或硬件限制:在某些情况下,系统资源不足或者硬件不支持视频编码可能会导致问题。 为了解决这个问题,可以按照以下步骤进行排查: - 检查OpenCV库版本和安装状态。 - 确认视频文件路径和名称没有问题。 - 检查程序是否有足够的权限写入文件。 - 确保编码器名称和参数设置正确。 下面是使用C#调用OpenCV创建VideoWriter对象的简单示例代码: ```csharp // 引入OpenCV的命名空间 using OpenCvSharp; // 创建VideoWriter对象 var videoWriter = new VideoWriter("output.avi", VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, new Size(640, 480)); // 检查VideoWriter是否成功创建 if (!videoWriter.IsOpened()) { Console.WriteLine("Failed to create VideoWriter"); } // ... 其他代码,例如写入帧 ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值