In opencv, the video is saved as continuous images and continuous images as videos.

Source: Internet
Author: User
Convert from: watermark (.jpg,.bmp), and save the continuous image as a video for Demonstration:

  Paste the Code directly. After learning opencv for a week, you can definitely understand it:
# Include <stdlib. h> # include <stdio. h> # include <math. h> # include <cv. h> # include 
// Initialize a video file catcher cvcapture * capture = cvcapturefromavi (filename );
// Obtain the video information cvqueryframe (capture); int frameh = (INT) cvgetcaptureproperty (capture, cv_cap_prop_frame_height); int framew = (INT) cvgetcaptureproperty (capture, cv_cap_prop_frame_width ); int FPS = (INT) cvgetcaptureproperty (capture, cv_cap_prop_fps); int numframes = (INT) cvgetcaptureproperty (capture, duration); printf ("tvideo Height: % dntvideo width: % dntfps: % dntframe numbers: % DN ", frameh, framew, FPS, numframes );
// Define and initialize the int I = 0; iplimage * IMG = 0; char image_name [13]; cvnamedwindow ("mainwin", cv_window_autosize );
// Read and display while (1) {IMG = cvqueryframe (capture); // obtain an image cvshowimage ("mainwin", IMG ); // display the char key = cvwaitkey (20); sprintf (image_name, "% S % d % s", "image", ++ I ,". jpg "); // name of the saved image cvsaveimage (image_name, IMG); // save an image if (I = num_frame) break;} cvreleasecapture (& capture ); cvdestroywindow ("mainwin");} void image_to_video () {int I = 0; iplimage * IMG = 0; char image_name [13]; printf ("------------- image to video... ---------------- N ");
// Initialize the video writer. Modify the parameter cvvideowriter * Writer = 0 according to the actual video file; int iscolor = 1; int FPS = 30; // or 25 int framew = 400; // 744 for Firewire cameras int frameh = 240; // 480 for Firewire cameras writer = cvcreatevideowriter ("out. avi ", cv_fourcc ('x', 'V', 'I', 'D'), FPS, cvsize (framew, frameh), iscolor); printf (" tvideo height: % dntvideo width: % dntfps: % DN ", frameh, framew, FPS );
// Create a window cvnamedwindow ("mainwin", cv_window_autosize); While (I <num_frame) {sprintf (image_name, "% S % d % s", "image ", ++ I ,". jpg "); IMG = cvloadimage (image_name); If (! IMG) {printf ("cocould not load image file... N "); exit (0);} cvshowimage (" mainwin ", IMG); char key = cvwaitkey (20); cvwriteframe (writer, IMG );} cvreleasevideowriter (& writer); cvdestroywindow ("mainwin");} int main (INT argc, char * argv []) {char filename [13] = "1.avi "; video_to_image (filename); // return 0 for video conversion ;}

Convert from: watermark (.jpg,.bmp), and save the continuous image as a video for Demonstration:

  Paste the Code directly. After learning opencv for a week, you can definitely understand it:
# Include <stdlib. h> # include <stdio. h> # include <math. h> # include <cv. h> # include 
// Initialize a video file catcher cvcapture * capture = cvcapturefromavi (filename );
// Obtain the video information cvqueryframe (capture); int frameh = (INT) cvgetcaptureproperty (capture, cv_cap_prop_frame_height); int framew = (INT) cvgetcaptureproperty (capture, cv_cap_prop_frame_width ); int FPS = (INT) cvgetcaptureproperty (capture, cv_cap_prop_fps); int numframes = (INT) cvgetcaptureproperty (capture, duration); printf ("tvideo Height: % dntvideo width: % dntfps: % dntframe numbers: % DN ", frameh, framew, FPS, numframes );
// Define and initialize the int I = 0; iplimage * IMG = 0; char image_name [13]; cvnamedwindow ("mainwin", cv_window_autosize );
// Read and display while (1) {IMG = cvqueryframe (capture); // obtain an image cvshowimage ("mainwin", IMG ); // display the char key = cvwaitkey (20); sprintf (image_name, "% S % d % s", "image", ++ I ,". jpg "); // name of the saved image cvsaveimage (image_name, IMG); // save an image if (I = num_frame) break;} cvreleasecapture (& capture ); cvdestroywindow ("mainwin");} void image_to_video () {int I = 0; iplimage * IMG = 0; char image_name [13]; printf ("------------- image to video... ---------------- N ");
// Initialize the video writer. Modify the parameter cvvideowriter * Writer = 0 according to the actual video file; int iscolor = 1; int FPS = 30; // or 25 int framew = 400; // 744 for Firewire cameras int frameh = 240; // 480 for Firewire cameras writer = cvcreatevideowriter ("out. avi ", cv_fourcc ('x', 'V', 'I', 'D'), FPS, cvsize (framew, frameh), iscolor); printf (" tvideo height: % dntvideo width: % dntfps: % DN ", frameh, framew, FPS );
// Create a window cvnamedwindow ("mainwin", cv_window_autosize); While (I <num_frame) {sprintf (image_name, "% S % d % s", "image ", ++ I ,". jpg "); IMG = cvloadimage (image_name); If (! IMG) {printf ("cocould not load image file... N "); exit (0);} cvshowimage (" mainwin ", IMG); char key = cvwaitkey (20); cvwriteframe (writer, IMG );} cvreleasevideowriter (& writer); cvdestroywindow ("mainwin");} int main (INT argc, char * argv []) {char filename [13] = "1.avi "; video_to_image (filename); // return 0 for video conversion ;}

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.