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);
	    */



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值