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