Learning opencv learning notes series (3) display pictures and videos

Source: Internet
Author: User

Opencv is a computer vision library, so there are only two objects to process: "Images" and "videos" (in fact, videos are also extracted into single-frame images for processing. In general, or image processing ).

To learn opencv, you must first know how opencv opens the "image" and "video" files and then displays them.


To implement these functions, perform the following steps:


1. Create a project


Open vs2010, create a project, and select"Win32 console application"(Using the console can save a lot of trouble), named"Helloopencv", Click"OK"Button.


Next step


Remember to check"Empty Project", And then click"Complete"


After the project is created, the project has four folders: "external dependencies", "header files", "source files", and "resource files"





Now, the project is created, and the source code file is also created. The code is written below.


2. write code

Enter the following code in the "Main. cpp" file:


// Created by jimmygong20140903 // main. CPP: Implemented file // * The opencv header file contains */# include <stdio. h> # include <iostream> # include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> using namespace CV; using namespace STD; // load the image and display void loadimage_display () {mat myimage = imread ("airplane.jpg "); // load the specified image namedwindow ("Single Image Display"); // create a window named "Single Image Display" imshow ("Single Image Display", myimage ); // display the specified image waitkey (0) in the specified window;} // load the video and display void L Oadvideo_display () {mat myframe; cvcapture * mycap; namedwindow ("video playback", window_autosize); // create a window named "video playback" mycap = cvcreatefilecapture ("Megamind. avi "); myframe = cvqueryframe (mycap); While (1) {myframe = cvqueryframe (mycap); If (myframe. empty () // determines whether the frame is null {cout <"video file playback completed" <Endl; break;} Char chkeycode = cvwaitkey (20 ); if (chkeycode = 27) {break;} imshow ("video playback", myframe) ;}cout <"press any key to exit! "<Endl; waitkey (0) ;}int main () {int whichtypefile = 0; char CSTR; bool isgoodinput = false; cout <"Enter the file type to be displayed! \ N input "0" as the image file, input "1" as the video file, other invalid! \ N "<Endl; // CSTR = cin. Get (); While (! Isgoodinput) {CIN> CSTR; If (! Isdigit (CSTR) {cout <"enter" 0 "or" 1 ". Thank you! "<Endl ;}else {cout <" \ N input successful! "<Endl; isgoodinput = true ;}// whichtypefile = atoi (CSTR); whichtypefile = int (CSTR-'0 '); // char --> INTIF (0 = whichtypefile) {loadimage_display ();} else if (1 = whichtypefile) {loadvideo_display ();} else {cout <"the input is invalid! \ N "<" Please set any key to exit! "<Endl; waitkey (0);} return 0 ;}


3. Add the resource file

After the encoding is completed, you must put the desired image and video files in the project directory. Otherwise, the program fails to read the specified image and video files.

In this example, the "airplane.jpg" and "Megamind. Avi" files in opencv2.4.9 source code are copied to the project directory ("H: \ practice \ helloopencv.


4. debugging and Testing

When the code and resource files are correct, start to debug the program and compile the program smoothly. The running screen is as follows:




Now, the simplest function of Image Display and video playback has been implemented. Is it easy?


The source code project for this demonstration is as follows: helloopencv

OK. Come here first! Goodbye to next article!

Learning opencv learning notes series (3) display pictures and 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.