Use opencv to play multiple videos

Source: Internet
Author: User

In the field of monitoring, we often need to play multiple videos in real time. How is this implemented? A friend who uses opencv may think: I can first define a videocapture array, and then read and display videos one by one in a for loop, but this is obviously not synchronous, imagine that if a 32-path video needs to be played at the same time, and the frame rate of a general camera is 25 FPS or 30fps, play the video in the above method, the latency of the first and 32nd videos will be more than one second. If we need to perform some image processing, the latency will be greater.

If you have experience in multi-threaded or multi-core programming, you will find this problem well solved. This is a simple parallel problem. This section describes how to use OpenMP multi-core programming to play multiple videos:

# Include "opencv2/CORE/core. HPP "# include" opencv2/highgui. HPP "# include" opencv2/imgproc. HPP "# include" OMP. H "using namespace CV; using namespace STD; # define cam_num 4int _ tmain (INT argc, _ tchar * argv []) {videocapture CAP [cam_num]; mat img; string STR = "scene_1 // frame_00004d.jpg"; int I; # pragma OMP parallel for private (STR) for (I = 0; I <cam_num; I ++) {CAP [I]. open (STR);} int width = CAP [0]. get (cv_c Ap_prop_frame_width); int Height = CAP [0]. get (cv_cap_prop_frame_height); int min_width = width/2; int min_height = height/2; MAT imageshow (height, width, cv_8uc3); int left, top; MAT temp; bool runflag = true; while (1) {# pragma OMP parallel for private (IMG, temp, left, top) for (I = 0; I <cam_num; I ++) {CAP [I]> IMG; If (IMG. empty () {runflag = false; break;} resize (IMG, temp, size (min_width, min_height); left = I/2 * min _ Width; Top = I % 2 * min_height; temp. copyto (imageshow (rect (left, top, min_width, min_height);} If (! Runflag) {break;} namedwindow ("image"); imshow ("image", imageshow); waitkey (33) ;}return 0 ;}

One is as follows:


Is it interesting! You can try to modify the number of video channels. If you only play a video, there is no problem playing 4 videos with one core. a quad-core computer can try to play 16 videos simultaneously.

This article uses the idea of multi-core programming, because now multi-core computers are common, and this will certainly be a trend in the future, to learn more about the combination of OpenMP and opencv, please stay tuned to your blog !! There will be a big wave of new content to introduce to you!

Use opencv to play multiple videos

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.