-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenCV.cpp
More file actions
61 lines (47 loc) · 1.98 KB
/
openCV.cpp
File metadata and controls
61 lines (47 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char *argv[]){
Mat server_img,client_img;
VideoCapture cap("./video.mpg");
// Get the resolution of the video
int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Video resolution: " << width << ", " << height << endl;
// Allocate container to load frames
server_img = Mat::zeros(540, 960, CV_8UC3);
client_img = Mat::zeros(540, 960, CV_8UC3);
// Ensure the memory is continuous (for efficiency issue.)
if(!server_img.isContinuous()){
server_img = server_img.clone();
}
if(!client_img.isContinuous()){
client_img = client_img.clone();
}
while(1){
// Get a frame from the video to the container of the server.
cap >> server_img;
// Get the size of a frame in bytes
int imgSize = server_img.total() * server_img.elemSize();
// Allocate a buffer to load the frame (there would be 2 buffers in the world of the Internet)
uchar buffer[imgSize];
// Copy a frame to the buffer
memcpy(buffer, server_img.data, imgSize);
// Here, we assume that the buffer is transmitted from the server to the client
// Copy a fream from the buffer to the container of the client
uchar *iptr = client_img.data;
memcpy(iptr, buffer, imgSize);
// show the frame
imshow("Video", client_img);
// Press ESC on keyboard to exit
// Notice: this part is necessary due to openCV's design.
// waitKey function means a delay to get the next frame. You can change the value of delay to see what will happen
char c = (char)waitKey(33.3333);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}