Using opencv to process the video stream from camera

来源:互联网 发布:电脑屏幕录制软件 免费 编辑:程序博客网 时间:2024/06/05 21:04

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 framevoid MyEllipse(Mat img ,double angle);   // draw ellipsevoid MyRectangle(Mat img, Point a, Point b);  // draw rectanglevoid MyCircle(Mat img, Point center);         // draw circleint main(int argc,char* argv[]){VideoCapture cap(0);                  // open the camerachar name[]="camera";if(!cap.isOpened()){cout << "error 1" << endl;return -1;}namedWindow(name, WINDOW_AUTOSIZE);    // generate a window to show imagemoveWindow(name,200,200);              // move from top left cornet to position of (200,200)while(1){                              // process the image in a loopMat frame;bool bSuccess = cap.read(frame);  // obtain each frame from cameraif(!bSuccess){cout << "can not read a frame from video stream" << endl;break;}MyCircle(frame,Point(X/2,Y/2));   // draw a circleMyRectangle(frame, Point(225,450), Point(415,480));   // draw a rectangleimshow(name, frame);              // show the frameif(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 ellipseint 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);    */



0 0
原创粉丝点击